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();
} );