2
votes

i have a problem to show my products that have a specific category, this is my tables migration and models: Products migration:

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->string('slug'); 
            $table->text('description');
            $table->string('extract', 300);
            $table->decimal('price', 5, 2);
            $table->string('image', 300);
            $table->boolean('visible');
            $table->integer('user_id')->unsigned();

            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');

            $table->integer('category_id')->unsigned();

            // relations  
            $table->foreign('category_id')
                  ->references('id')
                  ->on('categories')
                  ->onDelete('cascade');

            $table->timestamps();

model product

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\User;

use dixard\Category;

use dixard\OrderItem;

use dixard\Color;

class Product extends Model
{

    protected $table = 'products';

    protected $fillable = 

    [
    'name',
    'slug',
    'description',
    'extract',
    'image',
    'visible',
    'price',
    'category_id',
    'user_id'

    ];



    public function user() {
            return $this->belongsTo('dixard\User');


    }

    public function category() {
            return $this->belongsTo('dixard\Category');

    }

    public function OrderItem() {
            return $this->belongsTo('dixard\OrderItem');

    }


    public function Color() {
            return $this->belongsTo('dixard\Color');

    }

}

Category migrations

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255)->unique();

            $table->string('slug');
            $table->text('description');

            $table->string('color', 30);

            //$table->timestamps();
        });

Model category

use dixard\Product;

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [

    'name',
    'slug',
    'description',
    'color',


    ];
    public $timestamps = false;

    public function products() {

        return $this->hasMany('dixard\Product');

    }

}

I'm trying to show all products that have category_id = 1, this category id=1 is my t-shirt category. My controller:

use dixard\Product;
use dixard\Category;

class TshirtController extends Controller
{

       public function men_tshirt()
        { 
            $category = Category::where('name', '=', 't-shirt')->orderBy('id', 'desc')->first();
            $product_men = Product::where('category_id','=', $category->id)->orderBy('id', 'desc');

            dd($product_man) OR
            return view('store.shop.men',compact('product_men'));

            // It doesnt work, doesnt show me nothing.

        }
3
great question and great answer . save my day. thanks. but i use request and then check what is my quesry string use this if: if ($request->has('categories')) { return Categury::find($request->categories)->clinics; }saber tabatabaee yazdi

3 Answers

2
votes

Try this:

Category::find(catagory_id)->products;

Example :

Category::find(1)->products;

You can also use where clause :

Example:

Category::where('name', 't-shirt')->products;

Refer : https://laravel.com/docs/5.1/eloquent-relationships

1
votes

You must add get() method at the end of Product model statement to get the specified result like this:

$product_men = Product::where('category_id','=', $category->id)->orderBy('id', 'desc')->get();

dd($product_men);

and here is the output which I work in laravel tinker:

enter image description here

NOTE: If you not use get() method it return null or nothing.

1
votes

This should do the trick :

 $category = Category::where('name', '=', 't-shirt')->orderBy('id', 'desc')->first();
 $product_men = $category->products()->orderBy('id', 'desc')->get();