V
V
vrazbros2021-12-09 12:36:15
Laravel
vrazbros, 2021-12-09 12:36:15

What is the best way to pass data to a template in Laravel?

What's the best way to do it?

1) option return view('layouts')
->with('var1', $var1)
->with('someVar2', $someVar2)
->with('someVar3', $someVar3)
->with('someVar4' , $someVar4);

or
2) $varData = $this->formatService($responce);
return view('layouts')
->with('varData', $varData);

but in the template you will need to access through varData.var1 and varData.someVar2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2021-12-09
@delphinpro

$var1 = '..';
$var2 = '..';
return view('layouts', compact('var1', 'var2'));

$var1 = '..';
$var2 = '..';
return view('layouts', [
  'var1' => $var1,
  'var2' => $var2,
]);

$varData = $this->formatService($responce); // ['var1' => '..', 'var2' => '..']
return view('layouts', $varData);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question