Fetch data from inside Blade view - php

I'm learning about Laravel. I know how to create a controller that builds an array of data from an Eloquent model, pass that array to a view and display it.
What I am trying to do is create a main view that has the header and footer information all pages use. Then I try to use a child view to display the body.
The main blade file has an include for a file that builds a dynamic navigation bar.
How can I go about retrieving data from within the include file the main blade file that's called by a child?
What I have (very abbreviated) is:
main.blade.php
<html>
#include('header')
<body>
#section('content')
#stop
</body>
</html>
home.blade.php
#extends('main')
#section('content')
<h1>Home</h1>
#endsection
Now from the controller, I call the home.blade.php view
Can I fetch the data thats needed in the header view from INSIDE the header file rather than having to pass the data from every view I call? Or do I need to simply create a global function that I call in every controller before I call a view?

In your main page:
<html>
#include('header')
<body>
#yield('content')
</body>
</html>
In your view:
#extends('main')
#section('content')
<h1>Home</h1>
#endsection

The dynamic navigation bar you mention should come from a view composer. In this way it will be available all the time to all views.
Do a test with the following in your routes.php file:
View::composer('*', function($view)
{
// $navBar stuff
$view->with('navBar', $navBar);
});
Now $navBar will be available in your views.
http://laravel.com/docs/5.1/views#view-composers

Related

Laravel Blade Not Rendering/ Layout Not Working

So I created a custom layout for my design, it works on other blade but not for this one blade. I did some recheck just to make sure that it is the same as other blade that works.
Also the controller is prettymuch the same since it has the same function for my other blade.
My checkout.blade :
#extends('layouts.custom')
#section('content')
<h1> Content </h1>
#endsection
My custom layout
<!DOCTYPE html>
<html>
<head>
</head>
<body>
#yield('content')
</body>
<footer>
</footer>
</html>
My Controller:
public function checkout()
{
$keranjangItems = \Cart::session(auth()->id())->getContent();
return view('keranjang.checkout', compact('keranjangItems'));
}
And I've tried to put the content on other blade and got no problem.
And I've tried to change my other controller to view this blade and also working.
Can anyone help me find where the problem is? thanks
Update: I also tried to just put together the code no include layout or what so ever and it's still have the same result
On your custom.blade file it says
#yield('layouts.header')
and on your checkout.blade file it says
#section('content')
<h1> Content </h1>
#endsection
Either update the layout to match the section name on your checkout view or update the checkout view to match the yielded section name on the layout.

How to using extending the same masterpage layout in two partials?

