skip to Main Content

It was brought to my attention in a code review, instead of:

Version A: $someModel->with('relationMethodA.id,some_field, created_at')

rather use this notation:

Version B $someModel->with(['relationMethodA' => fn($q) => $q->select('relationMethodA.id,some_field, created_at')]).

I wonder because both give the same result. Are there any reasons to use one or the other?

4

Answers


  1. When using ‘with()’ with an array the array parameter allows you to pass multiple variables to the view, which can then be accessed within the view using the specified keys For example:

    return view('my-view')->with(['var1' => $value1, 'var2' => $value2]);
    

    When using with() with a string parameter it is typically used in the context of a single variable or data being passed to a view. For example:

    return view('my-view')->with('variable', $value);
    

    It depends on your specific use case and the number of variables you need to pass to the view.

    Login or Signup to reply.
  2. As what document said in Constraining Eager Loads

    Sometimes you may wish to eager load a relationship but also specify additional query conditions for the eager loading query. You can accomplish this by passing an array of relationships to the with method where the array key is a relationship name and the array value is a closure that adds additional constraints to the eager loading query:

    You can see in the official document example:

    
    use AppModelsUser;
    use IlluminateContractsDatabaseEloquentBuilder;
     
    $users = User::with(['posts' => function (Builder $query) {
        $query->where('title', 'like', '%code%');
    }])->get();
    

    Only those posts with the word code in their title will load for each user.

    $someModel->with(['relationMethodA' => fn($q) => $q->select('relationMethodA.id,some_field, created_at')])
    

    This will load the relation "relationMethodA" with this specific columns only:
    ‘relationMethodA.id,some_field, created_at’, However Version A will load all columns. This might be a good option when you don’t need whole data in your relations.

    Login or Signup to reply.
  3. Well, Indeed both queries return the same result but there’s very slight and neglible perfromance measure which makes Version A to be faster.
    However Version B will allow you to add more complexity in the query.

    Login or Signup to reply.
  4. When eager loading, the first syntax you illustrated should use a colon;

    $someModel->with('relationMethodA:id,some_field, created_at')
    

    So this loads just three of the columns and is the same as adding a select statement to the relationship, as you did in the second example

    Note that you can also pass an array of related models;

    $someModel->with(['relationA', 'relationB','relationC']);
    

    as well as just passing multiple parameters

    $someModel->with('relationA', 'relationB','relationC');
    

    and nested relations

    $someModel->with('relationA.subrelation','relationB');
    

    personally, I would always use the colon to specify the columns to load as this is far more concise and readable than the closure approach, although this can be useful for adding additional conditions or even query scopes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search