Include layout in Laravel - php

My master.blade.php file is like below
<body>
#include('layouts.header')
#yield('content')
#include('layouts.footer')
#include('layouts.footer-script')
</body>
My dashboard.blade.php file is like below
#extends('layouts.master')
// I would like to add Menu here
#section('content')
//more code here
#endsection
My menu.blade.php is like below.
#extends('dashboard')
#section('menu')
// more HTML code
#endsection
How can I add Menu in dashboard.blade.php ?

dashboard.blade.php file is like below
#extends('layouts.master')
#include('menu')
#section('content')
//more code here
#endsection
menu.blade.php is like below.
<div>
// more HTML code
</div>

If your menu is static just use it as include
dashboard file
#extends('layouts.master')
#section('content')
#include('layouts.menu')
#endsection
and menu file will be (menu.blade.php)
<a href=''>Home</a>

master.blade.php
<body>
#include('layouts.header')
#yield('content')
#include('layouts.footer')
#include('layouts.footer-script')
</body>
dashboard.blade.php
#extends('layouts.master')
#include('menu')
#section('content')
//more code here
#endsection
This is your code.
It is Good.
If this is error, I think the path must have been wrong of menu.blade.php.
ex:#include(frontend.menu)
Thank you for your answer.

Related

how to send data from controller to include/header blade file but return another blade file in laravel

I want to include send my data from my controller to my header.blade.php file in view
but i want to return the other view in the laravel as mentioned below:-
public function handleProviderCallback()
{
$user = Socialite::driver('google')->stateless()->user();
$finduser = User::where('google_id', $user->id)->first();
if($finduser) {
Auth::login($finduser);
$google_avatar = $user->getAvatar();
view('pages.home',['google_avatar', $google_avatar]);
return redirect()->route('home',);
}
}
By extending a layout is the cleanest way.
From the docs:
<!-- Stored in resources/views/layouts/app.blade.php -->
<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>
The child:
#extends('layouts.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
By just returning the "child" view from the controller, the data will be available in the header as well. If you pass it in the #extends declaration

Correct view output Laravel

That's my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Spieler;
class SpielerController extends Controller
{
public function getSpieler(Request $request){
$spielID = $request->get('spielID');
$spielerOutput = Spieler::where('spielPlanID', '=', $spielID)->get();
return view('spieler')->with('alleSpieler', $spielerOutput);
}
}
here you can see my view which I will trigger
#extends('app')
#section('contentSpieler')
<h1>Spieler</h1>
#if(count($alleSpieler) > 0)
#foreach($alleSpieler as $teamSpieler)
{{ $teamSpieler->note }}
#endforeach
#endif
#endsection
And here is my main index/app page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dateneingabe</title>
<link rel="stylesheet" href="/css/app.css">
<script src="{{ asset('/js/jquery-3.2.1.min.js') }}"></script>
</head>
<body>
<div class="container">
<h1>Spiele und Spieler AJAX - Drop Down</h1>
<div class="col-lg-4">
#yield('contentSpiel')
</div>
<div class="col-lg-4">
#yield('contentSpieler')
</div>
</div>
</body>
</html>
When my controller is going to trigger i get back this in my console
<script src="http://localhost:8000/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<div class="container">
<h1>Spiele und Spieler AJAX - Drop Down</h1>
<div class="col-lg-4"></div>
<div class="col-lg-4">
<h1>Spieler</h1> 2.5 Blasdasd
</div>
</div>
</body>
</html>
But I want only that {{ $teamSpieler->note }} should appear in my index/app page in the part #yield('contentSpieler'). What is wrong with my code? The correct value of note 2.5 is in the html console output but at the moment I don't know why there is no output on my index/app page?
if your master view file location is view->index->app.blade.php then
your view file should be like this
#extends('index.app')
#section('contentSpieler')
<h1>Spieler</h1>
#if(count($alleSpieler) > 0)
#foreach($alleSpieler as $teamSpieler)
{{$teamSpieler->note}}
#endforeach
#endif
#endsection
here your master view file location should be correct to extend it in your view file

How to arrange display position of conten when using blade in Laravel?

