Laravel 8 undefined variable - laravel

I am getting the following error.
ErrorException Undefined variable $brands (View:
C:\xampp\htdocs\wearwearemv\resources\views\backend\brand\brand_view.blade.php)
http://127.0.0.1:8000/brand/view
I could not figure out what is wrong here. Any help?
Here is my route
Route::get('/brand/view', [BrandController::class, 'BrandView'])->name('all.brands');
Controller function
public function BrandView()
{
$brands = Brand::latest()->get();
return view('backend.brand.brand_view', compact($brands));
}
And my forloop
<tbody>
#foreach($brands as $item)
<tr>
<td>{{ $item->brand_name }}</td>
<td><img src="{{ asset($item->brand_image) }}" style="width: 70px; height: 40px;"></td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</tbody>

The compact function expects strings with variable names:
return view('backend.brand.brand_view', compact('brands'));

Related

Laravel : "Object of class stdClass could not be converted to string" while pass variable into closure

I get an error like this
ErrorException
This my link index
<td>{{$cmface}}</td>
This my Routes
Route::get('/detail_face_to_face/{id}', 'FaceToFaceController#detail')->name('detail_face_to_face');
This my Controller
public function detail($id)
{
$xclass = DB::table('face_to_face')->select('Class')->where('id', $id)->first();
$tface = DB::table('tbclass')
->select('tbclass.F_Name','tbclass.Class')
->where('tbclass.Class', $xclass)
->whereNotExists(function ($query) use ($id) {
$query->select('id_user')
->from('absent')
->where([['absent.id_face_to_face', $id],['absent.type_face_to_face', '1'],])
->whereRaw('absent.id_user = tbclass.ID_No');
})
->orderBy('tbclass.F_Name', 'ASC')
->paginate(10);
return view('face_to_face.detail',['tface' => $tface]);
}
This my face_to_face.detail page
<table class="table">
<thead style="white-space:nowrap;">
<tr>
<th>No</th>
<th>Name</th>
<th>Class</th>
</tr>
</thead>
<tbody style="white-space:nowrap;">
#if($tface->count()===0)
<tr>
<td class="table-success text-center" colspan="10"><< Data is Empty >></td>
</tr>
#else
#foreach($tface as $no => $tm)
<tr>
<td>{{ ++$no + ($tface->currentPage()-1) * $tface->perPage() }}</td>
<td>{{$tm->F_Name}}</td>
<td>{{$tm->Class}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $tface->links() }}
if I click the link on the index page, i get that error
Can anyone help ???
Could you dd($xclass)?
Error is in, ->where('tbclass.Class', $xclass). You probably do ->where('tbclass.Class', $xclass->Class)
Regard

how can i check there is an variable laravel?

i have this on my view im trying to pass $reservations when there is some reservations in DB there is no error but when it is empty i got error
Undefined variable: reservations (View: C:\Users\yass\Desktop\E-tourisme-44\E-tourisme-5554\resources\views\moderateur\reservation.blade.php)
Template
<tbody>
#if($reservations )
#foreach($reservations as $reservation)
<tr>
<td> {{$reservation->children}} </td>
<td> {{$reservation->checkin}} </td>
<td> {{$reservation->checkout}} </td>
<td>
#endforeach
#else
<div>
No Data
</div>
#endif
</tbody>
and this is my function
public function index()
{
$myhotels = hotel::where('created_by', Auth::user()->id)->first();
if ($myhotels) {
$reservations = Reservation::where('hotel_id', $myhotels->id)->get();
return view('moderateur/reservation', compact('reservations'));
}
return view('moderateur/reservation');
}
To make your code work, instead
#if($reservations )
use
#if(! empty($reservations))
Also instead combining #if with #foreach for displaying "no data" consider using #forelse #empty instead. Docs: https://laravel.com/docs/7.x/blade#loops
Try with:
#if(isset($reservations))
You can use
#isset($reservations)
//your code
#endisset
Or you can write your code like this
{{ $reservations ?? null }}

Display list of purchase order base from filtered vendor in Laravel eloquent

