Laravel - ErrorException Undefined variable - php

I have tried to refer all the possible solutions in stackoverflow but could not solve this.
This is my Controller.
public function getMessages(){
$messages = Message::all()->toArray();
return View('admin.thesis', ['messages' => $messages]);
}
This is my route/web.php
Route::get ('/thesis','MessagesController#getMessages');
And this is my view
<div class="panel-heading">Thesis Details</div>
<div class="panel-body">
<table class="table table-bordered">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>Contact</th>
<th>State</th>
<th>Country</th>
<th>Article</th>
<th>Author</th>
<th>Uploaded File</th>
</tr>
#foreach($messages as $row)
<tr>
<td>{{$row['firstName']}}</td>
<td>{{$row['lastName']}}</td>
<td>{{$row['email']}}</td>
<td>{{$row['contactNumber']}}</td>
<td>{{$row['state']}}</td>
<td>{{$row['country']}}</td>
<td>{{$row['article']}}</td>
<td>{{$row['author']}}</td>
<td>
<a href="{{ URL::to('/') }}/uploads/{{$row['file_path']}}">
{{$row['file_path']}}
</a>
</td>
</tr>
#endforeach
</table>
</div>
</div>
This is my error
ErrorException
Undefined variable: messages (View:
C:\xampp\htdocs\fileupload\resources\views\admin\thesis.blade.php)
How do I resolve this?

Try this on your Controller
public function getMessages(){
$messages = Message::all();
return view('admin.thesis')->with(['messages' => $messages]);
}
And in your blade file
#foreach($messages as $row)
<tr>
<td>{{ $row->firstName }}</td>
<td>{{ $row->lastName }}</td>
<td>{{ $row->email }}</td>
<!-- etc... -->
<td>
<a href="{{ URL::to('/') }}/uploads/{{ $row->file_path }}">
{{ $row->file_path }}
</a>
</td>
</tr>
#endforeach

Try this on your Controller
public function getMessages(){
$messages = Message::all()->toArray();
return view('admin.thesis', compact('messages'));
}

I used following code and its work for me :
class SidebarController extends Controller
{
public function news_widget() {
$posts = Post::take(5)->orderBy('updated_at', 'DESC')->take();
return view('index', array('data'=>$posts));
}
}
And within the view I simply use $data and its working fine for me.

Related

Attempt to read property "name" on array (View: C:\xampp\htdocs\Testing\resources\views\product.blade.php)

I'm beginner in laravel, when I put stats table in blade I got this warning
Attempt to read property "name" on array (View: C:\xampp\htdocs\Testing\resources\views\product.blade.php)
This is Controller
public function index()
{
$response = Http::get('https://api.lazada.co.id/rest/products/get?filter=live&app_key=100132&sign_method=sha256&timestamp=1612318435886&access_token=50000801006o5nrcA5192d1f9ag1FHQBUqffCEyCmrXDohvhzExSkczUnnxJ4y&sign=F31584775544970D59AB58EC4B1B77933BC2D32401E33C1D2D5095690C31627C');
$data = $response->json();
return view('product',compact ('data'));
}
This is view:
#extends('layout/main')
#section ('title', 'Testing Laravel')
#section ('container')
<div class="container">
<div class="row">
<div class="col-10">
<h1 class="mt-3">List Product</h1>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Desc</th>
<th scope="col">Brand</th>
<th scope="col">Clothing Material</th>
<th scope="col">Leather Material</th>
</tr>
</thead>
<tbody>
#foreach($data as $datas)
<tr>
<th scope="row">1</th>
<td>{{ $datas->name }}</td>
<td>{{ $datas-description }}</td>
<td>{{ $datas->brand }}<td>
<td>{{ $datas->clothing_material }}</td>
<td>{{ $datas->leather_material }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
According to the error message its clearly saying Attempt to read property (because you are trying to access like $data->name <--OBJECT) on array and your foreach variable is array. so you will need to access like key value pair.
Use like this in loop-
{{ $data['name'] }}
{{ $data['description'] }}

Laravel Blade not showing data from controller

I want to show data in tabular form in Laravel Blade View. Now, data is not showing up in blade, but when I check in controller using print_r it shows up Following is the code for Controller:-
public function index(){
$pickup = PickupLocation::select('*')
->whereNull('deleted_at')
->get()
->toArray();
$page = 'pickup_locations';
return view('backEnd.pickupManagement.index',compact('page'));
}
Following is the code in web.php:-
Route::match(['get','post'],'pickup-locations','backEnd\pickupManagement\PickupController#index');
And following is the code I am using View:-
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Address</th>
<th width="13%">Action</th>
</tr>
</thead>
<tbody>
#if(!empty($pickup))
#foreach($pickup as $key=>$value)
<tr>
<td>{{ ucfirst($value['name']) }}</td>
<td>{{ ucfirst($value['contact_no']) }}</td>
<td>{{ $value['location'] }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
add $pickup variable as second argument in your compact method.
Try like this
return view('backEnd.pickupManagement.index',compact('page', 'pickup')); }
Happy codding

Pagination in laravel returning method undefined

The pagination method returns App\Article::link undefined, any ideas? I thought this was a helper method
public function index()
{
$data = Article::paginate(5);
return view('datatypes.articles.index', compact('data'));
}
in my view:
<tbody>
#foreach($data as $data)
<tr>
<th scope="row">{{$data->id}}</th>
<td>{{$data->title}}</td>
</tr>
#endforeach
</tbody>
{{ $data->links() }}
<tbody>
#foreach($data as $_data)
<tr>
<th scope="row">{{$_data->id}}</th>
<td>{{$_data->title}}</td>
</tr>
#endforeach
</tbody>
{{ $_data->links() }}
try this in your view
<tbody>
#foreach($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->title}}</td>
</tr>
#endforeach
</tbody>
{{ $data->links() }}

pagination not working when clicking on page number

I am using laravel pagination. When I click on next page or any page number, nothing happens.
Controller function
public function getIndex()
{
$articles = Inspector::paginate(15);
return view('admin.inspector.test', compact('articles'));
}
View file
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}
Any help is much appreciated...Thanks...
public function getIndex()
{
return view('admin.inspector.test');
}
just return view while calling the action. dont send any data from the controller, just load the view,
when the view loads, fetch the db connection with pagination method. and render the pagination as below
<?php $articles = DB::connection('table_name')->where('if any condition')->get(); ?>
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}

laravel 4.1 fill table with data from data base

I'm trying to fill atable with data from my data base ive done that with laravel 4.2 but in 4.1 it won't work
the error is
Undefined variable: questions (View: C:\wamp\www\happy_Road\app\views\home\home.blade.php)
this is the view
<div class="col-md-12">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Thémathique</th>
<th>Question</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($questions as $question)
<tr>
<td>{{ $question->theme }}</td>
<td>{{ $question->question }}</td>
<td >
</td>
</tr>
#endforeach
</tbody>
<tfoot></tfoot>
</table>
{{ $questions->links(); }}
</div>
this is the controller
public function index()
{
if (Auth::check())
{
$questions = questionList::paginate(10);
return View::make('home.home')->with('user',Auth::user());
}
else
{
return Redirect::to('login')->with('message',"Vous devez vous connecter d'abord");
}
}
what is wrong with this method !! any help please ! thx :)
Pass questions through to your view
return View::make('home.home')
->with('questions', $questions)
->with('user', Auth::user());

Categories