1

我有三张桌子:

  1. collections其中有id,name
  2. genre_collection其中有id, genre_id,collection_id
  3. genres其中有id,name

我想从具有基因的集合中检索数据。

集合模型

class Collections extends Model{
    public function genres(){
        return $this->hasMany('App\Models\GenreCollectionRelationships', 'genre_id' , 'id');
    }
}

generic_collection

class GenreCollectionRelationships extends Model{
    public function genre(){
        return $this->hasOne('App\Models\Genres', 'id', 'genre_id');
    }
}

搜索控制器

class SearchController extends Controller{
    $collection->genres;
    foreach($collection->genres as $item){
        $item->genre;
    }
}

这段代码工作正常。输出是

实际的

"genres": [{
    "id": 1,
    "genre_id": 1,
    "collection_id": 1,
    "created_at": "2019-02-07 17:13:36",
    "updated_at": "2019-02-07 17:13:36",
    "genre": {
        "name": "Action",
        "meta": null
    }
}]

有什么办法可以直接得到如下所示的输出

预期的

"genres": [ {
    "name": "Action",
    "meta": null
}]

我尝试了 hasManyThrough,belongsToMany,但没有任何结果。

笔记。我在 laravel 5.7

提前致谢。

4

5 回答 5

0

我假设您想直接从 collection 访问 Generic 。如果是这种情况,您可以在集合模型中直接定义多对多关系到通用模型以访问它。请参考:https ://laravel.com/docs/5.7/eloquent-relationships#many-to-many 。对不起,如果我错了

于 2019-02-08T18:40:37.923 回答
0

我发现你的代码有点难以理解......让我试试看我是否理解正确......

你基本上有两个模型:

  • 模型Collection保存在表格中collections
  • 模型Genre保存在表格中genres

由于它们之间存在多对多关系,因此您需要第三个表将它们链接在一起。

按照命名约定,Laravel 期望你根据两个模型来命名它,按字母顺序排列。因此,要创建集合和流派之间的链接,您需要创建一个表,该表collection_genre有一个collection_id作为对集合表的引用,同样还有一个genre_id用于标识链接的流派。

然后,您可以按如下方式定义您的关系:

class Collection extends Model {
    public function genres() {
       $this->belongsToMany(\App\Models\Genre::class);
    }
}

class Genre extends Model {
    public function collections() {
       $this->belongsToMany(\App\Models\Collection::class);
    }
}

现在,我不确定您的控制器是什么样的,因为问题中有一些无效代码,但我怀疑您想搜索给定集合的流派。

您的代码可能像这样:

Class CollectionController extends Controller {

    function getGenres(Collection $collection) {
        return $collection->genres;
    }
}

这将返回给定集合的流派。如果你想格式化这个,你可以为此创建一个 Eloquent Resource:

Class CollectionResource extends Resource {
    public function toArray() {
        return [
           'name' => $this->name,
           'meta' => $this->meta
        ];
    }
}

在您的控制器中,您可以执行以下操作:

Class CollectionController extends Controller {

    function getGenres(Collection $collection) {
        return CollectionResource::collection($collection->genres);
    }
}
于 2019-02-07T18:46:42.633 回答
0

在您的收藏模型中

class Collections extends Model
{
    protected $table='collections';
    public $primaryKey='id';
    protected $fillable = ['name'];

    public function genres()
     {
            return $this->belongsToMany('App\Model\Genres','genre_collection','collection_id','genre_id')->withTimestamps();
     }

}

在你的流派模型中

class Genre extends Model {

  protected $table='genres';
  public $primaryKey='id';
  protected $fillable = ['name'];


    public function collections()
    {
        return $this->belongsToMany('App\Model\Collections','genre_collection','genre_id','collection_id')->get();
    }

}
于 2019-02-07T18:56:02.170 回答
0

您可以构建自己的查询来实现您正在寻找的内容。尝试这个:

$collection = Collection
        ::join('genres', 'genre.id', '=', 'collections.genre_id')
        ->select('collections.*', 'genres.name','genre.meta')
        ->get();
于 2019-02-07T18:14:49.960 回答
0

collections您正在创建和genre使用genre_collection数据透视表之间的多对多关系。在这种情况下,belongsToMany是合适的。而且您不需要任何用于genre_collection 表的模型。

集合模型

class Collections extends Model
{
    public function genres(){
        return $this->belongsToMany('App\Models\Genres', 'genre_collection', 'genre_id', 'collection_id');
    }
}

流派模型

class Genres extends Model
{

    public function collections(){
        return $this->belongsToMany('App\Models\Collections', 'genre_collection', 'collection_id', 'genre_id');
    }
}

搜索控制器

class SearchController extends Controller
{
    foreach($collection->genres as $item){
        $item->genre; // get genre info
    }
}
于 2019-02-07T18:58:22.290 回答