1

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
3
  • which one is line -26 in your view?? Commented Jul 14, 2016 at 6:43
  • 26 line in view <td>{{ $order->vendor->username }}</td> Commented Jul 14, 2016 at 6:50
  • try changing method name "vendor()" to something else "order_vendor()" in your model. and then use it in view like - $order->order_vendor->username. based on this link - laravel.io/forum/… Commented Jul 14, 2016 at 7:11

2 Answers 2

1
@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');
    } 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

not working..... i had made some changes but this return only 1..... here are the changes... {{ !is_null($order->assignedBy ? $order->assignedBy->username: null) }}
again you are doing the same mistake try assignedBy() instead of assignedBy.
hmmm thanks for that.... i've got the solution actually you are that letting me know there is issue in view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.