Commit ec549257 authored by TTS Tran Viet Anh's avatar TTS Tran Viet Anh

get products and post by categories

parent a9ebfe3e
......@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Post;
use App\Models\PostCategory;
use App\Models\Product;
use App\Models\ProductCategory;
use Illuminate\Http\Request;
......@@ -16,7 +17,8 @@ class HomeController extends Controller
*/
public function index()
{
$productCategories = ProductCategory::noParent()->get();
$productCategories = ProductCategory::noParent()->with('children')->get();
$postCategories = PostCategory::noParent()->with('children')->get();
$latestDrops = Product::with('productAttributes')
->enabled()
->latest()
......@@ -32,16 +34,49 @@ public function index()
->latest()
->take(3)
->get();
return view('client.index', compact('latestDrops', 'productCategories', 'products', 'latestBlogs'));
return view('client.index', compact('latestDrops', 'productCategories', 'postCategories', 'products', 'latestBlogs'));
}
public function getProductsByCategory(Request $request)
public function getProductsAjax(Request $request)
{
$category = ProductCategory::findOrFail($request->cat);
$productsByCategory = Product::ofCategories($category->ids)
->enabled()
->latest()
->take(8)
->get();
return view('client.products_by_category', compact('productsByCategory'));
}
public function getProductsByCategory(Request $request, $slug)
{
$limit = $request->input('limit', 10);
$productCategories = ProductCategory::noParent()->with('children')->get();
$postCategories = PostCategory::noParent()->with('children')->get();
$category = ProductCategory::where('slug', $slug)->firstOrFail();
$productsByCategory = Product::with('productAttributes')
->enabled()
->ofCategories($category->ids)
->paginate($limit);
return view('client.product.index', compact('productCategories', 'postCategories', 'productsByCategory'));
}
public function getPostsByCategory(Request $request, $slug)
{
$limit = $request->input('limit', 10);
$productCategories = ProductCategory::noParent()->with('children')->get();
$postCategories = PostCategory::noParent()->with('children')->get();
$category = PostCategory::where('slug', $slug)->firstOrFail();
$recentPosts = Post::with('user')
->published()
->orderBy('publish_date', 'desc')
->take(5)
->get();
$postsByCategory = Post::with('user')
->published()
->ofCategories($category->ids)
->orderBy('publish_date', 'desc')
->paginate($limit);
return view('client.post.index', compact('productCategories', 'postCategories', 'recentPosts', 'postsByCategory'));
}
}
......@@ -2,6 +2,7 @@
namespace App\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
......@@ -23,6 +24,6 @@ public function register()
*/
public function boot()
{
//
Paginator::useBootstrap();
}
}
......@@ -105,7 +105,7 @@
$('#productCategory{{ $productCategory->id }}').click(function() {
$.ajax({
type: "GET",
url: "{{ route('productsByCategory') }}",
url: "{{ route('productsAjax') }}",
data: {
cat: {{ $productCategory->id }},
},
......
<li style="list-style: none;" value="{{ $postCategory->id }}">
<a class="dropdown-item" href="{{ route('postByCategory', ['slug' => $postCategory->slug]) }}">
{{ $postCategory->name }}
</a>
@if (count($postCategory->children) > 0)
<ul>
@foreach ($postCategory->children as $sub)
@include('client.post.categories', ['postCategory' => $sub])
@endforeach
</ul>
@endif
</li>
@extends('layout.app')
@section('content')
<div class="container my-5">
<div class="row">
<div class="col-4">
<h2>Recent Post</h2>
@foreach ($recentPosts as $recentPost)
<div class="row my-3">
<div class="col-6"><img src="{{ $recentPost->image }}" class="card-img-top" alt="..."></div>
<div class="col-6">
<span>{{ $recentPost->publish_date }}</span>
{{-- <h6 class="card-title">{{ $recentPost->title }}</h6> --}}
<p>Posted by: {{ $recentPost->user->name }}</p>
</div>
</div>
@endforeach
</div>
<div class="col-8">
<h2>Blog</h2>
@foreach ($postsByCategory as $postByCategory)
<div class="row my-3">
<div class="col-6"><img src="{{ $postByCategory->image }}" class="card-img-top" alt="...">
</div>
<div class="col-6">
<span>{{ $postByCategory->publish_date }}</span>
<h4 class="card-title">{{ $postByCategory->title }}</h4>
<p class="mt-3">Posted by: {{ $postByCategory->user->name }}</p>
<span>{{ $postByCategory->content }}</span>
</div>
</div>
@endforeach
<div class="paginator m-3">{{ $postsByCategory->links() }}</div>
</div>
</div>
</div>
@endsection
<li style="list-style: none;" value="{{ $productCategory->id }}">
<a class="dropdown-item" href="{{ route('productByCategory', ['slug' => $productCategory->slug]) }}">
{{ $productCategory->name }}
</a>
@if (count($productCategory->children) > 0)
<ul>
@foreach ($productCategory->children as $subCategory)
@include('client.product.categories', ['productCategory' => $subCategory])
@endforeach
</ul>
@endif
</li>
@extends('layout.app')
@section('content')
<div class="container">
<div class="row mx-1 my-5">
@foreach ($productsByCategory as $productByCategory)
<div class="col-3 my-2">
<div class="card">
<img src="{{ $productByCategory->image }}" class="card-img-top" alt="...">
<div class="card-body">
<h6 class="card-title">{{ $productByCategory->name }}</h6>
<p style="max-height: 50px; overflow: auto;">{{ $productByCategory->description }}</p>
<h5>{{ $productByCategory->price }}</h5>
<div class="color">
@foreach ($productByCategory->productAttributes as $productAttribute)
<span class="dot mx-"
style="height: 20px; width: 20px;background-color:{{ $productAttribute->color }}; border-radius: 50%;
display: inline-block;"></span>
@endforeach
</div>
</div>
</div>
</div>
@endforeach
</div>
<div class="paginator m-3">{{ $productsByCategory->links() }}</div>
</div>
@endsection
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<a class="navbar-brand" href="{{ route('home') }}">
<img src="{{ asset('assets') }}/images/logo.png" alt="" srcset="">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
......@@ -10,7 +10,7 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
<a class="nav-link active" aria-current="page" href="{{ route('home') }}">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
......@@ -29,9 +29,9 @@
Product
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
@foreach ($productCategories as $productCategory)
@include('client.product.categories', ['productCategory' => $productCategory])
@endforeach
</ul>
</li>
<li class="nav-item dropdown">
......@@ -40,9 +40,9 @@
Blog
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
@foreach ($postCategories as $postCategory)
@include('client.post.categories', ['postCategory' => $postCategory])
@endforeach
</ul>
</li>
<li class="nav-item">
......
<?php
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Route;
/*
......@@ -15,6 +16,8 @@
*/
Route::controller(HomeController::class)->group(function () {
Route::get('/', 'index');
Route::get('/productsByCategory', 'getProductsByCategory')->name('productsByCategory');
Route::get('/', 'index')->name('home');
Route::get('/productsAjax', 'getProductsAjax')->name('productsAjax');
Route::get('/san_pham/{slug}', 'getProductsByCategory')->name('productByCategory');
Route::get('/bai_viet/{slug}', 'getPostsByCategory')->name('postByCategory');
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment