Home > Enterprise >  Functionality of likes using cookies
Functionality of likes using cookies

Time:10-29

Let's say I have a like field in my model, now I want to make the functionality of adding a like using Cookie. For example, we have an article ID, we check in cookies, if there is no data, then we add like and set the time for 10 hours, for example, and so on, 10 hours pass, we check and again we can add like and set this time again. If the data is already there, then we do not add like and do not update the time.

Can anyone see some examples of this? Or where to start and how to implement it?

I read the documentation but did not quite understand how it should work

Let's get a value with a like field

$likes = Request::cookie('like');

Choosing the time

$minutes = 600;

And what's next, how can I make a check to add a like?

CodePudding user response:

If your updated_at column is not updated automatically on update (Eloquent do this by default), you need to do it, so the query can filter the row with correct time for update. You can use Carbon for easy time calculations.

 $likes = Request::cookie('like');
 $hours = 10;
    if($likes) {
        # Eloquent way
        Article::find($id)
            ->where('updated_at', '<', Carbon::now()->subHours($hours)->toDateTimeString())
            ->increment('like');
    }

If you want Query Builder way use this syntax:

# Query Builder way
DB::table('articles')
    ->where('id', $id)
    ->where('updated_at', '<', Carbon::now()->subHours($hours)->toDateTimeString())
    ->increment('like');

Not tested.

  • Related