【问题标题】:Laravel 5.1 - view data filter with relationshipLaravel 5.1 - 使用关系查看数据过滤器
【发布时间】:2016-09-18 14:14:03
【问题描述】:

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

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.

        }

【问题讨论】:

  • 好问题和好答案。拯救我的一天。谢谢。但我使用请求,然后检查我的查询字符串是什么,如果: if ($request->has('categories')) { return Categury::find($request->categories)->clinics; }

标签: php mysql laravel laravel-5.1


【解决方案1】:

您必须在Product模型语句的末尾添加get()方法才能得到指定的结果,如下所示:

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

dd($product_men);

这是我在laraveltinker工作的输出:

注意:如果您不使用get() 方法,它会返回 null 或不返回任何内容。

【讨论】:

    【解决方案2】:

    试试这个:

    Category::find(catagory_id)->products;
    

    例子:

    Category::find(1)->products;
    

    你也可以使用where子句:

    例子:

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

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

    【讨论】:

    • 感谢您的帮助!它工作是的!就像我添加了其他一个关系性别的类别一样,我怎样才能获得具有特定类别和特定性别的所有产品?例如,性别=男性。感谢您的帮助!
    • 如果我们想要有两个或更多的 find 或 where 再次过滤,我们如何使用 find 和 where?
    • @sabertababaeyazdi,你想使用多个 where 条件??
    • @YasinPatel 是的,我如何在两列上使用多查找()或查询。塔
    【解决方案3】:

    这应该可以解决问题:

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

    【讨论】:

      猜你喜欢
      • 2016-09-19
      • 2016-01-09
      • 2016-02-12
      • 2021-08-07
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 2021-11-15
      • 2021-03-09
      相关资源
      最近更新 更多