Converting objects to arrays - laravel

I'm trying to paginate csv file and I succeeded with it's data, but I couldn't with the file's header(keys). The error says array_keys expected() to be array but objects given, so I wanna to convert array_keys() to array.
I tried to use toArray() function but useless:
public function indexPagination()
{
$products = Product::all();
$products = $products->toArray();
$products = Product::paginate(5);
return view ('inventory.layout', ['products'=>$products]);
}
The error commes from this section who is gonna shows the file's keys, but error says array_keys() expecting to be array but objects given.... please help
<thead>
#if ($arrkeys= array_keys($products[0]))
#foreach ($arrkeys as $keys)
<th>{{$key}}</th>
#endforeach
#endif
</thead>

It is not very clear exactly what your desired result is. It looks like you are trying to paginate over the table headers. If this is correct, the following might suffice:
Controller
return view ('inventory.layout', [
'products' => Product::paginate(5),
]);
View
<table>
<thead>
#foreach ($products as $product)
<th>{{ $product->name }}</th>
#endforeach
</thead>
</table>
{{ $products->links() }}

It worked well now through convertin the parameter of array_keys() to array using toArray() function because it was an object before. this is the answer:
#if ($arrkeys= array_keys(($products[0])->toArray()))
#foreach ($arrkeys as $key)
<th>{{$key}}</th>
#endforeach
#endif

Related

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.

Want to Join three table but not work [Eloquent]

I want to
SELECT events.id = scan.event_id IF Scan.student_id MATCH user.student_id SHOW events.id and events.name
But my code dint work, it blank screen nothing happen to show
so blind :|
Controller
public function pdftranscript($id)
{
$users = DB::table('users')
->join('scan', 'users.student_id', '=', 'scan.student_id')
->join('events', 'scan.event_id', '=', 'events.id')
->select('users.*', 'events.m_event_name', 'scan.event_id')
->get();
This blade
#foreach($user as $user)
<tr>
<td>{{$user->student_id}}</td>
<td>{{$user->name}}</td>
#foreach($event as $event)
<td>{{$event->m_event_name}}</td>
#endforeach
<tr>
#endforeach
First you could dd($users) in your controller to check whether you have got a successful query. Then in blade, it should be:
#foreach($users as $user)
<tr>
<td>{{$user->student_id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->m_event_name}}</td>
<tr>
#endforeach
You already took the values in $users variable..No need for the second foreach loop.
#foreach($users as $user)
<tr>
<td>{{$user->student_id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->event_id}}</td>
<td>{{$user->m_event_name}}</td>
<tr>
#endforeach

Display multiple variable data separately from multiple tables on single view in blade template in laravel

I want to display two variable data on same view in a foreach loop
public function getWalletList()
{
$data = Data::orderBy('id','desc')->get()->toArray();
$user_ids = array_column($data, 'id');
$user_data = User::whereIn('id',$user_ids)->get()->toArray();
$d= array('data'=>$data,'user'=>$user_data);
return view('wallet_list', compact('d'));
}
So how to diplay data on laravel blade template
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach
why dont you use two foreach ?
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach
#foreach($b as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
<tr>
#endforeach

Trying to pass data from controller to view in laravel 5

I am trying to pass 2 different columns from my sql table to my view ,so that i can show them.
My controller is:
$interface = DB::table("users")->distinct()->get(['name']);
$interfac = DB::table("users")->distinct()->get(['email']);
view()->share('users',compact('interface','interfac'));
My view is :
#foreach ($users as $key => $value)
<tr>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
But this is not working while passing the two variables.If i pass one it is working but passing two is not working.thanks.
Controller
$users = DB::table('users')->get();
return view('users', compact('users'));

Resources