E
E
elionalisov2018-10-19 15:55:16
Laravel
elionalisov, 2018-10-19 15:55:16

How to implement lazy loading of an unknown laravel model?

This is the notification model:
e793c21323.jpg
This is how I create a new notification

/**
 Создает в таблице modernote уведомление пользователю
  
 param $user_id - id пользователя, которому показываем уведомление
 param $model_type - класс модели, с которой связано уведомление (не обязательное)
 param $model_id - id модели, связанной с уведомлением (не обязательное)
 param $header - заголовок 
 param $message - message тело
 param $lang_var - переменная, которая подставляется в ту или иную языковую переменную уведомления
  
 return boolean
*/
public function create( $user_id, $model_type=null, $model_id=null, $header, $message, $lang_var )
{		
    $newModerNote = new ModerNote;

    $newModerNote->user_id = $user_id;
    $newModerNote->model_type = $model_type;
    $newModerNote->model_id = $model_id;
    $newModerNote->header = $header;
    $newModerNote->message = $message;
    $newModerNote->lang_var = $lang_var;

    $newModerNote->save();
    
    return true;		
}

bda2a63a58.jpg
Tell me how you can implement lazy loading so as not to make a separate model request for each notification
$user_notifications = ModerNote::where('user_id', $user_id)
    ->orderBy('id','DESC')
    ->paginate(10);

The model in the notification is needed, for example, to form a link to the object on which the notification was sent to the user.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sokharev, 2018-10-19
@elionalisov

First, you are most likely confusing lazy loading with greedy loading. You obviously need exactly the greedy one.
And secondly: I recommend that you read this section of the documentation , which describes polymorphic relationships. It is very likely that they will help you solve this problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question