Bài 35: Lấy dữ liệu sản phẩm cho danh mục

Bài 35: Lấy dữ liệu sản phẩm cho danh mục

  

-    Vào dự án frontend 

-    Vào 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('/','HomeController@index')->name('home');
Route::get('/category/{slug}/{id}',[
'as'=>'category.product',
'uses'=>'CategoryController@index',
]);
-    Vào terminal chạy lệnh :
php artisan make: controller CategoryController
<?php

namespace App\Http\Controllers;

use App\Category;
use App\Product;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
public function index($slug,$categoryId)
{
$categorys = Category::where('parent_id', 0)->latest()->get();
$categorysLimit = Category::where('parent_id',0)->take(3)->get();
$products = Product::where('category_id',$categoryId)->paginate(12);
return view('product.category.list',compact('categorysLimit','products','categorys'));
}
}
-    Vào siderbar.blade.php
<div class="col-sm-3">
<div class="left-sidebar">
<h2>Category</h2>
<div class="panel-group category-products" id="accordian"><!--category-productsr-->
@foreach($categorys as $category)
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
@if($category->categoryChildrent->count())
<a data-toggle="collapse" data-parent="#accordian" href="#sportswear_{{$category->id}}">
<span class="badge pull-right">
<i class="fa fa-plus"></i>
</span>
{{$category->name}}
</a>
@else
<a href="#">
<span class="badge pull-right">
</span>
{{$category->name}}
</a>
@endif
</h4>
</div>
<div id="sportswear_{{$category->id}}" class="panel-collapse collapse">
<div class="panel-body">
<ul>
@foreach($category->categoryChildrent as $categoryChilrent)
<li>
<a href="{{ route('category.product',['slug'=>$categoryChilrent->slug,'id'=>$categoryChilrent->id])}}">
{{$categoryChilrent->name}}
</a>
</li>
@endforeach
</ul>
</div>
</div>
</div>
@endforeach
</div><!--/category-products-->

</div>
</div>-    Vào HomeController.php
<?php

namespace App\Http\Controllers;

use App\Category;
use App\Product;
use App\Slider;
use Illuminate\Http\Request;

class HomeController extends Controller
{
public function index()
{
$sliders = Slider::latest()->get();
$categorys = Category::where('parent_id', 0)->latest()->get();
$products = Product::latest()->take(6)->get();
$productsRecommend = Product::latest('views_count','desc')->take(12)->get();
$categorysLimit = Category::where('parent_id',0)->take(3)->get();
return view('home.home',compact('sliders','categorys','products','productsRecommend','categorysLimit'));
}
public function test()
{
return view('test');
}
}
-    Vào resoucer => views => tạo foder product => tạo foder category => tạo file list.blade.php
@php
$baseUrl = config('app.base_url');
@endphp

@extends('layout.master')

@section('title')
<title>Home page</title>
@endsection

@section('css')
<link rel="stylesheet" href="{{asset('home/home.css')}}">
@endsection

@section('js')
<link rel="stylesheet" href="{{asset('home/home.js')}}">
@endsection

@section('content')
<section>
<div class="container">
<div class="row">
@include('components.siderbar')

<div class="col-sm-9 padding-right">
<div class="features_items"><!--features_items-->
<h2 class="title text-center">Features Items</h2>
@foreach($products as $product)
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<img src="{{$baseUrl . $product->feature_image_path}}" alt="" />
<h2>{{ number_format($product->price) }}VND</h2>
<p>{{$product->name}}</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2>{{ number_format($product->price) }}VND</h2>
<p>{{$product->name}}</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>
</div>
@endforeach
{{$products->links()}}
</div><!--features_items-->
</div>
</div>
</div>
</section>

@endsection

kết quả :
Link mã nguồn:
https://github.com/LESYHUNG2612/Laravel

Đăng nhận xét

0 Nhận xét

myadcash