laravel pass parameter to master layout before render? - php

i am making an application and i want that pass error parameters to master view use within partial blade called error.blade.php.
i am checking some variable in controller and if anything occur error i added it to error array as below :
$error[] = 'error 1';
and i want to pass this to partial blade view called error.blade.php and render it but how can i do this ?
this array could be empty or not.
how can i pass this array to master layout before render ?
i use this error array every controller.
please help me

You can use a View Composer and just pass the data in within the boot() method of the provider.
View::composer('master', function ($view) {
$view->with('errors', $errors);
});
The $errors will be available in your view now.

All you need to do is add this line :
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
To your app/Http/Kernel.php especially in the protected $middleware = array
And all you need to do is add ->withErrors($errors) to redirect or
view methods and $errors variable will be accessible everywhere in
the view even if the master layout

Related

Can I pass variables to blade from Observer in Laravel?

I want to send variables to the navigation blade from Observer in Laravel.
What I actually want to do is showing badges on the navigation bar every time created new model data.
I made a variable in the created function in the event observer and want to pass it to the navigation blade.
so I tried like below.
public function created(QnaNonmember $qnaNonmember)
{
$qna_new = 1;
return $this->view('partials.navigation')->with(compact('qna_new'));
}
But in the navigation, it causes an error like below.
Undefined variable: qna_new
How can I do this in the right way?
You can't pass variable to blade from Observers. If you want to pass variable to blade then you will pass from controller.
More info check Doc

Variable is undefined Make the variable optional in the blade template after using view composer

I've been trying to define a variable globally into my project (laravel 7). I need this variable be available in all views but i am getting following error:
$count is undefined
Make the variable optional in the blade template. Replace {{ $count }} with {{ $count ?? '' }}
First step i created a new service provider TestSeriviceProvider
Second step I added following line to congif\app.php
App\Providers\TestServiceProvider::class,
Then i wrote following codes into boot method of TestServiceProvider
public function boot()
{
View::composer('*', function ($view) {
$view->with('count', 333);
});
}
Which part have i made mistake?
This can be done in the boot() method of the AppServiceProvider.
Just add view()->share('count', 333); and your $count is accessible on any blade page. If you have many shared data implemented, yes you can make a separate ServiceProvider for this. But keep in mind that your shared data may be overwritten in controllers.
Depending on the reason why you want to do this, you can look into a combination of middleware and the session() helper or combine shared variables in an array.

How to make the controller data override view controller in Laravel?

To build a sidebar that has a lot of dynamic data on it I learned about View composers in Laravel. The problem was that View Composers trigger when the view loads, overriding any data from the controller for variables with the same name. According to the Laravel 5.4 documentation though, I achieve what I want with view creators :
View creators are very similar to view composers; however, they are
executed immediately after the view is instantiated instead of waiting
until the view is about to render. To register a view creator, use the
creator method:
From what I understand, this means if I load variables with the same name in the controller and the creator, controller should override it. However this isn't happening with my code. The view composer:
public function boot()
{
view()->creator('*', function ($view) {
$userCompanies = auth()->user()->company()->get();
$currentCompany = auth()->user()->getCurrentCompany();
$view->with(compact('userCompanies', 'currentCompany'));
});
}
And here is one of the controllers, for example:
public function name()
{
$currentCompany = (object) ['name' => 'creating', 'id' => '0', 'account_balance' => 'N/A'];
return view('companies.name', compact('currentCompany'));
}
the $currentCompany variable in question, for example, always retains the value from the creator rather than being overridden by the one from the controller. Any idea what is wrong here?
After some research, I realized my initial assumption that the data from the view creator can be overwritten by the data from the controller was wrong. The creator still loads after the controller somehow. But I found a simple solution.
For those who want to use View composers/creators for default data that can get overwritten, do an array merge between the controller and composer/creator data at the end of the boot() method of the composer/creator, like so:
$with = array_merge(compact('userCompanies', 'currentCompany', 'currentUser'), $view->getData());
$view->with($with);

Laravel - How to pass data to include

So essentially all of my views are using the header.blade.php since I am including it in my master layout. I need to pass data to the header on every single view. Is there a way to pass data just to the include rather than passing the data for the header in each view?
You don't need to do that, but you can:
All variables that are available to the parent view will be made
available to the included view. Even though the included view will
inherit all data available in the parent view, you may also pass an
array of extra data to the included view:
#include('view.name', ['some' => 'data'])
One option if you're trying to send data only to the included view is to use a view composer. They will fire even in the case of trying to prepare a view for #include
view()->composer('header', function($view) {
$view->with('data', 'some data');
});
actually the very best and faster method of sharing data to all views could be just using the
AppServiceProvider
instead of Jeff's answer you can use the share method instead of the composer method and achieve your goal faster.
Just pass the data you want in the boot method of the AppServiceProvider like following
public function boot()
{
View::share('key', 'value');
}
for more check this

How to send single variable to view in codeigniter

Hi I want to send single variable to view but I am unable to do it, Please help me.
Controller:
public function src($id=""){
$this->load->view('catsrc', $id);
}
View:
<div class="container">
<?php echo $id ;?>
</div>
Error Getting:
Message: Undefined variable: id
I am not an expert of CodeIgniter but according to official documentation: "Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function"
$this->load->view('catsrc', [ 'id' => $id ]);
was that so hard ?
Happy coding :)
CodeIgniter views read from a $data array. To reference $id in a view, put it in the $data array in your controller
$data = array('id' => 'your_value_here');
and pass $data to the view.
Full documentation on views lives at: https://ellislab.com/codeigniter/user-guide/general/views.html
How CI Classes Pass Information and Control to Each Other
Calling Views
We have seen how the controller calls a view and passes data to it:
First it creates an array of data ($data) to pass to the view; then it loads and calls the view in the same expression:
$this->load->view('testview', $data);
You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers.
However, you can't call one controller from another, or call a controller from a
model or library. There are only two ways that a model or a library can refer back to a controller:
Firstly, it can return data. If the controller assigns a value like this:
$foo = $this->mymodel->myfunction();
and the function is set to return a value, then that value will be passed to the variable $foo inside the controller.
This may not answer your question directly but helps!!

Categories