Skel
Skel

Reputation: 1667

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

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

Upvotes: 2

Views: 8738

Answers (1)

Francesco de Guytenaere
Francesco de Guytenaere

Reputation: 4833

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).

Upvotes: 4

Related Questions