Laravel Blade view not working - php

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

Related

Show.blade.php is not displaying the content from the database, only the layout

`i am having a problem with my show.blade.php template, everything works fine but when I click on a post in the index page it directs me to the /post/1 page without showing the post content only the extended layout. please help
Web.php
Route:: resource('best-practices' , 'BestpracticesController');
*bestpracticescontroller.php
public function index()
{
$bestpractices = Bestpractices::all();
return view('bp.index',compact('bestpractices'));
}
public function show(Bestpractices $bestpractices)
{
return view('bp.show',compact('bestpractices'));
}
bp.show view template
#extends('layouts.front')
#section('content')
<div class="blog-details pt-95 pb-100">
<div class="container">
<div class="row">
<div class="col-12">
<div class="blog-details-info">
<div class="blog-meta">
<ul>
<li>{{$bestpractices->Date}}</li>
</ul>
</div>
<h3>{{$bestpractices->title}} </h3>
<img src="{{asset('storage/'.$bestpractices->cover_img)}}" alt="">
<div class="blog-feature">
{{$bestpractices->body}}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Thats because when you register routes via
Route::resource('best-parctices', BestparcticeController');
//Generated show route is equivalent to
Route::get(
'/best-practices/{best_practice}',
[BestpracticeController::class, 'show']
);
//route parameter is best_practice
Hence to achieve implicit route model binding the route parameter name must match the parameter name in the controller method
public function show(Bestpractices $bestpractices)
{
//here $bestpractices will be an int and not an object with
//model record as implicit route model binding doesn't work
return view('bp.show',compact('bestpractices'));
}
public function show(Bestpractices $best_practice)
{
//here implicit route model binding works so $best_practices is an object
//with model record
return view('bp.show',['bestpractices' => $best_practices]);
}
Or if you don't want to change the method parameter name in the controller methods then you need to override the route parameter name in the Route:resource() call when you define routes
Route::resource('best-practices', BestpracticesController::class)
->parameters([
'best-practices' => 'bestpractices'
]);
Laravel docs: https://laravel.com/docs/8.x/controllers#restful-naming-resource-route-parameters

How to make Wire:loading for every components

I use Livewire, how can I put a loder on all pages but call it once
nav.blade.php
<html>
<head>
#livewireStyles
</head>
<body>
<livewire:layouts.header/>
{{ $slot }}
#livewireScripts
</body>
</html>
header.blade.php
<div>
<div class="spinner" wire:loading></div>
</div>
welcome.blade.php
<div>
<input wire:model="search" placeholder="Search">
</div>
When I write things in the input, it doesn't show the loder.
I want to call this loder only once and work on every page
As of today wire:loading only works in the scope of a single livewire component. There is no wire:loading.all.
Simple solution: create a blade component and name it e.g. x-loading
<div class="page-loading" wire:loading>
Loading...
</div>
Style the component such that it appears e.g. in the bottom right corner of the page. You might want to use position: absolute.
Place this component in the view of every livewire component you want to show the loading indicator for.
This solution comes with two drawbacks. First, if you try to position the loading indiator in a container that has absolute position (like a modal), it wont have the same position as you intended. Second, the loading blade component will be in every livewire component.
I just tested a more elegant solution. For this you have to use livewire js hooks: https://laravel-livewire.com/docs/2.x/reference
<script>
window.onload = function() {
Livewire.hook('message.sent', () => {
window.dispatchEvent(
new CustomEvent('loading', { detail: { loading: true }})
);
})
Livewire.hook('message.processed', (message, component) => {
window.dispatchEvent(
new CustomEvent('loading', { detail: { loading: false }})
);
})
}
</script>
<div
x-data="{ loading: false }"
x-show="loading"
#loading.window="loading = $event.detail.loading"
class="absolute z-50 bottom-20 right-20"
>
This is taking way too long...
</div>
Place this this code e.g. in your app layout.

How to make dynamic #yield in laravel?

I want to make #yield('dynamic') to load different pages like single page application in laravel.
Route:
Route::get(
'/front-office-setup/{setup_type}',
'AdmissionFrontOfficeSetupController#index'
)->name('frontofficesetupview');
Controller:
public function index($setup_type)
{
$data['setup_type'] = $setup_type;
return view('frontoffice::frontofficesetup.frontofficesetup', $data);
}
View:
<div class="row">
<div class="col-md-3">asdf</div>
<div class="col-md-4">asdf</div>
<div class="col-md-5"> #yield('{{$setup_type}}')</div>
</div>
Section:
#extends('frontoffice::frontofficesetup.frontofficesetup')
#section('visitor-purpose')
sdfasd
#endsection
But it doesn't render or show in #yield('{{$setup_type}}')
Is there any way to do this?
Edit part*
Also i have already included a #yield type of things in view file
#extends('backend.master.master')
#section('content')
<div class="row">
<div class="col-md-3">asdf</div>
<div class="col-md-4">asdf</div>
<div class="col-md-5"> #yield($setup_type)</div>
</div>
#endsection
#yield is already started PHP tag <?php. SO no need to mention braces again{{}}. Simply try #yield($setup_type) It will work perfectly.

Laravel PHP if is homepage then <h1> tag

I have a <h1> tag in my template blade, its duplicate in all pages of my website. I want to do a condition if the page is Homepage then use this <h1> else don't write it.
This is my header code
<div class="col-sm-7">
<div class="slogan">
<h1 id="h1" class="tlt">my h1 Tag</h1>
<span id="span" class="tlt">" {{$slogan[0]->text}} "</span>
</div>
</div>
Thanks
You can use Request::is('/'), given that the "homepage" is on this url. The is() method can also accept wildcards which can be useful for stuff like Request::is('admin/*');.
If you have a route name for your routes, you can do like this in your view:
Route:
Route::get('/', ['as'=>'home', 'uses'=>'HomeController#index']);
In view:
#if(request()->route()->getName() == 'home')
// <h1> Text for home page </h1>
#else
//text for other pages.
#endif

coneigniter: linking controllers

I would like to link a controller to another page.
I have an index page that contains the login and now I need to link the controller and the login link:
<div class="toplogin">
<p>SIGN IN <span class="separator">|</span> REGISTER</p>
</div>
<div class="header_phone">
<p>CALL US NOW: <strong>1-800-531-453</strong></p>
</div>
<div class="clear"></div>
</div>
You need to link SIGN IN where "controller_name" is the name of your controller file and "login" is the name of the method.
In your controller_name.php file:
public function login(){
//do login stuff
$this->load->view('login');
}
Usually in codeignitor, we link by calling the controller function eg, if you have a controller register in which you have this function getdata(), then we link it by calling REGISTER

Categories