Calling database from a Laravel blade template - laravel

I've got a menu that is visible in the entire website, separated in a section that i call in every layout that i made :
<select class="custom-select">
<option>All categories</option>
<option>Bakery</option>
<option>Fruits</option>
<option>Eggs</option>
<option>Sweets and Chips</option>
<option>hygiene</option>
<option>Kitchenware</option>
</select>
Is there a simpler way to load and fill the options directly from the Blade template instead of calling the controller in every view that i make just to fill the menu?

If this is something you want to use in many different places,you could create a blade component and/or a view composer to load the data.

Related

Ajax call and then include a blade section

I have a {{!! Form::model!!}} where I am passing a variable $values from the Controller and displaying it inside of the Form select those values. In mine main blade i have 3 additional blades I am including through
#if($jobs->open)
#include('jobs.review.open')
#endif
I am trying to load the data $jobs from controller only when someone clicks , I figured to do some by create an ajax call with onclick event, so i have the data $jobs back. An only now trying to include jobs.review.open blade.
Any help will be appreciated
One common strategy is to put the rendering in the AJAX call. The server returns a ready-to-go chunk of HTML in its reply, and the JavaScript code simply inserts it into the DOM at the appropriate place with innerHTML. The server uses a blade to prepare the HTML that it then returns.
The initial page HTML that is displayed doesn't include the content, but does include dummy placeholder <div>s which mark the places where the HTML will be inserted.

Add a watcher for form fields

I'm using Laravel Nova, and I want to make "Save" button(on the Update view) unavailable while all required fields are empty.
Here's the part of the code of the "Update.vue" file that creates my form fields:
<div v-for="field in fields">
<component
#file-deleted="updateLastRetrievedAtTimestamp"
:is="'form-' + field.component"
:errors="validationErrors"
:resource-id="resourceId"
:resource-name="resourceName"
:field="field"
/></div>
Fields array fills with the data from backend.
So I can see this structure in the Vue dev tool. And when I change the input value it changes value of <form-text-field>, but I don't see this changes anywhere in my Update component.
So my question is - How I can get changes of my input fields dynamically?
In Vue data flows only one way, so the change of data in your Update component will be reflected on form-text-field, but not the other way around. To create 2-way data binding you should use v-model.
I suppose form-text-field is one of your custom components, in that case you should create a custom v-model for it. See: Customizing Component v-model
If you can't get it work post your implementation of form-text-field here, so we can help.

How to pass value to layout without passing value from all functions in Laravel?

In my project I am using a layout file and other view files. In my layout, there is a place to display users data. But I using this layout for different pages, so when I uses this layout for each pages, I need to pass user details from corresponding functions to view page. Is there any other short cut to pass user data only once, to layout directly??
Can anyone please reply??
Use Laravel View Composers
Official documentation
Here is an example :
'layout' is your view
View::composer('layout', function($view)
{
$view->with('count', User::count());
});
Put this into your "/app/start/global.php" file

Can I declare a view composer for a Form or HTML macro in Laravel 4?

I have a partial view that I am using to provide a pair of form select fields, and both sets of options depend on some variables set by a view composer for this view.
I would like to implement this view as a Form::macro instead so that I can parameterize some parts of it. However, I'm not sure how I can point the view composer at a macro instead of a partial view. Is this even possible?
I could go the route of pointing the composer at any view that I'm using the macro in, but I'd much rather have the option data load automatically any time I use the macro as it is a common component in my web app.
I use partials for some Bootstrap UI components, and create a macro to connect data into the template, so I only use HTML::macro or Form::macro to access them:
Form::macro('myMacro', function($data) {
return View::make('myPartial', $data);
};
This doesn't really answer the primary question, but one way I can get what I want is to use View::Make instead of #include in my views.
Where I was using:
#include('thepartial')
I can use something like this instead:
{{ View::make('thepartial')->with(array('param1'=>$param1, 'param2'=>$param2, ...) }}
This way I get the parameterization I want, and I still get the composer's effects.

Laravel usage "#layout" in Ajax Requests

Hi there stackoverflow!
In my laravel views I am using a default layout by calling
#layout('layouts.default')
to the same controller I am sending Ajax Requests yet I can't put 'if' to #layout if its a ajax call. Because if ajax request has made to controller it also produce header, footer and content(header and footer are in default layout). So to avoid this I made one copy without #layout of my view.
However its so boring to edit two files for making changes. Can't I add a code to my view something like that?:
#if(!$ajaxrequest)
#layout('layouts.master')
#endif
I want this because my codes in controllers are too messy
A slight variation is to put the logic for the layouts in your main layout template. e.g.
layouts/app.blade.php:
#if (Request::ajax())
#include('layouts.ajax-app')
#else
#include('layouts.default-app')
#endif
Your views just extend the main layout as usual e.g.
#extends('layouts.app')
#section('content')
Content goes here...
#endsection
And then create a default layout file (default-app.blade.php) and an ajax layout file (ajax-app.blade.php). The advantage of doing it this way is that any of your blade templates can be loaded via Ajax, without having to clutter up controller methods with lots of duplicated logic.
You can't have the #layout call after the if statement like that (see the notice in red under "Blade Templating" in the docs. You'll have to set the public $layout and call $this->layout->nest instead of View::make (see "The Basics" on the page linked to above).
You can use something like this in your view template:
#extends( 'layouts.' . (isset($layout) ? $layout : 'default'))
Also apply check in your controller(or Supercontroller) for AJAX request, if it is set $layout variable to needed layout. Otherwise "default" layout will be taken.

Resources