I have these 3 tables: users, recipesand votes
user votes on a recipe
a recipe has many votes
a user has many votes
a user can only vote once on a recipe
In my view i run into a problem, when I try to access the vote rating of a user on the recipe like:($recipe->user->vote->rating), it returns the first vote rating of that user (might be the vote of that user on another recipe)
my relations are:
User.php:
public function vote()
{
return $this->hasOne('App\Vote');
}
Recipe.php:
public function votes()
{
return $this->hasMany('App\Vote');
}
Vote.php:
public function user()
{
return $this->belongsTo('App\User','user_id');
}
public function recipe()
{
return $this->belongsTo('App\Recipe','recipe_id');
}
First I would make $user->votes a hasMany relationship, since there are multiple votes per user. Then you can create two custom query scopes on the vote model:
User.php:
public function votes()
{
return $this->hasMany('App\Vote');
}
Vote.php:
public function scopeByUser($query, $user_id){
return $query->where('user_id',$user_id);
}
public function scopeOnRecipe($query, $recipe_id){
return $query->where('recipe_id',$recipe_id);
}
These can be used like:
$recipe->votes()->byUser($id)->first();
$user->votes()->onRecipe($id)->first();
Related
I have the following database schema:
The following is intended for this scheme:
1 user can have several teams and a team can have up to two users (I assumed it is a many-to-many relationship and then I validate whether or not a team has only two users)
1 team can have multiple games and a game only has a home_team (team) and an away_team (team) here I assumed it will have two 1 to many relationships (1 team has multiple games and a game only has an away_team and a home_team).
My question is how should I make their relationships in the models, for example, to get through a user all his games, both away_team and home_team.
I try this:
public function awayTeam()
{
return $this->belongsTo(Team::class, 'uuid');
}
public function homeTeam()
{
return $this->belongsTo(Team::class, 'uuid');
}
Team Model
public function homeGames()
{
return $this->hasMany(Game::class, 'uuid', 'home_team_uuid');
}
public function awayGames()
{
return $this->hasMany(Game::class, 'uuid', 'away_team_uuid');
}
User Model
public function teams()
{
return $this->belongsToMany(Team::class);
}
O find the problem:
in Team model i need declare 'foreign_key' and 'local_key' like that, because i dont use convencional fields
public function homeGames()
{
return $this->hasMany(Game::class, 'home_team_uuid', 'uuid');
}
public function awayGames()
{
return $this->hasMany(Game::class, 'away_team_uuid', 'uuid');
}
In Game model i change for that, for same reason:
public function awayTeam()
{
return $this->belongsTo(Team::class, 'away_team_uuid', 'uuid');
}
public function homeTeam()
{
return $this->belongsTo(Team::class, 'home_team_uuid', 'uuid');
}
and now i can have relation response.
i have a relation between User model and Friend model
User.php
public function friends()
{
return $this->hasMany(Friend::class);
}
the relation actually returns all the records where the foreign key column (user_id) between User and Friend equals the current user's id but i want to return also the records where current user's id is in another column in friends table which's name is friend_id, this is my try
public function friends()
{
return $this->hasMany(Friend::class,'user_id','friend_id');
}
it's actually not returning the correct records
I don't think there is a direct way to that but you could try making 2 relations and merge them into one relation
public function friends() {
return $this->hasMany(Friend::class);
}
public function additionalfriends() {
return $this->hasMany(Friend::class, 'friend_id');
}
public function allfreinds() {
return $this->friends->merge($this->additionalfriends);
}
another solution you might try is
public function friends()
{
return Friend::where(function($query) {
$query->where('user_id',$this->id)
->orWhere('friend_id',$this->id);
});
}
I've 2 tables
Users table
id
name
...
Chats table
id
from_id // fk - id on users, the user sent the message
to_id // fk - id on users, intended user
message_body
Now, I'm trying to set up One to Many relation where user has many chat messages but a chat message has two user. from and to user
How can I define this relationship?. I have to user eager loading.
I tried to use Compoships but it didn't work.
My code
User Model
public function chats() {
return $this->hasMany(Chat::class, ['from_id', 'to_id'], 'id');
}
Chat Model
public function user() {
return $this->belongsTo(User::class, ['from_id', 'to_id'], 'id');
}
public function chats() {
return $this->hasMany(Chat::class, 'from_id', 'id') + $this->hasMany(Chat::class,'to_id','id');
}
public function userFrom() {
return $this->belongsTo(User::class, 'from_id', 'id');
}
public function userTo() {
return $this->belongsTo(User::class, 'to_id', 'id');
}
Why don't you do something like that?
On your User model, set up two relationships like this:
public function chatsFrom() {
return $this->hasMany('App\Chat', 'from_id');
}
public function chatsTo() {
return $this->hasMany('App\Chat', 'to_id');
}
Then, on your Chat model, also set up two relationships, one to from and another to to, both referencing a User model. Like this:
public function fromUser() {
return $this->belongsTo('App\User', 'from_id');
}
public function toUser() {
return $this->belongsTo('App\User', 'to_id');
}
This way, you can access the relationships using something like this:
$user->chatsFrom();
$user->chatsTo();
$chat->fromUser();
$chat->toUser();
I'm creating a website where a User can login, select a Company and after selection has assigned Roles for that company. These Roles can be different for each company.
I've tried hasMany and polymorphic releationships with tables for users, companies and roles but can't get it to work.
//In the Role model:
public function company()
{
return $this->morphToMany('App\Comp', 'userroles');
}
public function users()
{
return $this->morphToMany('App\User', 'userroles');
}
I would like a list of all the Roles/User for the selected Company, a list of Users/Companies for each Role and so on.
Use this:
//In the Company model:
public function users()
{
return $this->hasMany('App\User');
}
//In the User model:
public function company()
{
return $this->belongsTo('App\Comp');
}
public function role()
{
return $this->hasOne('App\Comp');
}
//In the Role model:
public function users()
{
return $this->hasMany('App\User');
}
Then, you can simply access the users in a company like {{$company->users}}, {{$user->company}}, {{$user->role}} or may be like {{$role->users}}
I'm having a problem while creating a Forum with Laravel 5.7
I want to have these three models: User, Category, Thread and Post. The problem is that i don't how to define one of my Thread model relationships.
This model has a creator:
public function creator()
{
return $this->belongsTo('App\User');
}
...it has a series of replies:
public function replies()
{
return $this->hasMany('App\Post');
}
...and, finally, participants:
public function participants()
{
return $this->hasManyThrough('App\User', '???')
}
I should get the thread participants through its replies but it don't know if i should put the post class like this:
public function participants()
{
return $this->hasManyThrough('App\User', 'App\Post')
}
...or if this is an special case with some considerations. Any help?
Since posts acts as a pivot table, a BelongsToMany relationship is the better choice here:
public function participants()
{
return $this->belongsToMany('App\User', 'posts');
}