在join()方法中使用模型

时间:2019-04-05 14:41:41

标签: php laravel eloquent

是否可以使用join()中的模型名称作为参数而不是表名称。

例如数据库表名称为'SeriesGenres',但模型名称为'SerieGenre'

public function showContent($id){
        $serie = Serie::findOrFail($id);

        // SELECT Genres.name
        // FROM Genres INNER JOIN SeriesGenres ON Genres.id = SeriesGenres.Genre_id
        // WHERE SeriesGenres.Serie_id = $serie

        $getGenreNamesById = Genre::select('name')
                   ->join('SeriesGenres', 'SeriesGenres.Genre_id', '=', 'Genres.id')
                   ->whereIn('Serie_id',$serie)->get();
}

想做这样的事情:

$serieGenre =SerieGenre::all();
$getGenreNamesById = Genre::select('name')
                   ->join($serieGenre, 'Genre_id', '=', 'Genres.id')
                   ->whereIn('Serie_id',$serie)->get();

2 个答案:

答案 0 :(得分:0)

您可以尝试像这样访问表名:

(new SerieGenre)->getTable();

它将返回表的名称。

所以它就像:

$serieGenre =SerieGenre::all();
$getGenreNamesById = Genre::select('name')
                   ->join((new SerieGenre)->getTable(), 'Genre_id', '=', 'Genres.id')
                   ->whereIn('Serie_id',$serie)->get();

答案 1 :(得分:0)

您正在使用QueryBuilder而不是Eloquent。在Serie模型文件中,应该定义一个函数来访问Genre关系。

据我从您的代码中了解,您在流派和意甲之间建立了多对多关系。

我假设SerieGenre的主键都被命名为id,而SerieGenre中的外键被命名为Genre_id和{{ 1}},分别根据您的代码

Serie_id

如果要从数据透视表访问Serie或Genre模型,则还应该定义以下函数。

// App\Serie.php

public function series_genres()
{
    // hasMany($related, $foreign_key, $local key)
    // if you don't specify the $foreign_key, Laravel will assume it's named 'serie_id' based on its conventions.
    // if you don't specify the $local_key, Laravel will assume it's named 'id' which is the case so we don't need to specify
    return $this->hasMany('App\SerieGenre', 'Serie_id');
}

public function genres()
{
    // belongsToMany($related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relation)
    // if you don't specify the $table, Laravel will assume it's named 'serie_genre' based on its conventions.
    // if you don't specify the $foreignPivotKey, Laravel will assume it's named 'serie_id' based on its conventions.
    // if you don't specify the $relatedPivotKey, Laravel will assume it's named 'genre_id' based on its conventions.
    // if you don't specify the $parentKey, Laravel will assume it's named 'id' which is the case so we don't need to specify
    // if you don't specify the $local_key, Laravel will assume it's named 'id' which is the case so we don't need to specify
    // You don't need to worry about the $related variable
    return $this->belongsToMany('App\SerieGenre','SeriesGenres', 'Serie_id', 'Genre_id');
}

最后,如果您想从流派模型访问Serie关系,那是同一回事。

// App\SerieGenre.php

public function serie()
{
    // belongsTo($related, $foreignKey, $ownerKey, $relation)
    // if you don't specify the $foreign_key, Laravel will assume it's named 'serie_id' based on its conventions.
    // if you don't specify the $local_key, Laravel will assume it's named 'id' which is the case so we don't need to specify
    // you don't need to worry about the $relation variable
    return $this->belongsTo('App\Serie', 'Serie_id');
}

public function genre()
{
    return $this->belongsTo('App\Genre', 'Genre_id');
}

通常,您不需要将多个参数传递给关系函数,但这只有在遵循laravel的编写方式(模型的单数StudlyCase,表名的复数snake_case,id作为主键以及snake_case'd)的情况下才是正确的。型号名称+ _id(外键)

最后,您需要问自己是否只需要关系的详细信息,或者是否还需要模型本身。

在此示例中,id = 2的Serie与id = 4的流派相关(还有其他) // App\Genre.php public function series_genres() { return $this->hasMany('App\SerieGenre', 'Genre_id'); } public function series() { return $this->belongsToMany('App\SerieGenre','SeriesGenres', 'Genre_id', 'Serie_id'); } 将产生以下对象

Serie::find(2)->genres()

Illuminate\Database\Eloquent\Collection { all: [ App\Genre { id: 4, name: 'name', ..., pivot: Illuminate\Database\Eloquent\Relations\Pivot { Genre_id: 4, Serie_id: 2, ..., }, }, App\Genre {...}, App\Genre {...}, ], } 将产生以下对象

Serie::with('genres')->find(2)

我建议您阅读文档,因为在那里可以更好地说明此主题。