![]() ![]() Usually you do not need to pass more than one parameter to the relationship functions but that's true only if you follow laravel's way of writing things (singular Studl圜ase for Models, plural snake_case for table names, id as primary key and snake_case'd model name _id for foreign keys)įinally, you need to ask yourself if you want only the relationship's detail or if you also need the model itself. Return $this->belongsToMany('App\SerieGenre','SeriesGenres', 'Genre_id', 'Serie_id') Laravel eloquent or query builder join method do the same as MySQL inner join function. For example if you have 10 rows and you want the join from other table then it will only return the rows which satisfy both the tables. Return $this->hasMany('App\SerieGenre', 'Genre_id') laravel eloquent Join method by default use inner join. Return $this->belongsTo('App\Genre', 'Genre_id') Īnd finally, if you want to access the Serie relationship from the Genre model, it's the same thing. Return $this->belongsTo('App\Serie', 'Serie_id') you don't need to worry about the $relation variable The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. To perform a basic 'inner join', you may use the join method on a query builder instance. belongsTo($related, $foreignKey, $ownerKey, $relation) The query builder may also be used to add join clauses to your queries. If you want to access the Serie or the Genre model from the pivot table, you should define the following functions as well. Return $this->belongsToMany('App\SerieGenre','SeriesGenres', 'Serie_id', 'Genre_id') You don't need to worry about the $related variable 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 $relatedPivotKey, Laravel will assume it's named 'genre_id' 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 $table, Laravel will assume it's named 'serie_genre' based on its conventions. belongsToMany($related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relation) Return $this->hasMany('App\SerieGenre', 'Serie_id') 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 if you don't specify the $foreign_key, Laravel will assume it's named 'serie_id' based on its conventions. hasMany($related, $foreign_key, $local key) I'm assuming the primary keys for Serie and Genre are both named id and the foreign keys in SerieGenre are named Genre_id and Serie_id respectively based on your code // App\Serie.php In your Serie model file, you should define a function to access the Genre relationship.įrom what I understand from your code, You've got a Many-to-Many relationship between Genre and Serie. What you're doing is using QueryBuilder instead of Eloquent. ![]()
0 Comments
Leave a Reply. |