Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.1-使用关系过滤多个数据_Php_Laravel_Eloquent_Laravel 5.1 - Fatal编程技术网

Php Laravel 5.1-使用关系过滤多个数据

Php Laravel 5.1-使用关系过滤多个数据,php,laravel,eloquent,laravel-5.1,Php,Laravel,Eloquent,Laravel 5.1,我无法显示具有特定类别和特定性别的产品,这是我的表和模型: 产品迁移: Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name', 255); $table->string('slug'); $table->text('des

我无法显示具有特定类别和特定性别的产品,这是我的表和模型: 产品迁移:

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();
模型产品

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

    }

}
模型类别

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

    }

}
性别移徙

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

您正在从DB中提取性别模型,但没有将其用于任何用途。 试着这样做:

        $products = $category->products()->where(['gender_id' => $gender->id])->get();
将此添加到产品模型中。 使用laravel的范围

product.php

public function scopeCategory($query, Category $category)
{
    if($category) {
        return $query->whereCategoryId($category->id);
    }
    return $query;
}

public function scopeGender($query, Gender $gender)
{
    if($gender){
        return $query->whereGenderId($gender->id);
    }
    return $query;
}
t衬衫控制器中

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

    $products = Product::category($category)->gender($gender)->get()->toArray();

    return view('store.shop.men',compact('products'));
}
此外如果要处理反向关系,您可能需要将以下内容添加到Product.php

public function categories()
{
    return $this->belongsTo('dixard\Category.php');
}

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

男士女式t恤?当然可以?非常感谢!是的,它可以工作,所以,你认为这是从我的表中筛选关系的最好方法吗?谢谢你的帮助!我从拉威尔身上学到的一件事是,做任何事情都有十几种方法,但没有一种是“最好的”或“正确的”方法。您可以将整个过程作为一个链式单行查询来完成,也可以执行一系列简单的查询。做你觉得合适的事情。我得到这个错误:TshirtController.php第33行中的ErrorException:不应该静态调用非静态方法dixard\Product::category(),假设$this来自不兼容的上下文,我更正了-->$products=products with Product@Diego182 oops..应该是Product::not products::typo
public function scopeCategory($query, Category $category)
{
    if($category) {
        return $query->whereCategoryId($category->id);
    }
    return $query;
}

public function scopeGender($query, Gender $gender)
{
    if($gender){
        return $query->whereGenderId($gender->id);
    }
    return $query;
}
public function men_tshirt()
{
    $category = Category::whereName('t-shirt')->orderBy('id', 'desc')->first();
    $gender = Gender::whereGender('woman')->orderBy('id', 'desc')->first();

    $products = Product::category($category)->gender($gender)->get()->toArray();

    return view('store.shop.men',compact('products'));
}
public function categories()
{
    return $this->belongsTo('dixard\Category.php');
}

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