Good day. I am building a eCommerce system that has categories. Also this system will allow me to blog.
In the app folder i will have two categories one for the blog one for the products. Is it workable to have two (files with the same name doing different things.)categories in the model folder(app)?
In the controllers also, should i have to CategoriesControllers? one for post one for products?
namespace App\Http\Controllers;
use App\Category;
use App\Product;
use Illuminate\Http\Request;
class CategoriesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories=Category::all();
return view('admin.category.index',compact(['categories','products']));
}
/**
* 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)
{
Category::create($request->all());
return back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($categoryId=null)
{
if(!empty($categoryId)){
$products=Category::find($categoryId)->products;
}
$categories=Category::all();
return view('admin.category.index',compact(['categories','products']));
}
/**
* 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)
{
$category=Category::find($id);
$category->products()->delete();
$category->delete();
return back();
}
}
Category for BLOG POSTS
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
//
public function postsCat()
{
return $this->belongsToMany('App\Post');
}
}
@Jimmy please bear with me. i am new to laravel and php. Please explain to me your examples up there so that i fully understand.
This is my category models for post blog and for product. they are the same but differs with functions.
1 is for post and 2 is for product.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
//
public function postsCat()
{
return $this->belongsToMany('App\Post');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable=['name'];
public function products()
{
// return $this->hasMany('App\Product');
return $this->hasMany(Product::class);
}
}
@mikevrind, please you also bear with me. What do you mean? i ask this because my models have two files which are the same but intended for different functions or purpose.
you have to add a 'categoryable_id' and a 'categoryable_type' column to you category table.
the 'categoryable_id' - column will contain product_id OR post_id
the 'categoryable_type' column will contain the classname of your model. post or product