Laravel 5 Hasmany Relationship on Two Columns

Laravel 5 hasMany relationship on two columns

I don't think it's possible to do exactly what you are asking.

I think you should treat them as separate relationships and then create a new method on the model to retrieve a collection of both.

public function userRelations() {
return $this->hasMany('App\UserRelation');
}

public function relatedUserRelations() {
return $this->hasMany('App\UserRelation', 'related_user_id');
}

public function allUserRelations() {
return $this->userRelations->merge($this->relatedUserRelations);
}

This way you still get the benefit of eager loading and relationship caching on the model.

$cause = Cause::with('donations.user.userRelations', 
'donations.user.relatedUserRelations')
->where('active', 1)->first();

$userRelations = $cause->donations[0]->user->allUserRelations();

Laravel Eloquent relationship - multiple columns of table reference same foreign key

Without changing the database structure, you could misuse a hasMany declaration to get all 4 players.

class Game extends Model
{
public function players()
{
return $this->hasMany(Player::class, 'id', 'player1_id')
->orWhere('id', $this->player2_id)
->orWhere('id', $this->player3_id)
->orWhere('id', $this->player4_id);
}
}
class Player extends Model
{
public function games()
{
return $this->hasMany(Game::class, 'player1_id', 'id')
->orWhere('player2_id', $this->id)
->orWhere('player3_id', $this->id)
->orWhere('player4_id', $this->id);
}
}

However that is not ideal.

You should have a third table to properly map this many to many relationship.

table 1 - players:     id (pk), name
table 2 - games: id (pk)
table 3 - game_player: id (pk), game_id (fk), player_id (fk), unique([game_id, player_id])
class Game extends Model
{
public function players()
{
return $this->belongsToMany(Player::class, 'game_player');
}
}
class Player extends Model
{
public function games()
{
return $this->belongsToMany(Game::class, 'game_player');
}
}

Laravel hasMany relation on multiple columns

First, you need to define 2 relations in your team model:

public function teamleader1() {
return $this->belongsTo(User::class, 'Teamleader_1', 'User_id');
}

public function teamleader2() {
return $this->belongsTo(User::class, 'Teamleader_2', 'User_id');
}

Once you have it, define an accessor that will get the teamleader from either of those relations:

public function getTeamleaderAttribute() {
return $this->teamleader1 ?: $this->teamleader2;
}

Once you have it, you should be able to access the user by calling:

$teamLeader = $team->teamleader;

Lumen 5.4 hasMany relationship on two columns to same model

I think in your case it's not important to return a hasMany object because it would be impossible to add. I think there is no solution because a 1 to n relationship needs one field for the foreign key and not an or-statement.

If you just want to return the battles in an array you can do this:

public function battles() {
return App\Battle::where('player1_id', $this->id)->orWhere('player2_id', $this->id)->get();
}

In this case you just have one query. Otherwise you can fire up 2 has many queries where you link first to the one id, than to the other one and then merge them, but I think the solution above is the easiest one.

If you want to work with has many you can try this (maybe it does not work because the foreign key is not set in the db)

public function battlesChallenger() {
return $this->hasMany('App\Battle', 'player1_id');
}

public function battlesChallenged() {
return $this->hasMany('App\Battle', 'player2_id');
}

public function battles() {
return $this->battlesChallenger()->merge($this->battlesChallengered());
}

Laravel Relationship One to two columns

Maybe you can try this:

class Team extends Model
{
protected $table = 'teams';

public function gameOwner()
{
return $this->hasMany('App\Games', 'team_1');
}

public function gameVisitor()
{
return $this->hasMany('App\Games', 'team_2');
}

public function games() {
return $this->gameOwner->merge($this->gameVisitor);
}

}

class Game extends Model
{
public function teamOvner() {
return $this->belongsTo('App\Team', 'team_1');
}

public function teamVisitor() {
return $this->belongsTo('App\Team', 'team_2');
}

public function teams() {
return $this->teamOvner->merge($this->teamVisitor);
}
}

laravel 5 reletionship hasMany on two columns

The error says Colomn not found, the column name you are trying to 'call' is called 'tabel_name'. Are you sure the column name isn't 'table_name'? (table in english and not dutch, atleast I think you are dutch)

So it should be:

public function Attachments()
{
return $this->hasMany('App\Attachments','record_id','id')->where('table_name','`battery_assets`');
}

If your column is called 'tabel_name' in your database. I would recommend using English naming, since 'name' is English.

How we get specific columns from multiple relations using With() in Laravel

to make Laravel able to load the relation, you should select the foreign key that responsible for that relation

Questions::with(array(
'questionCategory' => function ($query) {
$query->select('cat_id', 'cat_value');
},
'ans_options' => function ($query1) {
$query1->select(
'ac_id',
'ac_choiceTxt',
'ac_quest_id'
);
}
))->get();

just add 'ac_quest_id' to your select.

Get data with relation using two columns in Laravel

First create Attachment model php artisan make:model Attachment

Create a hasMany relationship to 'Attachment' at the 'Users' model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
public function attachments()
{
return $this->hasMany(Attachment::class);
}
}

Then you can query like

$data = Users::with(['attachments' => function ($query) {
$query->where('type', 7);
}])->find($id);


Related Topics



Leave a reply



Submit