0

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!!🙌

4
  • I want to get the most viewed image in the PAST last 7 days. Only ONE? Or one per some group? If there is 2 or more images with the same max. views amount - do you need in all, or only one of them? Anycase solve this task using pure SQL at first... Commented Feb 28, 2020 at 10:22
  • Could you plz add your Image and ImageView models ? Commented Feb 28, 2020 at 10:24
  • i want only 1 image results whether they are equal or not. and yes it is working in pure sql the problem is implementing in LARAVEL. Commented Feb 28, 2020 at 10:25
  • @Foued MOUSSI they are empty that's why i did not add them Commented Feb 28, 2020 at 10:26

1 Answer 1

1

Given images & image_views tables

enter image description here

enter image description here

$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"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.