I have many to many relationship between an actor and a theater_play.
How to add his stage name when making the connection? I have a table for the actor, a table for theater_play and a table for the connection of many to many call actor_theater_play where I connect the IDs.
My goal is to display John Doe is know for his roles: and then to list Name of Play - Character Name.
In your theatre play relation on your Actor model, make sure you use the ->withPivot() method to specify which pivot fields you want attached with the relation:
public function theaterPlays() {
return hasMany(\App\TheaterPlay::class)->withPivot('stage_name');
}
You will then able able to access in your list (Laravel blade example):
#foreach($actor->theater_plays as $theater_play)
{{ $theater_play->name . ' - ' . $theater_play->stage_name }}
#endforeach
Related
I have two additional column for login with different tables. So, I used 3 tables for login with output like this:
When I want to use username its simply write it like this {{ Auth::user()->username }} so I get value KASIR WEB. But it not work with kd_lokasi and kd_shift. Where I should register it? Or how I can get kd_lokasi and kd_shift value and paste it on view? Thank you for helping me.
Table :
$kd_lokasi = DB::table("tb_kasir_lokasi")->where("vTampil", 1)->get()
->pluck("nm_lokasi", "kd_lokasi");
$kd_shift = DB::table("tb_kasir_shift")->where("vTampil", 1)->get()
->pluck("nm_shift", "kd_shift");
I've got 4 tables:
users
teams
locations
users_teams
my teams table has relationship with locations. (it has location_id field)
users and teams have belongsToMany relationship with users_teams.
users doesn't have directly relationship with locations.
But I need to get coaches in this location.
It means; "get me all coaches; which are belongs to teams of that location"
But couldnt move any to make this.
How can this be possible?
how my "coaches" function of my "Location" model?
If your relationships are set up correctly, and assuming you have an enum column named type with coach as a possible value, you should be able to do something like this:
$coaches = User::where('type', 'coach')
->whereHas('teams.locations', function($query)
{
$query->where('name', 'some place');
})->get();
I have a table containing multiple artist. While creating an event (updating event table) I need to select artists that I want to perform at my event and need to add their ids in the event_artist (transitive) table (that contains event_id & artist_id). How shall I proceed?
You would define a many to many relationship.
In Artist model class:
public function events()
{
$this->belongsToMany(Event::class);
}
In Event model class:
public function artists()
{
$this->belongsToMany(Artist::class);
}
This assumes your pivot table is named artist_event (the other two table names combined in alphabetical order). Otherwise you can pass the pivot table name as the second argument to belongsToMany: belongsToMany(Event::class, 'event_artist').
Then you can attach an artist to an event using $event->artists()->attach($artist);. Or detach $event->artists()->detach($artist);. You can also sync a collection of artists: $event->artists()->sync($artists);. You can also attach, detach or sync events to artists. All three methods will accept either Eloquent models or ids.
I have a database composed by users, users_child, child.
I create a ONE to MANY relationship between Users and users_child, then i create a relationship between users_child and child. Now the below code work:
$test = users::find(1)->users_child
$test1= users_child::find(1)->child
Now i want to know if is possible to create a single row that link the three table like this:
$test = users::find(1)->users_child->child
I create the relationship in the model but in the db i don't create Foreign Key, it's a problem? on the model i specify the field for link table.
http://laravel.com/docs/5.1/eloquent-relationships#querying-relations
You can chain relationships like this:
$user = Users::with("users_child.child")->where("id",1)->first();
Each point will mean a relation stored in the first.
Out of users users_child will be taken and out of users_child child will be taken. (Relations)
foreach($user->users_child as $user_child) {
$user_child->child;
}
will get you the data you need.
Yo all,
I have a users relationship pivot db table as follows:
id | user_id | relation_id | relationship
1 4 2 tutor
1 4 3 parent
The table relates user with one-and-other for various reasons.
I am trying to get the relationship column within the $user. I have managed to pull the related users
details no problem - $user->relations.
However, I just need to get the relationship - eg. Tutor or parent.
I am getting no dice with $relative->pivot->relationship
Any ideas? Thanks for taking the time to help.
#foreach($user->relations as $index=>$relative)
{{ $relative->first_name . ' ' . $relative->last_name}}
{{ $relative->pivot->relationship }}
#endforeach
To access ->pivot->whatever you need to add withPivot('whatever') to the relation definition:
public function relations()
{
return $this->belongsToMany('Relation')->withPivot('relationship');
}
Note: I wouldn't use relations and Relation names here, since it's misleading AND it may collide with Eloquent\Model stuff.