Php 不按ID的拉维关系';s
我有两张这样的桌子: 作者表 -身份证 -书号 -名字 书桌 -身份证 -参考号 -作者 -头衔 books表上的ref与authors表上的book_id相同,但在我看来,这样做似乎无法获取authors书籍:Php 不按ID的拉维关系';s,php,laravel-4,eloquent,Php,Laravel 4,Eloquent,我有两张这样的桌子: 作者表 -身份证 -书号 -名字 书桌 -身份证 -参考号 -作者 -头衔 books表上的ref与authors表上的book_id相同,但在我看来,这样做似乎无法获取authors书籍: {{$author->books->first()->>title} 我的作者和书籍模型如下所示: class Authors extends \Eloquent { protected $guarded = ['id']; public $table = 'authors';
{{$author->books->first()->>title}
我的作者和书籍模型如下所示:
class Authors extends \Eloquent {
protected $guarded = ['id'];
public $table = 'authors';
public $timestamps = false;
public function books()
{
return $this->hasMany('Books', "author","name");
}
}
class Books extends \Eloquent {
protected $guarded = ['id'];
public $timestamps = false;
public function author()
{
return $this->hasOne('Authors');
}
public function categories(){
return $this->hasMany('Categories');
}
}
真的需要一些帮助 您可以在
hasMany
中传递外键和本地键,但看起来您将author
作为外键传递,name
作为本地键传递。外键应为ref
,本地键应为book\u id
class Authors extends \Eloquent {
protected $guarded = ['id'];
public $table = 'authors';
public $timestamps = false;
public function books()
{
return $this->hasMany('Books', "ref", "book_id");
}
}
class Books extends \Eloquent {
protected $guarded = ['id'];
public $timestamps = false;
public function author()
{
return $this->hasOne('Authors');
}
public function categories(){
return $this->hasMany('Categories');
}
}
谢谢,我想我把自己的领域弄得一团糟,非常感激。