Laravel Blade not showing data from controller - php

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

Related

Passing variable from index to controller using Laravel

I am new to using Laravel and I am currently trying to pass a variable from the index blade file to the controller in order to bring up all apparatus type fields associated with a specific apparatus code id.
I am able to access the apparatus type fields associated with the apparatus code id when I include a specific number which correlates to that apparatus code id (eg. inputting 2 brings up the apparatus type details for the apparatus code id 2, inputting 3 brings up the apparatus type details for the apparatus code id 3 etc).
However when I have tried to pass a variable relating to each individual apparatus code id using a for loop in the controller, the apparatus type loop appears to not be accessed and does not return any information. I will include snippets of my current code for clarity.
Any help or guidance with this issue would be greatly appreciated!
Controller file
public function index()
{
$apparatusCodes = ApparatusCodes::get();
foreach ($apparatusCodes as $apparatusCode){
$apparatusTypes = ApparatusTypes::where('apparatus_code_id', $apparatusCode->id)->get();
}
return view('apparatus_codes.index', compact('apparatusCodes', 'apparatusTypes'));
}
Index.blade file
<table id="datatable" class="stripe hover dt-responsive display nowrap" style="width:100%; padding-top: 1em; padding-bottom: 1em;">
<thead>
<tr>
<th>ID</th>
<th >Apparatus Code</th>
<th>Description</th>
<th>Rent</th>
<th></th>
</tr>
</thead>
<!--start of for loop-->
#foreach ($apparatusCodes as $apparatusCode)
<tbody>
<tr data-toggle="collapse" id="table{{ $apparatusCode->id}}" data-target=".table{{ $apparatusCode->id}}">
<td> {{ $apparatusCode->id}} </td>
<td>{{ $apparatusCode->apparatus_code}} </td>
<td> {{ $apparatusCode->description}}</td>
<td> {{ $apparatusCode->rent}}</td>
<td #click="isOpen = !isOpen" class="main-bg"><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/Icon-arrow-dropdown-white.png" alt="arrow down">
</td>
<td><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/edit-icon.svg" alt="Edit Icon">
</td>
</tr>
<tr x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak #click.away="isOpen = false" class="collapse table{{ $apparatusCode->id}}">
<td colspan="999">
<div>
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th>Apparatus Code </th>
<th>Apparatus Type</th>
<th>Compensation </th>
<th>Created On </th>
<th>Created By </th>
<th></th>
#foreach ($apparatusTypes as $apparatusType)
</thead>
<tbody>
<tr>
<td>{{ $apparatusCode->apparatus_code}}</td>
<td>{{ $apparatusType->apparatus_type }}</td>
<td>{{ $apparatusType->compensation }}</td>
<td>{{ $apparatusType->created_at }}</td>
<td>{{ $apparatusType->users->name}}</td>
<td> <button type="button" class="btn btn-default btn-edit js-edit"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="/../../img/Icon-edit-gray.png" alt="edit"></button></td>
</tr>
#endforeach
</table>
</tr>
</tr>
</td>
</tbody>
#endforeach
</table>
If you have the relationships setup you could eager load the ApparatusTypes for each ApparatusCode:
$apparatusCodes = ApparatusCodes::with('appartusTypes')->get();
Then in the view you could iterate the appartusTypes of each ApparatusCode:
#foreach ($apparatusCodes as $apparatusCode)
...
#foreach ($apparatusCode->apparatusTypes as $type)
...
#endforeach
...
#endforeach
Without using the relationships you could get all the ApparatusTypes for the ApparatusCodes and then group them by apparatus_code_id:
$codes = ApparatusCodes::get();
$types = ApparatusTypes::whereIn('apparatus_code_id', $codes->pluck('id'))
->get()
->groupBy('apparatus_code_id');
Then in the view you could iterate the group that corresponds to the current Code's apparatus_code_id:
#foreach ($codes as $code)
...
#foreach ($types->get($code->id, []) as $type)
...
#endforeach
...
#endforeach
Laravel 8.x Docs - Eloquent - Relationships - Eeager Loading with
Laravel 8.x Docs - Collections - Available Methods - pluck
Laravel 8.x Docs - Collections - Available Methods - groupBy

Displaying data from db in table form laravel blade

I tried to display all menus from the db in table form however i got an error saying that "undefined variable:menus(0)"
dashboard_index.blade.php
<div class= "menu-content">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Category Code</th>
<th scope="col">Menu Title</th>
<th scope="col">Menu Price</th>
</tr>
</thead>
<tbody>
#foreach ($menus as $menu)
<tr>
<td>{{ $menu->id }}</td>
<td>{{ $menu->category_code }}</td>
<td>{{ $menu->menu_title }}</td>
<td>RM{{ $menu->menu_price }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
web.php
Route::get('/dashboard', 'AdminMenuController#index');
AdminMenuController.php
public function index()
{
$menus = \App\Menu::all();
return view('admin.dashboard_index',
[
'menus'=>$menus,
]);
}
You have a syntax error in $menu = \App\Menu::all()
it should be $menus = \App\Menu::all()

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 - ErrorException Undefined variable

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.

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() !!}

Categories