Laravel从关系表

时间:2016-06-29 19:33:16

标签: database laravel return relational-database record

您好我想要返回包含所选类别的产品列表。 我有两个表由关系表产品连接:

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('article_id')->unsigned();
        $table->integer('category_id')->unsigned();
        $table->string('sn');
        $table->integer('quantity');
        $table->date('warranty');
        $table->timestamps();
    });

    Schema::table('products', function(Blueprint $table) {
        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('article_id')->references('id')->on('articles');
        $table->foreign('category_id')->references('id')->on('categories');
    });

} 

和表类别:

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('category_name');
        $table->timestamps();
    });
}

产品型号:

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

public function article(){
    return $this->belongsTo('App\Article');
}

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

和类别模型:

public function products(){
    return $this->hasMany('App\Product')->withTimestamps();
}

我在phpmyadmin中编写查询SQL:

SELECT
        products.id,
        products.user_id,
        articles.article_name,
        categories.category_name,
        products.sn,
        products.quantity,
        products.warranty,
        products.created_at,
        products.updated_at,
        SUM(quantity) AS ilosc
        FROM
        products
        LEFT JOIN
        categories ON products.category_id = categories.id
        LEFT JOIN
        articles ON products.article_id = articles.id
        GROUP BY articles.article_name
        ORDER BY id;

此查询适用于phpmyadmin,但我想在Laravel中编写metohod。 如何返回分配给所选类别的产品列表?我想补充说明所选类别是通过ID CATEGORY显示的方法传递的:

public function show($id){
    $categories = Category::find($id);

    return view('categories.showcategory', compact('categories'));
}

我不想要解决方案,只是暗示我该怎么做。我找到了这个主题enter link description here,但我仍然不知道该怎么做;)

我将查询SQL转换为查询laravel i文件CategoriesController:

public function show($id){
    $categories = Category::find($id);
    $productsList = DB::table('products')->select('products.id,
    products.user_id,
    articles.article_name,
    categories.category_name,
    products.sn,
    products.quantity,
    products.warranty,
    products.created_at,
    products.updated_at,
    SUM(quantity) AS ilosc')->from('products')->where('id' , '=' , $categories->id)->leftJoin('categories', 'products.category_id', '=', 'categories.id')->leftJoin('articles', 'products.article_id', '=', 'articles.id')->groupBy('articles.article_name')->orderBy('id')->get();

    return view('categories.showcategory', compact('categories', 'productsList'));
}

但是Laravel回复了错误:

  

SQLSTATE [42S22]:未找到列:1054未知列'products.id,'   在'字段列表'中(SQL:选择productsid,为``来自products   在categories上左加入productscategory_id = categoriesid   在articles上左加入productsarticle_id = articlesid   其中id = 1分组articlesarticle_nameid asc排序

在swatkins的帮助下我编写了方法:

public function show($id){
$categories = Category::find($id);
$productsList = Category::with(['products' => function ($query) {
                  $query->where('category_id', 20);
              }])->get();
return view('categories.showcategory', compact('categories', 'productsList'));
}

Laravel正确返回选定类别的产品,但仅限于我写ID类别时。在此方法中,ID类别等于20.

$ query-> where('category_id', 20 );

如何动态传递GET category_id ??

的值

1 个答案:

答案 0 :(得分:1)

您已经有了一个类别,您只需要急切加载产品关系:

public function show($id){
    $categories = Category::with(['products' => function ($query) {
                      $query->where('paid', true);
                  }])->find($id);

    // this will give you a child property on your category 
    // called 'products' that will be an array of associated 
    // Product objects

    return view('categories.showcategory', compact('categories'));
}

docs:https://laravel.com/docs/5.2/eloquent-relationships#eager-loading