C
C
chelkaz2016-03-23 11:10:55
Laravel
chelkaz, 2016-03-23 11:10:55

Should Blade use this design?

Everyone advises to use Blade to the maximum. Here I have a condition, to use it this way or all the same in a native way through <?php ?>. The if shorthand does not need to be offered, since the variable assignment code is long, for example:
$UserPhoto = '/images/user_profile/'.\App\Models\Question::find($quest_id)->user->UserPhoto->image_url .'.jpg';
Here's how it is now through Blade:
@if( $a == $b )
{{-- */ $a = 1 /* --}}
@else
{{-- */ $a = 0 /* --}}
@endif

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
chelkaz, 2016-03-23
@chelkaz

I don’t know which solution is more logical, but I did the following:
In order to avoid constant access to the database for information about the user and to the table for the photo, I used:
As a result , an array of 3 tables is formed in $AllQuestions .
In the Question
model I did the following:

class Question extends Model
{
    //
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function file(){
        return $this->hasOne('App\Models\File','user_id','user_id')->where('module_id', 'user_profile');
    }
}

And I explicitly specified fields for communication for the files table!
Otherwise, there will be no connection by default!
After that, we can pass a working array to the view:
return view('questions.index', compact('AllQuestions'));
Requests have decreased to 6 instead of almost 70 if for example 20 users.
Brought the idea to the doc. Eager Loading Multiple Relationships

E
Evgeny Perin, 2016-03-23
@seoperin

{{ $user->photo or 'дефолтное фото' }}

S
Suinly, 2016-03-23
@Suinly

In blade files, only blade constructs. Most of the logic, especially assigning any values ​​to variables, is still recommended to be performed before rendering the view (View) and outside of it, so as not to complicate.
In fact, just prepare the necessary variables in the controller, pass them to the view:

$a = ($a == $b) ? 1 : 0;
return render('someview')->with(['a' => $a]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question