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
Related
In my web.php,
Route::get('studentmarksheet/addit','StmarksheetController#addit');
Route::resource('studentmarksheet','StmarksheetController');
I created a custom function addit() inside StmarksheetController. I have a form inside a view where I need to pass values to that addit function. For default functions inside my resoource controller, I used to call by
but, while trying to pass form values in addit function, it says route not defined. What exactly should I write? I have tried
{{route('studentmarksheet/addit')}}
{{route('studentmarksheet#addit')}}
and various other combinations.
I am a total beginner and I don't even know if am questioning this correctly. Please share your answers/suggestions/tips and so on, I would love to read them all.
First you need to name a route
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
Then call it
{{route('stmarksheet.addit')}}
Resource route are named for you by Laravel
You didn't set any name for the addit route. So, you need to write your route as like.
Route::get('studentmarksheet/addit','StmarksheetController#addit')->name('stmarksheet.addit');
And then you can get it in the view.
{{ route('studentmarksheet#addit') }}
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
I have never learnt this framework. As a beginner, I have to modify/add a link in the existing software which is made by some professional team. Could you people can help me out of this situation?
There is a function that shows the records on the main page public function index(). It has return View::make('Titles.Index')->withType('movie');. But I have to add a parameter that shows only english movies having field name language can have 'en' as its value. Would you suggest what needs to be done that it will show up using this condition? I have tried return View::make('Titles.Index')->with('language','en'); but it shows error on the main page.
Basically, index is a controller method that handles the view for your specific route.
Inside the controller method, you can filter the movies and pass a collection to the view. Considering that you have a model called Movie with namespace App\Movie so you can do something like:
public function index() {
// Returns a collection with movies where language field
// is equal 'en'.
$moviesEn = App\Movie::where('language', 'en')->get();
return View::make('Titles.Index')->with('moviesEn', $moviesEn);
}
This code injects the moviesEn variable inside your Titles.Index view. So there you can use it:
#foreach ($moviesEn as $movie)
{{ $movie->title }}
#endforeach
I am stuck in a situation where I am trying to get the output of a component to be shown in the view form as a dropdown. I cannot use model to store the data from the component output and show it in the view. As that will result in a huge data storage and might slow the application processing. The only option left is to directly use this component output in view.
Now, I am confused if I should call this output in controller and from controller I call the controller method in view similar to this: http://www.yiiframework.com/forum/index.php/topic/63169-way-to-call-controller-method-in-view-yii2/
The other way is to use JS in frontend and use the component output shown there in the view. Not sure if this is a good way either.
What will be the right way to do this?
You can use Component result in your view.
I am giving you an exapmle to get country code from component and store it in Model.
Ex.Function in Component.
public static function getDialCode()
{
return array(
"+91"=>"India (+91)",
"+62"=>"Indonesia (+62)",
"+98"=>"Iran (+98)",
"+39"=>"Italy (+39)",
);
}
Function used in view for DropDown List
<?= $form->field($model, 'country_code')->dropDownList(Yii::$app->mycomponent->getDialcode(),['prompt'=>'Select']) ?>
// I registered component as mycomponent
Let's say you load a view from a controller and that view loads another view that uses a lot of the same variables as the view that loaded it. How do get both views to share those variables? Thanks
All variables you define to a view, are passed down to views loaded within the parent one. You don't need to pass them down an other level through the second array parameter, unless you want to override a specific value.
Basically, define all variable in the 2nd parameter to the "parent" view and both views will have these variables.
For ex: you are loading view in controller:
$data["msg"] = "hi";
$this->load->view("view_file",$data);
In view_file, you are loading another view file
$this->load->view("view_file2",array("msg"=>$msg)); // here msg is extracted from first view file
In one view, i set this:
window.variable= variableToAnotherView;
windows.variable is to pass the variable globaly, so you will be able to call it in another view.