I am trying to display the list of vendor with their purchase orders in a table.
VENDOR MODEL
public function vendorPo()
{
return $this->hasMany('App\PurchasedOrder', 'vendor_id','vendor_id');
}
VENDOR CONTROLLER
public function vendor()
{
$vendorPo = Vendor::with('vendorPo')
->get()
->keyBy('id')
->groupBy('vendor_name');
$purchaseOrders = PurchasedOrder::all()->where('publish','=','1');
return view('backend.purchase-order.vendor', compact('purchaseOrders'))
->with('vendorPo', $vendorPo);
}
for my index
#foreach ($vendorPo as $vendor => $vendor_list)
<tr>
<th colspan="7"
style="background-color: #F7F7F7"> {{ $vendor }}: ({{ $vendor_list->count() }} vendors)
</th>
</tr>
#foreach ($vendor_list as $key => $vendorPoList)
<tr>
<td>
{{ $vendorPoList->vendorPo->po_id}}
</td>
</tr>
#endforeach
#endforeach
if I do this
...
#foreach ($vendor_list as $key => $vendorPoList)
<tr>
<td>
{{ $vendorPoList}}
</td>
</tr>
#endforeach
...
the result is this
and if I
...
<td>
{{ $vendorPoList->vendorPO->po_id}}}
</td>
...
the result is error
Facade\Ignition\Exceptions\ViewException
Property [po_id] does not exist on this collection instance. (View:
Please help me. Thanks!
You are getting this issue because your vendorPO relation is a HasMany relation. If you pull back the records from that sort of relation, the results are always an array (even if there is only one record), which is always converted by Eloquent into a Collection.
Therefore, $vendorPoList->vendorPO is a Collection, and there is no variable on a Collection called po_id.
Depending on your data model, you should either revisit the relation, or do a #foreach on $vendorPoList->vendorPO.

Laravel - Undefined property: Illuminate\Database\MySqlConnection::$username

I wrote a query to pass parameter from view to controller, and I got this error:
Undefined property: Illuminate\Database\MySqlConnection::$username
View Passing the parameter
<td width="25%"><a class="btn btn-info" href="{{ route('gamesListDetail',$game->id) }}">{{ $game->name }}</a></td>
Receiving Controller
public function gamesListDetail($id = null)
{
$gamelists = DB::table("platform_games")
->select("platform_games.id", "platform_games.username","game_player.game_id")
->join("game_player","game_player.game_id","=","platform_games.id")
->where('platform_games.id',$id)
->take(5);
return view('soccerrave.games.gamesListDetail', compact('gamelists'));
}
Receiving View
<tbody>
#foreach($gamelists as $key => $gamelist)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $gamelist->username }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $gamelists->links() }}
</td>
</tr>
</tbody>
I expect the view to display top 5 data based on the parameter. But I got this error:
Undefined property: Illuminate\Database\MySqlConnection::$username
Documentation says:
skip / take
To limit the number of results returned from the query, or to skip a
given number of results in the query, you may use the skip and take
methods:
$users = DB::table('users')->skip(10)->take(5)->get();
So by example we see that after take method there must be get() call.
So fix Your code:
public function gamesListDetail($id = null)
{
$gamelists = DB::table("platform_games")
->select(
"platform_games.id",
"platform_games.username",
"game_player.game_id"
)
->join(
"game_player",
"game_player.game_id", "=", "platform_games.id"
)
->where('platform_games.id', $id)
->take(5)
->get(); // this one is required
return view('soccerrave.games.gamesListDetail', compact('gamelists'));
}

how to get data from laravel5.5?

I am using Laravel 5.5 and I want to view my data in database from my view page (home.blade.php)
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
In my home.blade.php I have an empty table and I want to show data from database fyp:
Route::get('home', function () {
$fyp = DB::table('reports')->get();
return view('home', ['fyp' => $fyp]);
});
This must work (as #Nazmul said):
#foreach($fyp as $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
just follow the code
Route::get('home', function () {
$home = DB::table('reports')->get();
return view('home', compact('home'));
});
After in your view page
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
I hope this will help you.

Resources