I want to show the result when search a keyword successful.
In routes\web.php:
Route::get('tim-kiem', 'Frontend\ListBaiVietController#timkiemBaiViet');
In controller ListBaiVietController, I have a function:
public function timkiemBaiViet() {
$tukhoa = \Request::get('tukhoa');
$ketquatimkiems = Post::where('title','like','%'.$tukhoa.'%')
->orderBy('title')
->paginate(20);
// var_dump($ketquatimkiems);
return view('post/searchresult',compact('ketquatimkiems'));
}
I am using var_dump($ketquatimkiems), it shows 2 results.
In post/index.php I am calling content:
<body>
#yield('content')
</body>
And post/searchresult.php:
#extends('post.index')
#section('content')
#foreach($ketquatimkiems as $ketqua)
<div class="container-artical">
<div class="list-excerpt">
{!! $ketqua->excerpt !!}
</div>
</div>
#endforeach
<nav class="blog-pag">
{{ $ketquatimkiems->links() }}
</nav>
</div>
#endsection
When I am typing text quận 8. It is only showing code, not result.
your file name must have .blade extension
post/searchresult.php: to post/searchresult.blade.php:
post/index.php to post/index.blade.php
for more information
Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file
Ref: https://laravel.com/docs/5.5/blade
you are not using laravel blade engine. Rename view
files like so index.blade.php
Related
I'am new to Laravel I just want to know the difference between #extends and #include
#extends('tempalate')
can I use #include to add template file in my laravel project.
#include('tempalate')
To simply put it:
Using #include('') function you are including or adding an existing file.
Using #extends('') your are sending a portion of your file to the extended file.
Which is usually wrapped inside a #section('') function.
As per Laravel Documentation:
Blade's #include directive allows you to include a Blade view from
within another view. All variables that are available to the parent
view will be made available to the included view:
<div>
#include('shared.errors')
<form>
<!-- Form Contents -->
</form>
</div>
When defining a child view, use the Blade #extends directive to
specify which layout the child view should "inherit". Views which
extend a Blade layout may inject content into the layout's sections
using #section directives. Remember, as seen in the example above, the
contents of these sections will be displayed in the layout using
#yield:
<!-- Stored in resources/views/child.blade.php -->
#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
Laravel -> Blade Templates -> Including Subviews
I have a little problem with Laravel Blade in Laravel version 5.2. Can someone tell me how to include a Blade template within a Blade template? I already try to include welcome.blade.php
by #include('welcome') but, I always get an error message.
first you need to define a master page or main template what ever you say.
we create main.blade.php as template page .
<html>
<div class="row">
#yield("content")
</div>
the above code is our simple template page.
inside your welcome.blade.php write as below
#extends("main")
#section("content")
your code
#stop
if welcome.blade.php is your template or master page do like below.
welcome.blade.php :
<html>
<div class="row">
#yield("content")
</div>
inside your other page you want use welcome page as template do as below :
#extends("welcome")
#section("content")
your code
#stop
I recently started to build a Laravel/blade web application and I want to switch between what my layout view is showing. I want to replace 'content' with some other blade.php view when I press a button in the layout file. For example in ReactJS you can just determine the rendered content with an IF statement and some vars.
<div class="container">
#yield ('content')
</div>
I googled a bit but couldn't find a straight forward solution so I wondered if this is common in Laravel or do you just have to make a lot of different layout files with other #yield('...')? A lot of code would be duplicated right?
You can use conditional blade directives
#if(Session::get(user_type') == 'Admin')
#extends('layouts.admin')
#else
#extends('layouts.normal')
#endif
#section('title')
#endsection
#section('content')
etc ....
I have the site with the structure like:
First: master.blade.php : this contain section('content')
<body>
#include('partial.header')
#yield('content')
#include('partial.footer')
</body>
Second index.blade.php : contain section('content').
#extends('layouts.master')
#section('content')
<div id="container">
<div id="news">
#yield('news')
</div>
<div id="apartment">
#yield('apartment')
</div>
</div> <!-- ./container -->
#endsection
Third: news.blade.php : this simple to show all news
#foreach($posts as $post)
#endforeach
Final file: apartment.blade.php : this simple to show all apartment.
#foreach($apartments as $apartment)
#endforeach
My route direct to master.blade.php.
My question is:
When I include news with #yield('news') in index.blade.php. It shows correct all news in my database.
But when I delete #yield('news') in index.blade.php. It also show news from my database (but it's lost css/js for that).
Why I deleted #yield('news'), it's should don't show any news on my page?
Seem Laravel Blade not support two #yield in #section. When I add only 1 row #yield('news') into index.blade.php file. It shows list news on my index page. When I continues add #yield('apartment'). Don't have any apartment shown on the index page. I certainly it has values when foreach to get data. I also test with HTML statics but don't have anything changes.
My route direct to master.blade.php.
Index extends master, so point your route to index view.
If sections continue to be yielded, after #yield removal, I think the framework is loading a cached view.
Try clearing the view cache php artisan view:clear or use php artisan --help view:clear for help.
Also #yield yields a section in a parent child relationship.
Change yields to include like #include('partial.news') if news are in partial folder or to whatever the path. Rendering could be manipulated using #isset or #empty statements or even #forelse loops.
Blade Control Structures.
If I have a layout called RightSideBar.blade.php in Laravel blade, one area yield('content') and the other yield('sidebar').
Is there a built in way to display a default partial if the view that is extending RightSideBar does not have a section('sidebar')?
I know you can pass a value by default, just wondering if there is a way to make default a partial.
Yes you can pass a default
Looking at the documentation
#yield('sidebar', 'Default Content');
Which basically puts a default output when the child template does not have #section('sidebar')
Most of the time we want multiple line default content, we can use this syntax:
#section('section')
Default content
#show
For example I have this in the template file:
#section('customlayout')
<article class="content">
#yield('content')
</article>
#show
You can see the difference between #show and #stop/#endsection: the above code is equivalent to the one below:
#section('customlayout')
<article class="content">
#yield('content')
</article>
#stop
#yield('customlayout')
In the other view files I can either set the content only:
#section('content')
<p>Welcome</p>
#stop
Or I can also set a different layout:
#section('content')
<p>Welcome</p>
#stop
#section('defaultlayout')
<div>
#yield('content')
</div>
#stop
The #stop is equivalent as the #endsection.
Although the docs specifies a default only as a string you can in fact pass a view
#yield('sidebar', \View::make('defaultSidebar'))
Laravel 5.2 added a #hasSection directive that checks if a section is defined in a view.
It's not mentioned in 5.3 or 5.4 docs for some reason.
#hasSection('sidebar')
#yield('sidebar')
#else
#yield('default-sidebar')
#endif
Tested in Laravel 8:
#yield can have default content as a second parameter. It can either be a string or a view file
// user-layout.blade.php
#yield('header', View::make('layouts.header'))
You can now override this "header" with section
#section('header')
<div>New Header</div>
#endsection
//// OR - you can also pass a view file as a second parameter //////
#section('header', View::make('layouts.new-header'))