Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2

心不动则不痛 提交于 2019-12-02 04:01:51

问题


I'm trying to get iot to show The items within an order and i keep getting this error

These are my models

class westcoorder extends Model
{
    protected $table = 'westcoorders';
    protected $with = 'westcoorderitem';

    protected $fillable = ['is_sent', 'is_delivered'];

    /**
    * @return \Illuminate\Database\Eloquent\Relations\HasMany
    */
    public function westcoorderitem()
    {
        return $this->hasMany('App\westcoorderitem');
    }
}

class westcoorderitem extends Model
{
    protected $table = 'westcoorderitems';

    protected $fillable = ['westcoorder_id','quantity', 'productName', 'productCode', 'price'];


    public function westcoorder()
    {
        return $this->belongsTo('App\westcoorder');
    }
}

This is my controller

public function onGoingOrder($orderNumber)
{
    $orderNumber = westcoorder::where('id', $orderNumber)->firstOrFail();

    $items = westcoorderitem::where('westcoorder_id', $orderNumber)->get();

    return view('westco.onGoingOrder', compact('orderNumber', 'items'));
}

And this is what i have in my view

<div class="panel-heading">Order @if ($orderNumber) {{ $orderNumber->id }} @endif Items</div>
        <div class="panel-body">
                @if($items)
                        {{ $items->productName }}
                @endif
        </div>

Here is what my tables looks like

Schema::create('westcoorders', function (Blueprint $table)
{
        $table->increments('id');
        $table->tinyInteger('is_sent')->default(0);
        $table->tinyInteger('is_delivered')->default(0);
        $table->timestamps();
} );

Schema::create('westcoorderitems', function (Blueprint $table)
{
        $table->increments('id');
        $table->Integer('westcoorder_id'); // fk for westcoOrder.id
        $table->string('quantity');
        $table->string('productName');
        $table->string('productCode');
        $table->decimal('price');
        $table->timestamps();
} );

And this is the error that I'm getting

Undefined property: Illuminate\Database\Eloquent\Collection::$productName

回答1:


Like your error states:

Undefined property: Illuminate\Database\Eloquent\Collection::$productName

You are trying to access a property on a Collection, instead of a Model. First, you can make use of the relationship you created, like so:

$order = App\westcoorder::where('id', $orderNumber)->with('westcoorderitem')->firstOrFail();

This will ensure the order items will be included with the result, instead of executing another query to fetch them.

You can then pass on the $order to the view:

return view('welcome', compact('orderNumber', 'order'));

(You can probably just leave out the orderNumber which was the actual order, as well)

Then you can access the order in your view and loop through the items like this:

@foreach($order->westcoorderitem as $item)
    {{ $item->productName }}
@endforeach

FK

Another tip could be to update your table to use indexes to improve performance and make it neat, like the FK you mention in the comment of your create migration. You can make a migration to update it, like:

$table->foreign('westcoorder_id')->references('id')->on('westcoorders');

And/or expand on this, according to your needs (cascading, etc).



来源:https://stackoverflow.com/questions/36143589/undefined-property-illuminate-database-eloquent-collection-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!