Laravel 5.1-查看具有关系的数据过滤器

迭戈·塞斯佩德斯

我有一个问题要显示具有特定类别的产品,这是我的表迁移和模型:产品迁移:

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

模型产品

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

    }

}

类别迁移

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

型号类别

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

    }

}

我正在尝试显示所有具有category_id = 1的产品,此类别id = 1是我的T恤类别。我的控制器:

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.

        }
亚辛·帕特尔(Yasin Patel)

试试这个:

Category::find(catagory_id)->products;

例子 :

Category::find(1)->products;

您还可以使用where子句:

例子:

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

请参阅:https : //laravel.com/docs/5.1/eloquent-relationships

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel 5.1-查看具有关系的数据过滤器

来自分类Dev

Laravel 5筛选具有关系的主要数据

来自分类Dev

过滤器在Laravel 5

来自分类Dev

Laravel路由过滤器在Laravel 5

来自分类Dev

Laravel 5枢轴上的过滤器

来自分类Dev

Laravel 5获得具有关系和多重关系的用户

来自分类Dev

Laravel 5 Auth过滤器不起作用

来自分类Dev

jwd过滤器不起作用laravel 5

来自分类Dev

如何在Laravel中获取具有关系的数据以尝试查看Bill Products

来自分类Dev

错误试图在Laravel 5中获取具有关系和范围的非对象的属性

来自分类Dev

Laravel 5:尝试获取具有关系的非对象错误的属性

来自分类Dev

具有多个集合的Laravel多个过滤器表

来自分类Dev

Laravel雄辩的过滤器对hasMany关系

来自分类Dev

Laravel嵌套关系过滤器

来自分类Dev

Laravel雄辩的过滤器对hasMany关系

来自分类Dev

Laravel 查询过滤器审查与关系

来自分类Dev

R中具有nchar> 1的过滤器列表

来自分类Dev

Laravel 5嵌套关系

来自分类Dev

Laravel 5异常关系

来自分类Dev

Laravel 5查询关系

来自分类Dev

Laravel 5多态关系

来自分类Dev

Laravel 5与EAV的关系

来自分类Dev

Laravel 5关系

来自分类Dev

关系在Laravel 5

来自分类Dev

与Laravel 5的关系冲突

来自分类Dev

在视图laravel中传递具有关系的数据

来自分类Dev

在Laravel中检索具有关系的数据

来自分类Dev

Laravel多表数据过滤器

来自分类Dev

如何在具有关系总和的Laravel模型上使用紧急加载-当前获得多个查询(N + 1)