Displaying data from db in table form laravel blade - php

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

Related

Class "App\Http\Controllers\User" not found [duplicate]

This question already has answers here:
Class "App\Http\Controllers\Auth\User" not found
(2 answers)
Closed 1 year ago.
So I was fetching data from my database to print in a table however, it says that
Class "App\Http\Controllers\User" not found.
Here is the controller and here is how I will print the data
public function directory()
{
$dashboardTitle = "Directory";
$isCurrent = "Directory";
$users = User::all();
return view('dashboard.directory', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent,
'users' => $users
]);
}
table to print at
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th> Picture </th>
<th>Name</th>
<th>Email</th>
<th>User type</th>
<th>Birthdate</th>
<th>Contact number</th>
<th>Created time</th>
</tr>
#foreach($users as $user)
<tr>
<td>{{ $user->profile_image }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->user_type }}</td>
<td>{{ $user->bdate }}</td>
<td>{{ $user->contact_no}}</td>
<td>{{ $user->created_at }}</td>
</tr>
#endforeach
</thead>
<tfoot>
</tbody>
</table>
At the top off your controller add
Laravel 8+
use App\Models\User;
Laravel <=7
use App\User;

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'] }}

'Undefined variable: clients (View: C:\wamp\www\myLSF\resources\views\back\clients\index.blade.php)'

I'm creating a index.blade.php in view/back/client to list the clients.but I'm having this error.
Undefined variable: clients (View:
C:\wamp\www\myLSF\resources\views\back\clients\index.blade.php)
here is my index.blade.php
<table id="example1" class="table table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name </th>
<th>Last Name</th>
<th>Address</th>
<th>Postal Code</th>
<th>City</th>
<th>Province</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
#foreach($clients as $client)
<tr>
<td>{{ $client->id }} </td>
<td>{{ $client->firstname }}</td>
<td>{{ $client->lastname }}</td>
<td>{{ $client->address }}</td>
<td>{{ $client->postalcode }}</td>
<td>{{ $client->city }}</td>
<td>{{ $client->province }}</td>
<td>{{ $client->phoneno }}</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>First Name </th>
<th>Last Name</th>
<th>Address</th>
<th>Postal Code</th>
<th>City</th>
<th>Province</th>
<th>Phone</th>
</tr>
</tfoot>
</table>
here is my clientcontroller.php
class clientController extends Controller
{
public function list()
{
$clients = client::get();
return view('/back/clients/index', compact('clients'));
}
}
here is my client.php model
class client extends Model
{
use Notifiable;
#var
protected $table='clients';
protected $fillable=
['firstname','lastname','address','postalcode',
'city','province','phoneno'];
}
here i set the route for this in web.php
Route::name('clients')->get('clients', 'clientController#list');
You need to import your client model into the clientController.php file.
To do this, add use App\client at the top, like so:
use App\client;
class clientController extends Controller
{
public function list()
{
$clients = client::get();
return view('/back/clients/index', compact('clients'));
}
}

Undifined Variable : employess

i want to show image gallery at user page
this my code from ImageUserController
function list(){
$employees['img_user'] = ImageUser::where('user_id', Auth::user()->id)->get();
return view('ukm.index', $employees);
}
and this my code from UserContoller
public function list()
{
$employees = ImageUser::all();
$ukm = Ukm::all();
return view('/ukm/index', compact( 'employees', 'ukm'));
}
this my route
Route::get('/ukm/index', 'ImageUserController#list');
and this my code at ukm/index.blade.php
table class="table table-stripped table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Image</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
#foreach ($employees as $employee)
<tr>
<th scope="row">{{ $employee->id }}</th>
<td>{{ $employee->name }}</td>
<td>{{ $employee->description }}</td>
<td><img src="{{ asset('uploads/employee/' . $employee->image) }}" width="150px;" height="100px;" alt="Image"></td>
<td>Edit</td>
<td>Delete</td>
</tr>
#endforeach
</tbody>
</table>
i'm getting this error : Undefined variable: employees
You aren't passing the array to the view actually
return view('ukm.index')->with('employees', $employees);
In your ImageUserController list() method,
function list(){
$employees = ImageUser::where('user_id', Auth::user()->id)->get();
return view('ukm.index', compact('employees'));
}

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