helpmyworld's avatar

Models and Controllers

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');
    }
}
0 likes
6 replies
jimmy0699's avatar
Level 3

take look at polimorphic relations

class Category extends Model
{
    
     public function categoryable()
    {     
    return $this->morphTo();    
    }
}

and

class Product extends Model
{
    
     public function category()
    {     
    return $this->morphMany('App\Category', 'categoryable');
    }
}

and

class Post extends Model
{
    
     public function category()
    {     
    return $this->morphMany('App\Category', 'categoryable');
    }
}
mikevrind's avatar

Place your models in namespaces (and folders for autoloading).

helpmyworld's avatar

@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);
    }
}
helpmyworld's avatar

@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.

helpmyworld's avatar

Thank You Guc43, allow me to work on it then i will revert to you with my success.

Please or to participate in this conversation.