Laravel - edit data before switching to viewing

I am looking for a way to edit the data before submitting it for viewing.

Quick example (just for demonstration): Let's say I pass the variable $namefor viewing through the controller. I would like to use something to pass another variable $messagethat will contain Hello $name, therefore, for example, Hello John, if the variable $nameis John.

I donโ€™t want to send this second variable to the controller, because I am going to use a lot of controllers, views and what I want to do with the data is quite difficult.

I need to use this for both variables view("foobar", ["foo" => "bar"])and sessions view("foobar")->with("foo", "bar").

I tried to use both Middleware and Service Provider, but the problem was that I could not access the data being sent.

The only possible solution that I can think of now is to use the View layout, which I will include in each view, and which will convert the variables (using something like <? $message = "Hello $name"; ?>in the view), but that doesnโ€™t mean t seems like the correct MVC solution for me.

Thank you all for your answers!

+4
source share
1 answer

If you want to transfer session data and several variables, do the following:

session()->flash('message', 'some message');

return view('foobar', [
    'foo' => 'bar',
    'second' => 'something'
]);

Update

If you understand correctly, you want to use the composer view .

+1
source

Source: https://habr.com/ru/post/1657139/


All Articles