I have two page on Laravel.
I can easily extend this layout in another blade partial to get a working modal with a header, content, and footer which I can embed using #extends('master').
My problem is:
The first page is using header1.blade.php.
And second page is using header2.blade.php.
On master.blade.php is my master page.
<body>
#include('partials.header1')
#yield('content')
#include('partials.footer')
</body>
On index.blade.php is using master.blade.php is master page with extends('master.blade.php).
On listnews.blade.php have same master page.
I want at listnews.blade.php using partials.header2.
Have any way to do this?
As an alternative to #lewis4u's answer you can use the #section directve.
This way you can define the default header to be used but you can change it whenever you need to.
Firstly, change your master.blade.php file to be:
<body>
#section('header')
#include('partials.header1')
#show
#yield('content')
#include('partials.footer')
</body>
Then in your listnews.blade.php file just add another section after the extends:
#section('header')
#include('partials.header2')
#endsection
Hope this helps!

Laravel - How to load content with new URL

I have inherited a Laravel project to which I need to add a new page with some functionality that I have created. What I've got appears to be a main "app.blade.php" file, which includes some stuff that will always be visible.. like sidebar, login auth stuff and so on.
Now adding stuff to this is no problem. But what I want is a separate .php file that is loaded in the main content area of the app.blade.php when I go to a certain URL, let's call it "mypage.com/newpage". (Essentially, I want a link in the sidebar to load this new content.)
So my custom content should appear in the main content area, but the standard sidebar, etc, should still be there. I'm guessing it's something with routes, but... How do I proceed? Which files do I edit? What do I add and where? I already got my new HTML and Javascript code ready - I simply need to add it into the Laravel project the right way.
Suppose , bellow code is your app.blade.php file which you want to inherit.
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
and you want to load the app.blade.php file here. You need to extends the page and declare the sections like this.
#extends('app')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
Extending a layout on laravel This may help you.
What you're looking for is template inheritance. You create a layout template that you use as the main layout and your pages inherit that main layout template. See the Laravel Blade documentation: https://laravel.com/docs/5.2/blade#template-inheritance

Laravel 4 include multiple views in route

Is it possible to include multiple view in route? What is the best practice for doing this, let say I want config file, header, content, and footer file to join and load in a view? If do it in the route, then I can easily change the content based on route request.
Thank you.
You need to take a futher look at Laravel's Blade templating. With Blade templating, you can create layouts and cascade them onto each other really nicely. For example, let's take the following routes...
app/routes.php
Route::get('about', function()
{
return View::make('about');
});
Route::get('contact', function()
{
return View::make('content')
});
As you can see, we have two different views for those two different request. However, with Blade templating, and sections, we can create a master layout and only change the content that we need. So, here is what our master layout would look like.
app/views/layouts/master.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Site Title</title>
</head>
<body>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
#yield('content')
</body>
</html>
This is our master layout. We have our nav that will always stay the same, our HTML and head and everything that we don't want to write over and over again. But, we are also using yield in blade to accept content and place it there. This is where our actual views come into play from routes.php.
app/views/about.blade.php
#extends('layouts.master')
#section('content')
<p>This is the about me content.</p>
#endsection
We can simply extend the master layout, and place our content within the content section, which we can name anything we want. Same with the other page, contact.
app/views/contact.blade.php
#extends('layouts.master')
#section('content')
<p>This is the contact page content.</p>
#endsection
As you can see, it's not so much as including multiple views...but rather it's about extending different views and putting them together using Blade.
Alternate way to include header / footer in another view will as below.
https://laravel.com/docs/5.3/blade#including-sub-views
<div>
#include('templates.header') // views/templates/header.blade.php
<form>
<!-- Form Contents -->
</form>

load js when loading a subview in laravel

Is there a way to load js into the <head> section of the page whena subview is loaded. I would like to only load specific JS files based on the view that is being called. I am using the blade template engine.
There is a much easier way to do this using sections and template inheritance.
Firstly, create a master layout file, something like this:
http://paste.laravel.com/UY9 It's one I use that includes
Initializr/Bootstrap. I store this in views/layouts/frontend/ as
master.blade.php for the frontend and views/layouts/admin/ as
master.blade.php for the admin and amend as necessary.
You'll notice various #section declarations ending with #show. Using
#show at the end instead of #stop, allows you to override them in
your other views, you'll notice I've added a #section('scripts')
declaration. So, you can create a view like so:
#extends('layouts.frontend.master')
#section('scripts')
Your Scripts
#stop
#section('content')
Your content
#stop
It's that simple. This is very powerful stuff, as it gives you the ability to set a default but also override it if necessary, keeping your views very clean and minimal.
A better way to do this though would be to do:
#extends('layouts.frontend.master')
#section('head')
#parent
Your Scripts
#stop
#section('content')
Your content
#stop
Then you can remove the #section('scripts') declaration from your master layout. Using the #parent helper will allow you to append content to a section, thus keeping it's default while adding the extra content you have specified in your view.
You can also provide default content for #yield declarations, like so #yield('content', 'Default content').
Check out http://codebright.daylerees.com/blade
First make a common header layout.
app/views/layouts/header.blade.php - header layout
<!DOCTYPE html>
<html>
<head>
<title>{{ $page->title }}</title> {{-- Getting title from $page object, which is passed from route --}}
{{ HTML::style('css/common.css') }}
{{ HTML::script('js/jquery.js') }}
{{ HTML::script('js/common.js') }}
Then page specific script layout.
app/views/layouts/script-about.blade.php - about-page script layout
{{ HTML::script('js/about.js') }}
Then view for specific page.
app/views/about.blade.php - about page
#extends('layouts.master')
#section('content')
<p>About-Us page content goes here</p>
#stop
Then common footer.
app/views/layouts/footer.blade.php - footer layout
</body>
</html>
Then the main layout to render.
app/views/layouts/master.blade.php - main layout
#include('layouts.header')
#include('layouts.script-'.$page->slug) {{-- Getting page name from $page object --}}
</head>
<body>
#yield('content')
#include('layouts.footer')
And from the route, you can pass the $page variable. You may like this route,
Route::get('{slug}', function($slug) {
$page = Page::where('slug', '=', $slug)->first();
// getting the datas from Page model with pages table where slug = last uri in your address-bar
if ( is_null($page) ) { // if no page in db, call 404
App::abort(404);
}
// render the view with page details
return View::make($page->slug)->with(array('page' => $page));
});
In laravel 5.4 as stated in the document
You can simply use stacks(#stack and #push) to be able to load CSS and JS from subviews(child views).
Add #stack to your layout where you want JS files or CSS files to be added from child views to layout.
Here I will define two stacks in the layout file one for CSS files and one for JS files. I give the first and the second stacks arbitrary name for instance styles and scripts
we want our CSS files to be loaded in the part of layout file
<head>
#stack('styles')
</head>
and let's say we want our scripts to be added right be for the closing body tag
in layout file
#stack('scripts')
</body>
now in the child view i can easily add CSS and JS files like this
#push('styles')
<style rel="stylesheet" href="{{asset('dropzone/dist/min/dropzone.min.css') }}" ></style>
#endpush
#push('scripts')
<script src="{{ asset('dropzone/dist/min/dropzone.min.js') }}"></script>
#endpush

Categories