I have a folder in View:
resources -> view
- layouts
+ master.blade.php
- partials
+ footer.blade.php
+ header.blade.php
- news
+ show.blade.php
+ list.blade.php
- apartment
+ show.blade.php
My code in master.blade.php:
<head>
<script src="{{{ URL::asset('js/theme.min.js')}}}"></script>
</head>
<body>
<div class="main-container">
#include('partials.header')
#yield('content')
#include('partials.footer')
</div>
</body>
In news>show.blade.php:
#extends('layouts.master')
#section('content')
<div class="show-news">show all news at here</div>
#stop
In apartment>show.blade.php:
#extends('layouts.master')
#section('content')
<div class="show-apartment">show all apartment at here</div>
#stop
In HTML file have structure like:
header
content
- show slider image
- show list news
- show list apartment
footer
Because all file is define with section('content').
I don't know how to arrange display position of `section('content') correctly with my HTML file.
I think it should be like:
In news>show.blade.php: #section('news')
In apartment>show.blade.php: #section('apartment')
In master.blade.php:
<head>
<script src="{{{ URL::asset('js/theme.min.js')}}}"></script>
</head>
<body>
<div class="main-container">
#include('partials.header')
#yield('news')
#yield('apartment')
#include('partials.footer')
</div>
</body>
But I see on almost project don't using like this.
You can create another layout page that extends master page like this
master.blade.php
<head>
<script src="{{{ URL::asset('js/theme.min.js')}}}"></script>
</head>
<body>
<div class="main-container">
#include('partials.header')
#yield('content')
#include('partials.footer')
</div>
</body>
newlayout.blade.php
#extends('master')
#section('content')
#yield('news')
#yield('appartments')
#endsection
And you can use this layout for pages like you described, for another you can use master

Laravel Blade view not working

I am using Laravel 5.3.28 version. I have created main view(main.blade.php) and trying to extract the details from different child views(navbar.blade.php) which is in view/pages directory but its not working.
Route file in project/routes/web.php
Route::get('/', function () {
return view('main');
});
Main view in resources/views/main.blade.php(this file will receive the data from navbar.blade.php)
<div id="column">Test1</div>
<div id="container">
#yield('content')<!--This will get the content from navbar.blade.php-->
</div>
child view in resources/views/pages/navbar.blade.php
#extends('main')
#section('content')
<div id="row">
Test2
</div>
#endsection
I am able to see output as Test1 but not Test2. Why it this happening?
To see navbar content you should return navbar view:
Route::get('navbar', function () {
return view('navbar');
});
But if you're planning to include navbar in many views, you should use #include() instead of using section(), extends() and yield():
#include(`navbar`)
you should call view('navbar') instead of view('main') because navbar is a child of main view
if you call parent child content will not be seen there for call view('navbar')
For seeing the output Test2 you have to return navbar view
Route::get('/', function () {
return view('navbar');
});
#yield is a place when you will extends main view then you can put something inside this content.
I am explaining below by an example using your view file
you can simply put this in navbar.blade.php. it doesn't needs to be extended.
<div id="row">
Test2
</div>
In your main.blade.php you can put this
<div id="column">Test1</div>
<div id="container">
#include('navbar')
#yield('content')
</div>
The suppose you have decided to create a third view which will extends main view with navbar view. suppose your created a view named home.blade.php and add this to your home.blade.php view
#extends('main')
#section('content')
Welcome Home !!!
#endsection
Then if you calls create your route like below
Route::get('/home', function () {
return view('home');
});
It will render like below
<div id="column">Test1</div>
<div id="container">
<div id="row">
Test2
</div>
Welcome Home !!!
</div>
Hope this will clear your concept about view rendering

PHP Laravel 5.1

I do not understand why the content is not visible no browser.
index.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Teste</title>
</head>
<body>
#yield('content')
</body>
</html>
conteudo.blade.php
#extends('index')
#section('content')
<p>This is my body content.</p>
#endsection
routes.php
Route::get('index', function() {
return view('layouts/index');
});
Route::get('blade', function() {
return view('layouts/conteudo');
});
Try this out:
please change your conteudo.blade.php code
#extends('index')
#section('content')
<p>This is my body content.</p>
#endsection
To
#extends('layouts.index')
#section('content')
<p>This is my body content.</p>
#endsection
Hope this helps you.

Categories