Reputation: 716
I have following code
public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}
Where table vehicles
consists of:
spz
cust_id <FK> //this is foreign key to customer->id
type
brand
In the customers.detail
view, I tried to use following code to show data, but I get this error:
Undefined property: Illuminate\Database\PostgresConnection::$spz
Code:
@if (count($vehicles) > 0)
<?php $i = 1; ?>
@foreach ($vehicles as $vehicle)
<?php $i++; ?>
<td>{{$vehicle->spz}}</td>
<td>{{$vehicle->type}}</td>
<td>{{$vehicle->brand}}</td>
@endforeach
@endif
I have read this topic but seems it's not my problem because I use foreach
to iterate through the object but seems I do not get the object from database into my $vehicles
variable, because in the error page, it shows also something like this:
'customer' => object(Customer), 'vehicles' => object(Builder)
What makes me think that customer
gets its object correctly, but vehicles
gets Builder
?? Really no idea what is wrong there. Any ideas?
Just to describe what am I doing in the project that I work on, I have a customer detail page where I click a button to add a vehicle to his detail page (profile page) and I send customer id
as parameter to the function where I add vehicle into database, which works (vehicle is added correctly with the customer id
). Now that problem shows up when I want to show detail page with vehicle information like the code above shows.
Hope its clear enough. Thanks for suggestions.
Upvotes: 2
Views: 518
Reputation: 3009
Try to add ->get()
or ->paginate($YOUR_LIMIT_ONE_PAGE)
in your Controller
public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id)->get();
// ->where('cust_id', '=', $customer->id)->paginate($YOUR_LIMIT_ONE_PAGE);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}
And try to replace your foreach
to this forelse
@forelse ($vehicles as $index => $vehicle)
<tr>
<td>{{$index}}</td>
<td>{{($vehicle->spz !== null) ? $vehicle->spz : '-'}}</td>
<td>{{($vehicle->type !== null) ? $vehicle->type : '-'}}</td>
<td>{{($vehicle->brand !== null) ? $vehicle->brand : '-'}}</td>
</tr>
@empty
<td colspan='4'>Data not found</td>
@endforelse
Upvotes: 1