Php 如何使用tinker在laravel模型中找到反向关系
我上了这门课Php 如何使用tinker在laravel模型中找到反向关系,php,laravel,eloquent,laravel-5.3,Php,Laravel,Eloquent,Laravel 5.3,我上了这门课 namespace App; use Illuminate\Database\Eloquent\Model; class RetailerInfo extends Model { public function user() { //same as retailer() but more consistent return $this->belongsTo(User::class, 'retailer_id');
namespace App;
use Illuminate\Database\Eloquent\Model;
class RetailerInfo extends Model
{
public function user()
{
//same as retailer() but more consistent
return $this->belongsTo(User::class, 'retailer_id');
}
public function store()
{
return $this->belongsTo(Store::class);
}
}
找到属于该商店的零售商很好:
>>>namespace App;
>>>Store::find(2)->RetailerInfo
=> App\RetailerInfo {#1144
id: 1,
retailer_id: 65,
store_id: 2,
created_at: "2016-07-16 09:47:43",
updated_at: "2016-07-16 09:47:43",
}
但是查找存储所属的用户不起作用:
>>> RetailerInfo::find(1)->belongsTo(User::class)
=> Illuminate\Database\Eloquent\Relations\BelongsTo {#1176}
>>> RetailerInfo::find(1)->belongsTo(User::class, 'retailer_id')
如何使其工作?如前所述,只需在模型的方法中声明一次关系
你对这个例子做了什么
RetailerInfo::find(1)->belongsTo(User::class);
是不正确的,因为您没有在进行中声明关系
而是使用您在模型中已声明的关系(Store
与RetailerInfo
有关系,RetailerInfo
与用户有关系)
如上所述,您只需要在模型的方法中声明一次关系
你对这个例子做了什么
RetailerInfo::find(1)->belongsTo(User::class);
是不正确的,因为您没有在进行中声明关系
而是使用您在模型中已声明的关系(Store
与RetailerInfo
有关系,RetailerInfo
与用户有关系)
嗯。。你能告诉我更多吗?我提供参考资料什么的?嗯。。你能告诉我更多吗?ie提供引用还是什么?我们需要查看您的用户和存储类,看看您是如何声明反向关系的。这似乎是一对一!。我建议您查看这张雄辩的关系备忘单,然后在tinker is fine-Store::find(2)->RetailerInfo中为零售商重试您的呼叫。您对用户的调用使用不同的模式-RetailerInfo::find(1)->belongsTo(user::class)并尝试定义关系。你不应该用同样的方式来称呼它吗?i、 e RetailerInfo::find(1)->UserW需要查看您的用户和存储类,以查看如何声明反向关系。这似乎是一对一!。我建议您查看这张雄辩的关系备忘单,然后在tinker is fine-Store::find(2)->RetailerInfo中为零售商重试您的呼叫。您对用户的调用使用不同的模式-RetailerInfo::find(1)->belongsTo(user::class)并尝试定义关系。你不应该用同样的方式来称呼它吗?i、 电子零售信息::查找(1)->用户