Laravel Relationship not cascading to hasMany - php

I have 2 models, 'Tickets' and 'Messages', user can have many tickets and each ticket many messages.
class Message extends Model {
public function Ticket() {
return $this->belongsTo(Ticket::class, 'ticket_id', 'ticket_id');
}
public function user() {
return $this->belongsTo(User::class, 'user_id', 'user_id');
}
}
class Ticket extends Model {
public function messages() {
return $this->hasMany(Message::class, 'ticket_id', 'ticket_id');
}
public function user() {
return $this->belongsTo(User::class, 'user_id', 'user_id');
}
}
When trying to create a message attached to current user via the below code:
$Ticket->messages()->create([
'message' => $post['message']
]);
I get the following error:
Cannot insert the value NULL into column 'user_id', table 'messages'
Since Ticket is already linked to a user, I assumed it's going to cascade to message as well. I can manually specify it but I want everything to be built using laravel relationships the most correct way

Related

Relationship between laravel models does not work

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');
}

relationship laravel empty result

I´m traying create relation ship in my model user-roles.
Firt i have to say that i´m using Bouncer library to use permission and roles.
I have my model User with this realation:
public function roles()
{
return $this->belongsTo('App\AssignedRoles', 'entity_id');
}
and my model AssignedRole:
public function user()
{
return $this->hasMany('App\User', 'id', 'entity_id');
}
and
public function roleName()
{
return $this->belongsTo('App\Role', 'id', 'role_id');
}
in my controller i´m trayin return all role that it has one User, using this:
$employee = User::with('roles')->get();
but return this:
Array([roles] => )
what i´m doing wrong?
I need create user list with his roles.
Thanks for read and sorry for my english

Check user if is in chat room Laravel

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()

Laravel BelongsToMany Relation "Call to undefined relationship"

I can't find the problem in this case and need some help with Laravel Many to Many Relationship:
lluminate\Database\Eloquent\RelationNotFoundException
Call to undefined relationship [registered_events] on model [App\User].
Whenever I try to access the relation the above error shows, eventhough the relation seems correct. The inverse relation works fine.
User Model
class User extends Authenticatable
{
public function registered_events()
{
return $this->belongsToMany(Event::class, 'event_user', 'user_id', 'event_id' )->withPivot('id', 'status', 'eventmanager', 'created_at', 'updated_at');
}
}
Event Model
class Event extends Model
{
public function registered_users()
{
return $this->belongsToMany(User::class, 'event_user', 'event_id', 'user_id')->withPivot('id', 'status', 'eventmanager', 'created_at', 'updated_at');
}
}
EventAllocationController
public function admin_event_overview()
{
$user = auth()->user();
// Problem here ⬇️
$eventmanagers = $user->with('registered_events')->get();
return view('dashboard.admin.event_allocation.overview', compact('user','eventmanagers'));
}

One to Many relation in laravel

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);
}

Categories