3

I have a 'conversation_message' table and separate sender role by column 'sender_type' (admin/user). Both of admin & user in a different table. But when I call the model, that showed error Call to a member function addEagerConstraints() on null


Table column and data

| id | id_group | id_reply | id_sender | sender_type | message 
| 1  | 1 | null | 3 | admin | Hi, I'm admin
| 2  | 1 | 1 | 3 | admin | I wanna give u promo
| 3  | 1 | 2 | 18 | user | What's promo ?

I've tried if conditional with column value, but it doesn't work.

Conversation_message.php

public function sender(){
        switch($this->sender_type){
            case "user":
                return $this->belongsTo(User::class, 'id_sender', 'id');
            case "admin":
                return $this->belongsTo(Admin::class, 'id_sender', 'id');
            default:
                return;           
        }
    }

InboxController.php

public function message_detail_chat($groupId){
        $data = [];
        $data['messages'] = Conversation_message::with('replies')
                        ->with('sender')
                        ->with('recipients')
                        ->where(['id_group' => $groupId])->get();
}

I expect to use conditional model by column value 'sender_type' but the actual output is wrong.

0

1 Answer 1

1

laravel provide query scope your can read it from here https://laravel.com/docs/5.8/eloquent#local-scopes

   function userSender(){
      $this->belongsTo(User::class, 'id_sender', 'id');
      }

     function adminSender(){

       return $this->belongsTo(Admin::class, 'id_sender', 'id');

     }

public function scopeSender($query)
{
    return $query
          ->when($this->sender_type === 'user',function($q){
              return $q->with('userSender');
         })
         ->when($this->sender_type === 'admin',function($q){
              return $q->with('adminSender');
         }); 

}

Now you can access your Sender like this

     Conversation_message::Sender()->first();

This should give you the right Sender. Hope it helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry to late to reply, that code still error, sender field not appear in response. { id: 3, id_group: 3, id_reply: null, sender_id: 1, sender_type: "admin", message: "<p>Min</p>", created_at: "2019-05-10 08:31:22", updated_at: "2019-05-10 08:31:22", is_deleted: "0", date_readable: "5 days ago" }
when you get the error and what exactly error you get?
Actually I got no error message, but I want show sender field at response below. I access Sender with this code Conversation_message::Sender() ->where('id_group', $user_group[$i]['id']) ->orderBy('id', 'ASC') ->get();
simply echo sender_type if you know how to print the data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.