Ave
Ave

Reputation: 4430

How to show result in View when search in Laravel?

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.

Upvotes: 0

Views: 927

Answers (2)

Leo
Leo

Reputation: 7420

you are not using laravel blade engine. Rename view files like so index.blade.php

Upvotes: 1

Vision Coderz
Vision Coderz

Reputation: 8078

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

Upvotes: 4

Related Questions