Reputation: 248
i want to get vendor_name and user who assign order to vendor in view. But every time i got this error
ErrorException in b6bb559eccdc8a2d45a2d2d6ce89e8e217411386.php line 26: Trying to get property of non-object (View: E:\xampp\htdocs\ftdindia\resources\views\view\Orders\allorder.blade.php) in b6bb559eccdc8a2d45a2d2d6ce89e8e217411386.php line 26 at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
my codes are given below
Controller for Order
public function allorder(){
$orders = OrderGrid::paginate(100);
return view ('view.Orders.allorder', ['orders' => $orders]);
//dd($orders->all());
}
Here is My Model
class OrderGrid extends model{
protected $table = 'order_grids';
public function vendor() {
return $this->belongsTo(User::class);
}
public function assignedBy() {
return $this->belongsTo(User::class, 'vendor_assigned_by_user');
}
}
Here is my view
@foreach($orders as $order)
<tr>
<td> {{ $i }}</td>
<td>{{ $order->order_id }}</td>
<td>{{ $order->vendor->username }}</td>
<td>{{ $order->assignedBy->username }}</td>
<td>{{ $order->delivery_city }}</td>
<td>{{ $order->delivery_date }}</td>
<td>{{ $order->delivery_time }}</td>
<td>{{ $order->order_statuss }}</td>
<td>{{ $order->order_status }}</td>
<td>
@if(!$order->vendor_id)
Unassigned
@else
Assigned
@endif
</td>
<td></td>
</tr>
<?php $i +=1; ?>
@endforeach
Upvotes: 1
Views: 236
Reputation: 248
@foreach($orders as $order)
<tr>
<td> {{ $i }}</td>
<td>{{ $order->order_id }}</td>
<td>{{ $order->vendor->username }}</td> inspite of this do <td>{{ !is_null($order->vendorName) ? $order->vendorName->username : null }}</td>
<td>{{ $order->assignedBy->username }}</td> and same here also <td>{{ !is_null($order->assignedBy) ? $order->assignedBy->username : null }}</td>
<td>{{ $order->delivery_city }}</td>
<td>{{ $order->delivery_date }}</td>
<td>{{ $order->delivery_time }}</td>
<td>{{ $order->order_statuss }}</td>
<td>{{ $order->order_status }}</td>
<td>
@if(!$order->vendor_id)
Unassigned
@else
Assigned
@endif
</td>
<td></td>
</tr>
<?php $i +=1; ?>
@endforeach
and in Model
public function vendorName() {
return $this->belongsTo('App\Model\User', 'vendor_id', 'id');
}
public function assignedBy() {
return $this->belongsTo('App\Model\User', 'assigned_by', 'id');
}
Upvotes: 1
Reputation: 6276
The error you are getting relates to how you are calling the vendor()
function in the $order
collection.
The docs are a good place for a solid understanding on this, but essentially all Eloquent relationships are defined via functions and so you may call those functions to obtain an instance of the relationship without actually executing the relationship queries.
As such it needs to be:
<td>{{ $order->vendor()->username }}</td>
Nothing that we are now chaining vendor() rather than vendor.
Upvotes: 0