Tìm kiếm nhiều trường trong một bảng, search many tables (ok)

C:\xampp\htdocs\api\app\Http\Controllers\Api\V1\HomeController.php

<?php
namespace App\Http\Controllers\Api\V1;
use Illuminate\Http\Request;
use App\Models\CategoryPost;
use App\Models\Post;
class HomeController extends Controller {
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index() {
    $categories = CategoryPost::all();
    $posts = Post::with('category')->get();
    $posts_randon = Post::all()->random(6);
    $posts_views = Post::orderBy('views','DESC')->take('6')->get();
    return view("pages.main")->with(compact('categories','posts','posts_randon','posts_views'));
  }
  /**
   * Show the form for creating a new resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function create() {
    //
  }
  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request) {
    //
  }
  /**
   * Display the specified resource.
   *
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function show($id) {
    echo '<pre>';
      var_export($id);
    echo '</pre>';
    die("gggg");
  }
  /**
   * Display the specified resource.
   *
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function tim_kiem() {
    $keyword = $_GET['keyword'];
    $category_posts = Post::with("category")->where('title','LIKE','%'.$keyword.'%')->orWhere('short_desc','LIKE','%'.$keyword.'%')->get();
    $categories = CategoryPost::all();
    $posts = Post::with('category')->get();
    $posts_randon = Post::all()->random(6);
    $posts_views = Post::orderBy('views','DESC')->take('6')->get();
    return view("pages.search")->with(compact('categories','category_posts','posts','posts_randon','posts_views'));
  }
  /**
   * Show the form for editing the specified resource.
   *
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function edit($id) {
    //
  }
  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function update(Request $request, $id) {
    //
  }
  /**
   * Remove the specified resource from storage.
   *
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function destroy($id) {
    //
  }
}

C:\xampp\htdocs\api\resources\views\pages\search.blade.php

@extends('../layout')
@section('banner')
  @include('pages.banner')
@endsection

@section('main')
<div class="about">
  <div class="container">
    <div class="about-main">
      <div class="col-md-8 about-left">
        <div class="about-one">
          <p>Find The Most</p>
          <h3>Coffee of the month</h3>
        </div>
        <div class="about-tre">
          <div class="a-1">
            <div class="row">
              @foreach($category_posts as $post)
                <div class="row" style="margin: 10px 0;">
                  <div class="col-md-6 abt-left">
                    <a href="{{ url('bai-viet/' . $post->id) }}">
                      <img src="{{ url('images/' . $post->image) }}" alt="Image" />
                    </a>
                  </div>
                  <div class="col-md-6 abt-left">
                    <h6>{{ $post->category->title }}</h6>
                    <h3><a href="{{ url('bai-viet/'.$post->id) }}">{{ $post->title }}</a></h3>
                    <p>{{ $post->short_desc }}</p>
                    <label>{{ $post->post_date }}</label>
                  </div>
                </div>
              @endforeach
            </div>
          </div>
        </div>
      </div>
      <div class="col-md-4 about-right heading">
        <div class="abt-2">
          <h3>Bài viết xem nhiều</h3>
          @foreach($posts_views as $pviews)
            <div class="might-grid">
              <div class="grid-might">
                <a href="{{ url('bai-viet/' . $pviews->id) }}">
                  <img src="{{ url('images/' . $pviews->image) }}" class="img-responsive" alt="">
                </a>
              </div>
              <div class="might-top">
                <h4><a href="{{ url('bai-viet/' . $pviews->id) }}">{{ $pviews->title }}</a></h4>
                <p>{{ substr($pviews->short_desc, 0, 100) }}</p>
              </div>
              <div class="clearfix"></div>
            </div>
          @endforeach
        </div>
        <div class="abt-2">
          <h3>Bài viết ngẫu nhiên</h3>
          <ul>
            @foreach($posts_randon as $prandon)
              <li><a href="{{ url('bai-viet/' . $prandon->id) }}">{{ $prandon->title }}</a></li>
            @endforeach
          </ul>
        </div>
        <div class="abt-2">
          <h3>NEWS LETTER</h3>
          <div class="news">
            <form>
              <input type="text" value="Email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}" />
              <input type="submit" value="Subscribe">
            </form>
          </div>
        </div>
      </div>
      <div class="clearfix"></div>
    </div>
  </div>
</div>
@endsection

C:\xampp\htdocs\api\resources\views\layout.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Test</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="{{ asset('css/bootstrap.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ asset('css/chocolat.css') }}">
    <link rel="stylesheet" type="text/css" href="{{ asset('css/style.css') }}">
    <script src="{{ asset('js/jquery.min.js')}}"></script>
    <script type="text/javascript" src="{{ asset('js/move-top.js')}}"></script>
    <script type="text/javascript" src="{{ asset('js/easing.js')}}"></script>
    <script type="text/javascript">
      jQuery(document).ready(function($) {
        $(".scroll").click(function(event) {
          event.preventDefault();
          $('html,body').animate({ scrollTop: $(this.hash).offset().top }, 1000);
        });
      });
    </script>
  </head>
  <body class="antialiased">
    <div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0"> @if (Route::has('login')) <div class="hidden fixed top-0 right-0 px-6 py-4 sm:block"> @auth <a href="{{ url('/home') }}" class="text-sm text-gray-700 dark:text-gray-500 underline">Home</a> @else <a href="{{ route('login') }}" class="text-sm text-gray-700 dark:text-gray-500 underline">Log in</a> @if (Route::has('register')) <a href="{{ route('register') }}" class="ml-4 text-sm text-gray-700 dark:text-gray-500 underline">Register</a> @endif @endauth </div> @endif </div>
    <!--header-top-starts-->
    <div class="header-top">
      <div class="container">
        <div class="head-main">
          <a href="index.html"><img src="{{ url('images/logo-1.png') }}" alt="" /></a>
        </div>
      </div>
    </div>
    <!--header-top-end-->
    <!--start-header-->
    <div class="header">
      <div class="container">
        <div class="head">
          <div class="navigation">
            <span class="menu"></span>
            <ul class="navig">
              <li><a href="{{ url('/') }}" class="active">Home</a></li>
              @foreach ($categories as $category)
               <li><a href="{{ route('danh-muc.show',[$category->id]) }}">{{ $category->title }}</a></li> 
              @endforeach
              <li><a href="contact.html">Về chúng tôi</a></li>
            </ul>
          </div>
          <div class="header-right">
            <div class="search-bar">
              <form action="{{ url('tim-kiem') }}" method="get">
                @csrf
                <input type="text" placeholder="Nhập từ khóa" name="keyword">
                <input type="submit" value="">
              </form>
            </div>
            <ul>
              <li><a href="#"><span class="fb"> </span></a></li>
              <li><a href="#"><span class="twit"> </span></a></li>
              <li><a href="#"><span class="pin"> </span></a></li>
              <li><a href="#"><span class="rss"> </span></a></li>
              <li><a href="#"><span class="drbl"> </span></a></li>
            </ul>
          </div>
          <div class="clearfix"></div>
        </div>
      </div>
    </div>
    <!-- script-for-menu -->
    <!-- script-for-menu -->
    <script> 
      $("span.menu").click(function(){
        $(" ul.navig").slideToggle("slow" , function(){});
      });
    </script>
    <!-- script-for-menu -->
    <!--banner-starts-->
    @yield('banner')
    <!--banner-end-->
    <!--main-starts-->
    @yield('main')
    <!--main-end-->
    <!--slide-starts-->
    <div class="slide">
      <div class="container">
        <div class="fle-xsel">
          <ul id="flexiselDemo3">
            <li>
              <a href="#">
                <div class="banner-1">
                  <img src="{{ url('images/s-1.jpg') }}" class="img-responsive" alt="">
                </div>
              </a>
            </li>
            <li>
              <a href="#">
                <div class="banner-1">
                  <img src="{{ url('images/s-2.jpg') }}" class="img-responsive" alt="">
                </div>
              </a>
            </li>
            <li>
              <a href="#">
                <div class="banner-1">
                  <img src="{{ url('images/s-3.jpg') }}" class="img-responsive" alt="">
                </div>
              </a>
            </li>
            <li>
              <a href="#">
                <div class="banner-1">
                  <img src="{{ url('images/s-4.jpg') }}" class="img-responsive" alt="">
                </div>
              </a>
            </li>
            <li>
              <a href="#">
                <div class="banner-1">
                  <img src="{{ url('images/s-5.jpg') }}" class="img-responsive" alt="">
                </div>
              </a>
            </li>
            <li>
              <a href="#">
                <div class="banner-1">
                  <img src="{{ url('images/s-6.jpg') }}" class="img-responsive" alt="">
                </div>
              </a>
            </li>
          </ul>
          <script type="text/javascript">
            $(window).load(function() {
              $("#flexiselDemo3").flexisel({
                visibleItems: 5,
                animationSpeed: 1000,
                autoPlay: true,
                autoPlaySpeed: 3000,
                pauseOnHover: true,
                enableResponsiveBreakpoints: true,
                responsiveBreakpoints: {
                  portrait: {
                    changePoint: 480,
                    visibleItems: 2
                  },
                  landscape: {
                    changePoint: 640,
                    visibleItems: 3
                  },
                  tablet: {
                    changePoint: 768,
                    visibleItems: 3
                  }
                }
              });
            });
          </script>
          <script type="text/javascript" src="{{ asset('js/jquery.flexisel.js')}}"></script>
          <div class="clearfix"> </div>
        </div>
      </div>
    </div>
    <!--slide-end-->
    <!--footer-starts-->
    <div class="footer">
      <div class="container">
        <div class="footer-text">
          <p>© 2015 Coffee Break. All Rights Reserved | Design by <a href="http://w3layouts.com/" target="_blank">W3layouts</a> </p>
        </div>
      </div>
    </div>
  </body>
</html>

C:\xampp\htdocs\api\routes\web.php

<?php
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */
// Route::get('/', function () {
//     return view('pages.main');
// });
Route::get('/', [App\Http\Controllers\Api\V1\HomeController::class, 'index']);
// Route::get('/bai-viet/{id}', function () {
//     return view('pages.single');
// });
Route::get('/bai-viet/{id}', [App\Http\Controllers\Api\V1\BaivietController::class, 'show']);
Auth::routes();
Route::get('/home', [App\Http\Controllers\Api\V1\HomeController::class, 'index'])->name('home');
Route::get('/tim-kiem', [App\Http\Controllers\Api\V1\HomeController::class, 'tim_kiem']);

Last updated