[英]laravel 5 - Eloquent relations
I have a table a migration called plates . 我有一张桌子, 叫板 。
$table->increments('id');
$table->integer('supplier_id')->unsigned();
$table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('cascade')->onUpdate('cascade');
And a model called Plates 还有一个叫Plates的模型
public function supplier()
{
return $this->belongsTo('App\Models\Suppliers','supplier_id');
}
And a model Suppliers model 和供应商模型
public function plates()
{
return $this->hasOne('App\Models\Plates');
}
when I check the relations with in the route doing this kind of thing. 当我检查与路线做这种事情的关系时。
$storage = Suppliers::find(1);
$att = $storage->plates;
dd($att);
it just does not work. 它只是不起作用。 But the other way around works just fine.
但是反过来工作也很好。 I am not sure what I am doing wrong.
我不确定自己在做什么错。 I have checked the docs and did countless other tries, but I can't understand it anymore.
我已经检查了文档并进行了无数次其他尝试,但是我再也听不懂了。
You must add the parameter in the plates function at Supplier Model, you created the model in plural, Suppliers, so it tries to find a key suppliers_id 您必须在“供应商模型”的plates函数中添加参数,并以复数形式创建“供应商”模型,以便其尝试查找关键的Suppliers_id
public function plates()
{
return $this->hasOne('App\Models\Plates', 'supplier_id');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.