在Laravel发送数据到视图之前进行数据统计。

huangapple go评论71阅读模式
英文:

Counting data before sending to view in Laravel

问题

我有两张表,products(产品)和product_images(产品图片),现在我想显示至少有一张图片的产品。

在控制器中,我有一个简单的函数在ProductController中来获取所有的产品:

public function products(){
    $allProducts = $this->product->paginate(15);
    return view('frontend.pages.products',compact('allProducts'));
}

但是,我想要发送至少有一个图片的每个产品。

我应该怎么做才能实现这个目标?

编辑:

我已经在这两张表之间创建了关联,现在我该如何获得我想要的答案?

我在控制器中写了以下代码:

$allProducts = $this->product->whereHas('product_images', function ($query){
        $query->where();
    })->get();
英文:

I have two tables, products and product_images, now I want to show product which has at least one image.

In controller I have a simple function in ProductController to fetch all the products:

public function products(){
    $allProducts = $this->product->paginate(15);
    return view('frontend.pages.products',compact('allProducts'));
}

But, I want to send the products which has at least one image of each product.

What should I do to achieve that?

Edit:

I have created relationship between tables, now how can I get my desired answer?

I have written this in the Controller:

$allProducts = $this->product->whereHas('product_images', function ($query){
        $query->where();
    })->get();

答案1

得分: 0

Eloquent已经内置了这个功能。

示例

// 检索所有具有三个或更多评论的帖子...
$posts = App\Post::has('comments', '>=', 3)->get();

在你的情况下

在你的情况下,你可以将$allProducts行更改为

 $allProducts = $this->product()->has('product_image', '>=', 1)->paginate(15);

我没有测试上面的代码。

有关此主题的更多信息,请参阅文档。
https://laravel.com/docs/6.x/eloquent-relationships#querying-relationship-existence

英文:

Eloquent has this built in.

Example

// Retrieve all posts that have three or more comments...
$posts = App\Post::has('comments', '>=', 3)->get();

In your case

In your case you could change the $allProducts line to be

 $allProducts = $this->product()->has('product_image', '>=', 1)->paginate(15);

I didn't test the code above.

See documentation for more information on this topic.
https://laravel.com/docs/6.x/eloquent-relationships#querying-relationship-existence

答案2

得分: 0

假设表结构为

产品

    -id
    -名称

产品图片

    -id
    -产品_id
    -网址 //您所需的任何列
$product_ids = DB::table('product')
                      ->join('product_image','product_image.product_id','=','product.id')
                      ->select('product.id','product.name')
                      ->groupBy('product.id')
                      ->get();
    
$product_count = count($product_ids);
英文:

Assuming table schema

product

	-id
	-name

product_image

    -id
    -product_id
    -url //any columns you needed


$product_ids = DB::table('product')
                  ->join('product_image','product_image.product_id','=','product.id')
                  ->select('product.id','roduct.name')
                  ->groupBy('product.id')
                  ->get();

$product_count = count($product_ids);

答案3

得分: 0

你可以使用selectRaw:

$products = Product::leftJoin('product_images', function ($join) {
    $join->on('products.product_id', '=', 'product_images.product_id');
})->selectRaw("products.product_id i, count(product_images.id) c")
    ->groupBy('products.product_id')
    ->where('c', '>', 3)
    ->get();
英文:

You can use selectRaw:

$products = Product::leftJoin('product_images',function ($join){
        $join->on('products.product_id','=','product_images.product_id');
    })->selectRaw("products.product_id i, count(product_images.id) c")
        ->groupBy('products.product_id')
        ->where('c','>=',3)
        ->get();

答案4

得分: 0

获取答案查询关系:

查询将如下所示:

$allProducts = $this->product->has('Images')->paginate(15);

但是,请确保您在模型中创建了以下关系:

public function Images(){
    return $this->hasMany('\App\Models\ProductImages');
}

我使用了hasMany关系,因为一个产品可以有多个图像。

英文:

Getting answer querying realtionship:

The query will look like following:

$allProducts = $this->product->has('Images')->paginate(15);

But, make you sure you have created relation in Model like the following:

public function Images(){
    return $this->hasMany('\App\Models\ProductImages');
}

I have used hasMany relationship because one product can have multiple images.

huangapple
  • 本文由 发表于 2020年1月3日 19:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定