Laravel eloquent relationship with 2 foreign keys - laravel-5

There are 2 tables and 2 models. Table "games" has 2 foreign keys "team_a_id" and "team_b_id"
I try to define relationship but can't understand how build this relationship. One game can have only one team A and only one team B. But one team can have many games.
At this moment I have
class Game extends Model
{
public function teamA()
{
return $this->hasOne('App\Models\Team', 'team_a_id');
}
public function teamB()
{
return $this->hasOne('App\Models\Team', 'team_b_id');
}
}
and
class Team extends Model
{
public function gameTeamA()
{
return $this->belongsTo('App\Models\GameSerie', 'team_a_id');
}
public function gameTeamB()
{
return $this->belongsTo('App\Models\GameSerie', 'team_b_id');
}
}
I have to define relationship in order to find all games where team was team_a or team_b.
e.g.
$allGames = $team->games($id);
Also i am not sure that i defined relationships right

It's the other way around. The model with the foreign key has the BelongsTo relationship:
class Game extends Model
{
public function teamA()
{
return $this->belongsTo('App\Models\Team', 'team_a_id');
}
public function teamB()
{
return $this->belongsTo('App\Models\Team', 'team_b_id');
}
}
class Team extends Model
{
public function gameTeamA()
{
return $this->hasOne('App\Models\GameSerie', 'team_a_id');
}
public function gameTeamB()
{
return $this->hasOne('App\Models\GameSerie', 'team_b_id');
}
}

Related

One to Many Relationship Over Pivot Table

I have existing data in the database, and I want to create a one-to-many relationship. How can I limit or simulate a one-to-many relationship when I'm using a pivot table?
I want to get the data from the pivot table, so I thought it could be done like this.
class Asset extends Model {
public function publisher() {
return $this->belongsTo(Publisher::class, 'asset_publisher');
}
}
-
class Publisher extends Model {
public function assets() {
return $this->hasMany(Asset::class, 'asset_publisher');
}
}
A possible way to simulate the one-to-many is creating a model for the pivot table "AssetPublisher" so you will now have
class Publisher extends Model {
public function assets() {
return $this->belongsToMany(Asset::class, 'asset_publisher');
}
}
class AssetPublisher extends Model {
public function publisher() {
return $this->belongsTo(Publisher::class);
}
}
class Asset extends Model {
public function assetPublisher() {
return $this->hasOne(AssetPublisher::class)->with('publisher');
}
public function getPublisherAttribute(){
return $this->assetPublisher->publisher;
}
}
You can now call $asset->publisher to give you the Publisher instance and $publisher->assets()->get() to get all assets instances

Relationship: belongsTo this model or a different model

We have the following
Manager
[id]
Companies
[id]
[manager_id] not nullable
Stores
[id]
[company_id]
[manager_id] *nullable*
I am looking for a single Eloquent relationship for the manager of every store.
Any suggestions?
Building on your comment #Sanjay S
class Store extends Model {
public function manager() {
return $this->belongsTo('App\Manager');
}
public function getManager() {
if (is_null($this->manager)) {
return $this->company()->manager;
} else {
return $this->manager;
}
Then when you call $store->getManager() you will get the company manager if store manager_id is null
You question is not clear as your relationship in the stores table is like manager_id [nullable] and you are asking for a single Eloquent relationship for the manager of every store. But from my guesses your Eloquent models should be look like this
class Manager extends Model {
public function stores() {
return $this->hasMany(App\Store::class);
}
public function company() {
return $this->hasOne(App\Company::class);
}
}
Company class
class Company extends Model {
public function manager() {
return $this->belongsTo(App\Manager::class);
}
}
Store class
class Store extends Model {
public function manager() {
return $this->belongsTo(App\Manager::class);
}
}
Hope this solution works for you.

HasManyThrough with polymorphic and many-to-many relations

In my Laravel application I have the following classes:
class Product extends Model
{
public function extended()
{
return $this->morphTo();
}
public function users {
return $this->belongsToMany('App\User', 'products_users', 'product_id');
}
}
class Foo extends Model
{
public function product()
{
return $this->morphOne('App\Product', 'extended');
}
public function bars()
{
return $this->hasMany('App\Bar');
}
}
class Bar extends Model
{
public function product()
{
return $this->morphOne('App\Product', 'extended');
}
public function foo()
{
return $this->belongsTo('App\Foo');
}
}
class User extends Model
{
public function products()
{
return $this->belongsToMany('App\Product', 'products_users', 'user_id');
}
}
I can easily get users of a bar object using Bar::find(1)->product->users and I can also get the bars of a user with User::find(1)->products.
How can I get the users of all bars belonging to a specific foo? That is, Foo::find(1)->users should return all users that have the bars belonging to Foo with id 1. It's basically hasManyThrough with polymorphic and many-to-many relations.
Try something like this:
public function users()
{
$Foo = static::with(['bars', 'bars.products', 'bars.product.users'])->get();
return collect(array_flatten(array_pluck($Foo, 'bars.*.product.users')));
}
This should work for you (code has been tested, but not against the exact same structure as your setup). The first line will return all the users deeply nested through the relationships. The second line will pull out the users, flatten them into an array, then transform the array into a collection.
I created a HasManyThrough relationship for cases like this: Repository on GitHub
After the installation, you can use it like this:
class Foo extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function users() {
return $this->hasManyDeep(
User::class,
[Bar::class, Product::class, 'products_users'],
[null, ['extended_type', 'extended_id']]
);
}
}

Laravel Eloquent Relationship Through Another Table

I have the following database tables:
Seasons
id
number
Teams
id
name
Standings
id
season_id
team_id
The question is, how could I get all of the teams in a season through the standings table. At the moment I am getting all of the teams this way:
$teams = [];
$standings = $season->standings;
foreach($standings as $standing){
$teams[] = $standing->team;
}
Is there a way I could do this using Eloquent relationships? I have tried HasManyThrough with no success. These are what my models look like currently:
class Season extends Eloquent{
public function standings(){
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent{
public function team(){
return $this->belongsTo('Team');
}
}
class Team extends Eloquent{
public function standings(){
return $this->belongsToMany('Standing');
}
}
Your relationships look a little off. Here is all the relationships you should need though only the belongsToMany ones are required for this specific scenario of finding all the teams in a season.
class Season extends Eloquent {
public function teams()
{
return $this->belongsToMany('Team', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Team extends Eloquent {
public function seasons()
{
return $this->belongsToMany('Season', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent {
public function team()
{
return $this->belongsTo('Team');
}
public function season()
{
return $this->belongsTo('Season');
}
}
You would use the belongsToMany relationship rather than a hasManyThrough to query all the teams in a season. That would look something like...
Season::with('teams')->find($season_id);
foreach($season->teams as $team) {
echo $team->name;
}

Laravel polymorphic relationship

I have different Database tables and below is the DB Diagram.
I am trying to make a many to many polymorphic relationship, but no luck, Please guide me that what i need to do, means where i need to add which type relationship/code?
Course Model
class Course extends Eloquent {
public function quizzes()
{
return $this->morphToMany('Quiz', 'quizzable');
}
}
Chapter Model
class Chapter extends Eloquent {
public function quizzes()
{
return $this->morphToMany('Quiz', 'quizzable');
}
}
Lesson Model
class Lesson extends Eloquent {
public function quizzes()
{
return $this->morphToMany('Quiz', 'quizzable');
}
}
Quiz Model
class Quiz extends Eloquent {
public function courses()
{
return $this->morphedByMany('Course', 'quizzable');
}
public function chapters()
{
return $this->morphedByMany('Chapter', 'quizzable');
}
public function lessons()
{
return $this->morphedByMany('Lesson', 'quizzable');
}
}
Source: http://laravel.com/docs/4.2/eloquent#many-to-many-polymorphic-relations
I would change the quizzes_assigned table to:
Table name: quizzables
quiz_id - integer
quizzable_id - integer
quizzable_type - string

Resources