dimanche 22 mai 2016

Laravel 5.1 - Filter more one data with relationship

i have a problem to show my products that have a specific category AND specific gender, 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->integer('gender_id')->unsigned();


            $table->foreign('gender_id')
              ->references('id')
              ->on('genders')
              ->onDelete('cascade');

            $table->timestamps();

model product

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\User;

use dixard\Category;

use dixard\Gender;

use dixard\OrderItem;

use dixard\Color;

class Product extends Model
{

    protected $table = 'products';

    protected $fillable = 

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

    ];



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


    }

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

    }

    public function gender() {
        return $this->belongsTo('dixard\Gender');
    }



    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);


        });

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');

    }

}

Gender migrations

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGendersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('genders', function (Blueprint $table) {
            $table->increments('id');

            $table->text('gender');


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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('genders');
    }
}

Gender Model

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\Product;

class Gender extends Model
{
    protected $table = 'genders';


    // gli dico che voglio scrivere questo campi
    protected $fillable = [

    'gender',


    ];
    public $timestamps = false;

    public function products() {

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

    }
}

I'm trying to show all products that have category_id = 1 (t-shirt category) AND gender_id = 2 (Woman gender), but i have some problem, this is my controller:

use dixard\Product;
use dixard\Category;
use dixard\Gender;

class TshirtController extends Controller
{

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

            $products = $category->products();
            return view('store.shop.men',compact('products'));

            // It filter only category and not gender woman

        }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire