1

I keep getting this error Trying to get property of non-object.

This is my controller

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

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

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

This is what is in my view

<div class="panel-heading">Order {{ $orderNumber->id }} Items</div>
        <div class="panel-body">
            @foreach ($items->westcoorderitems as $item)
                <li>{{ $item }}</li>
            @endforeach

        </div>
    </div>

This is my two models

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

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

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

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

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


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

And i keep getting that error when i trying to list all the items within the order with the Order_id

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();
} );
Aakash Goplani
  • 1,150
  • 1
  • 19
  • 36
Skel
  • 1,611
  • 3
  • 16
  • 36
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – RiggsFolly Mar 20 '16 at 18:00

2 Answers2

0

In the last releases of Laravel a lot of things changed under the hood. With the release >= 5, Models are not loaded automatically in your application. In your controller, under the namespace {yournamespace}; statement, reference the classes you need with the use statement this way:

use App\westcoorders

Just an advice about the conventions: the respect of the conventions is always a good practice. Your models should be called WestCoorder and WestCoorderItem (notice the CamelCase naming of the classes). This concept is valid for your controller too (WestCoordersController and WestCoorderItemsController)

Andrea Alhena
  • 986
  • 6
  • 9
  • I've have already got this `use App\westcoorder;` in my controller and i still get the error – Skel Mar 20 '16 at 23:11
  • Ill also start using CamelCase So i should have it like this `WestcoOrderItems` and `WestcoOrder` – Skel Mar 20 '16 at 23:14
0

First of all, you should check if $orderNumber does exist before you use it:

@if ($orderNumber)
    {{ $orderNumber->id }}
@endif

Next, you're trying to iterate $items->westcoorderitems, but:

  • It's not a collection, since you're using first() method. It returns one instance, so you shouldn't iterate it.
  • I can't see westcoorderitems column in your westcoorderitems table, so you shouldn't try to use $items->westcoorderitems here.
  • You should also check if $items is not empty, before you use it.

So, your code should look something like this:

<div class="panel-heading">Order @if ($orderNumber) {{ $orderNumber->id }} @endif Items</div>
    <div class="panel-body">
        @if ($items)
            {{ $items->productName }}
        @endif
    </div>
</div>
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I did have it like that but i was getting this as an error `Undefined property: Illuminate\Database\Eloquent\Builder::$productName` and i still am?? – Skel Mar 21 '16 at 17:55
  • That means your query doesn't returns any results. Try to use `dd($items)` before `{{ $items->productName }}` to know if it has `productName`. – Alexey Mezenin Mar 22 '16 at 02:00
  • So I'd its returning no results. That mean the relationship between the two databases is not working – Skel Mar 22 '16 at 08:52