Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不按ID的拉维关系';s_Php_Laravel 4_Eloquent - Fatal编程技术网

Php 不按ID的拉维关系';s

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';

我有两张这样的桌子:

作者表 -身份证 -书号 -名字

书桌 -身份证 -参考号 -作者 -头衔

books表上的ref与authors表上的book_id相同,但在我看来,这样做似乎无法获取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');
    }

}

谢谢,我想我把自己的领域弄得一团糟,非常感激。