laravel get Data from table using model in laravel 5.2

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:xampphtdocsftdindiaresourcesviewsviewOrdersallorder.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

@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

并在模型中

public function vendorName() { 
        return $this->belongsTo('AppModelUser', 'vendor_id', 'id');
    } 

    public function assignedBy() { 
        return $this->belongsTo('AppModelUser', 'assigned_by', 'id');
    } 

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.

链接地址: http://www.djcxy.com/p/53084.html

上一篇: 为什么语法错误,意想不到的'endforeach'(T

下一篇: laravel使用laravel 5.2中的模型从表中获取数据