I have models:
ChatRoomMembers
ChatRoom
so I want to check if auth user is in chat room
my relationships:
ChatRoom:
public function chatRoomMembers()
{
return $this->hasMany(ChatRoomMember::class);
}
ChatRoomMembers:
public function chatRoom()
{
return $this->belongsTo(ChatRoom::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
So I created relationship in User Model:
public function chatRooms(){
return $this->hasManyThrough(
ChatRoom::class,
ChatRoomMember::class,
'user_id',
'id',
'id',
'chat_room_id'
)->orderBy('created_at', 'DESC');
}
and I am getting only this user chat rooms
In user model add below relationship
public function chatRoomMember(){
return $this->hasMany(ChatRoomMembers::class);
}
and in code
auth()->user()->chatRoomMember->count()
or
auth()->user()->chatRoomMember->exists()
Related
I have this Bookmark model
public function bookmarkable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
and in my User model
public function bookmarks()
{
return $this->hasMany(Bookmark::class);
}
public function posts()
{
return $this->hasMany(Post::class);
}
and the Post model
public function bookmarks()
{
return $this->morphMany(Bookmark::class, 'bookmarkable');
}
public function user()
{
return $this->belongsTo(User::class);
}
I want to create a new $bookmark and associate a $post with that bookmark
the bookmarks table has the columns bookmarkable_type, bookmarkable_id and user_id
Cause morphTo extends belongsTo
U can use like this
$post = Post::first()// the post u want add to bookmark
$newBookmark = new Bookmark([]);
$newBookmark->name="lorem ipsum";
$newBookmark->bookmarkable()->associate($post);
$newBookmark->user()->associate($post->user);
$newBookmark->save();
See the class docs:
https://laravel.com/api/9.x/Illuminate/Database/Eloquent/Relations/MorphTo.html
I have 2 models: one for users and one for clients. A user is a customer
User has a 'codigocli' field and client has a 'codigo' field
The relationships between my models are like this:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigo', 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'codigo');
}
My database is fine (I think) client has the 'codigo' field and users has the 'codigocli' field. So what am I doing wrong? When I want to query my home.blade.php with dd(auth()->user()-cliente()) I don't get anything, although it shows me the parent object fine.
H
You have an OneToOne relationship here so try this if you don't change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli');
}
if you change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli','local_id_name' );
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'local_id_name');
}
I have 3 tables users companies company_users when I try to get user company details it return null
Logic
company has many users
user belong to 1 company
user model
public function company()
{
return $this->belongsTo(Company::class, 'company_users', 'user_id', 'company_id');
}
company model
public function user()
{
return $this->belongsToMany(User::class, 'company_users', 'company_id', 'user_id');
}
any idea?
it's not one to many relation it's many to many, with pivot table (company_users )
so, the relation should be like:
public function company()
{
return $this->belongsToMany(Company::class, 'company_users', 'user_id', 'company_id');
}
I recommend rename it to 'companies' cause it plural
Regarding to this comment:
If you really want one to many relation. Try this:
public function company()
{
return $this->hasMany(User::class);
}
public function user()
{
return $this->belongsTo(Company::class);
}
I have these two tables:
product
- id
- name
favorites
- id
- product_id
- user_id
So, a user can add product to favorites only once. How can I set up this relation something like the following?
public function favorites() {
return $this->hasOne(Favorite::class, 'user_id', 'product_id')
}
So, I want to use both product_id & user_id such that the query would return proper result as per the following:
Get me the wishlist of user with id 1 and product with id 13!
you can do something like that:
in favourite Model class:
public function product(){
return $this->belongsTo('App\Product');
}
public function user(){
return $this->belongsTo('App\User');
}
In Product Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
In User Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
The user may have many favorites, so in Class User
public function favorites()
{
return $this->hasMany('App\Favorite');
}
Class Favorite
public function product()
{
return $this->belongsTo('App\Product');
}
public function user()
{
return $this->belongsTo('App\User');
}
If you have user you can
$userFavProducts = $user->favorites;
$product2 = $user->favorites()->where('product_id', 2)->get();
You should try this:
Favorites Model
public function product(){
return $this->belongsTo('App\Product','product_id');
}
public function user(){
return $this->belongsTo('App\User','user_id');
}
I having 3 model, User, Profile, University
i set up the relationship with
User model
public function profile(){
return $this->hasOne('App\Profile');
}
Profile model
public function user(){
return $this->belongsTo('App\User');
}
In profile table it will contain university_id & user_idcolumn as foreign key.
In University model
public function user(){
return $this->belongsTo('App\User');
}
Now i want to get data with $discussion->user->profile->university->name but it return Trying to get property of non-object .
What is the problems?
In Profile model add;
public function university(){
return $this->belongsTo('App\University');
}
public function user(){
return $this->belongsTo('App\User');
}
In university model add;
public function profile(){
return $this->hasMany('App\Profile');
}
then you can call;
$discussion->profile->university->name;
You must make a relationship with profile model and university model
Profile model
Public function university () {
return $this->belongsTo ('App\University')
}
University model
Public function profile () {
return $this->belongsTo ('App\Profile')
}
Now try :
$discussion->user->profile->university->name