简体   繁体   中英

Laravel sub-query

I have a database that looks like this:👇👇

images 🌅

| id            | name          | src         | status    |
| ------------- |---------------| ------------| ----------|
| 1             | nice sun set  | 1020288.jpg | published |
| 2             | poor sun set  | 1120288.jpg | published |
| 3             | best sun set  | 3120288.jpg | deleted   |
| ------------- |---------------| ------------| --------- |

image_views 👀

| id            | image_id      | browser_id🌎 | created_at         |
| ------------- |---------------| ------------ | ------------------ |
| 1             | 2             | 1020288e3221 |2020-02-23 13:55:11 |
| 2             | 1             | 1120288221ww |2020-02-27 13:50:51 |
| ------------- |---------------| ------------ | ------------------ |

Now in my laravel App,

I want to get the most viewed image in the PAST last 7 days.

( i want to have a column of image_views and those views 👀 should be grouped by browser id ).

so here is what i have tried:👇👇

$image_views = DB::table('image_views')
                        ->selectRaw('count(*) as view_count')
                        ->where(function($query){
                            $query->where('image_views.image_id', 'images.id');
                            $query->whereDate('image_views.created_at', '>=',  Carbon::now()->subDays(7)->toDateTimeString() );
                        });

$image = Image::select(['images.*', DB::raw('(' . $image_views->toSql() . ') as views ')])
->limit(1)
->orderBy('views', 'desc')
->where('images.status','published')
->mergeBindings($image_views)
->get();

return $image;

So unfortunately the posted above☝☝ code does not work😩

It only return blank results.

By the way i have lot of views in image_views table starting from 2⃣0⃣1⃣9⃣ to now , just that i couldn't post all here..


THE FUNNY THING IS THAT IF I CONVERT IT TO SQL AND PASTE IT IN PHPMYADMIN IT WORKS LIKE A CHARM

return $image->toSql();
//->mergeBindings($image_views)
//->get();

PLEASE SOMEONE TELL ME WHAT I AM DOING WRONG IN LARAVEL!!🙌

Given images & image_views tables

在此处输入图片说明

在此处输入图片说明

$mostViewdImage = DB::table('image_views')
->join('images', 'image_views.image_id', '=', 'images.id')
->select('browser_id', DB::raw('count(image_id) as occurrence'), 'images.*')
->where('image_views.created_at', '>=', Carbon::now()->subDays(7)->toDateTimeString())
->groupBy('image_id', 'browser_id')
->orderByRaw('occurrence DESC')->first();

dump($mostViewdImage);

//Output

"select `browser_id`, count(image_id) as occurrence, `images`.* from `image_views` inner join `images` on `image_views`.`image_id` = `images`.`id` where `image_views`.`created_at` >= ? group by `image_id`, `browser_id` order by occurrence DESC limit 1" (2.02 s)

{#261 ▼
    +"browser_id": "1020288e3221"
    +"occurrence": 2
    +"id": 2
    +"name": "poor sun set"
    +"src": "1120288.jpg"
    +"status": "published"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM