site ecommerce ب laravel 7 الجزء الثاني
فهاد الجزء الثاني من site ecommerce ب laravel 7 غادي نزيدو ل model Category ول controller CategoryController ومن بعد غادي نزيدو login système ول HomeController.
نظرة سريعة بالفيديو
1- إضافة model Category
غادي تزيد model Category فيه la relation مع les produits أيضا كنغير القيمة ديال البحث من id ل slug.
الكود ديال الملف هو :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
//
protected $fillable = ["title", "slug"];
public function getRouteKeyName()
{
return "slug";
}
public function products()
{
return $this->hasMany(Product::class);
}
}
2- إضافة ل controller CategoryController
من بعد زيد ل controller CategoryController لي غادي نخليهلك كتمرين زيد فيه les fonctions ديال الإضافة التعديل والحذف.
الكود ديال CategoryController هو :
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* 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 \App\Category $category
* @return \Illuminate\Http\Response
*/
public function show(Category $category)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function edit(Category $category)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function destroy(Category $category)
{
//
}
}
3- إضافة login & logout وتعديل الملف HomeController
منبعد باش نزيد login و logout زيد هاد les commandes ف cmd :
composer require laravel/ui:^2.4
php artisan ui vue --auth
دبا من بعد متزاد غادي تمشي ل HomeController وتزيد عليه تعديلات باش ن afficher les produits فل home page.
الكود ديال HomeController بعد التعديل هو :
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Product;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
// $this->middleware('auth');
}
/**
* Show products.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home')->with([
"products" => Product::latest()->paginate(10),
"categories" => Category::has("products")->get(),
]);
}
/**
* Show products by category.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getProductByCategory(Category $category)
{
$products = $category->products()->paginate(10);
return view('home')->with([
"products" => $products,
"categories" => Category::has("products")->get(),
]);
}
}