我不断收到此错误试图获取非对象的属性。
这是我的控制器
public function onGoingOrder($orderNumber)
{
$orderNumber = westcoorder::where('id', $orderNumber)->firstOrFail();
$items = westcoorderitem::where('westcoorder_id', $orderNumber)->first();
return view('westco.onGoingOrder', compact('orderNumber', 'items'));
}
我认为这就是
<div class="panel-heading">Order {{ $orderNumber->id }} Items</div>
<div class="panel-body">
@foreach ($items->westcoorderitems as $item)
<li>{{ $item }}</li>
@endforeach
</div>
</div>
这是我的两个模特
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');
}
}
当我尝试使用
Order_id
列出订单中的所有项目时,我一直收到该错误这是我的桌子的样子
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();
} );
最佳答案
在Laravel的最新版本中,很多事情在后台发生了变化。对于发行版> = 5,模型不会自动加载到您的应用程序中。在控制器中,名称空间{yournamespace}下;语句,使用use语句以这种方式引用所需的类:
use App\westcoorders
只是有关约定的建议:遵守约定始终是一种好习惯。您的模型应称为WestCoorder和WestCoorderItem(请注意类的CamelCase命名)。这个概念也对您的控制器有效(WestCoordersController和WestCoorderItemsController)