Functionality of likes using cookies - laravel

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?

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

Unknown column 'subscriptions' in 'where clause'

I am using this subscription package https://github.com/overtrue/laravel-subscribe , so i want to get thread (post) associated with the space a user has subscribe to
$spaces = Space::whereHas('subscribers', function($query){
$query->where('subscribable_id', '=', 'user_id');
})->with('thread')->orderBy('id', 'desc')->paginate(10);
I have very recently come across a question asking almost the same thing. So I'll plagiarize my own answer in hopes it can be tailored for you as well.
To get the currently logged in user, we can use the auth()->user() helper function. Then we must get the spaces the current user is subscribed to, so if you have a relationship in your User model, it would be perfect and you can use the following:
$space = auth()->user()->spaces;
Otherwise, you can use your code from your question to get the spaces the user is subscribed to, but I'd still advise creating a method for this inside the user model:
$spaces = Space::whereHas('subscribers', function($query){
$query->where('subscribable_id', '=', 'user_id')->get();
We got the spaces, but what you need is the threads of those spaces, so we must add in more logic where we fetch the IDs of those spaces you follow, then retrieve all the threads that those spaces contain to then perform whatever queries you need at the end.
$spaceIds = $spaces->pluck('id');
$threads = Thread::whereIn('space_id', $spaceIds)->latest()->paginate(10);
I haven't tested this code since I don't have the same structure, so you may need to debug a thing or two to fix the queries but it should theoretically perfectly match your structure.

cookie laravel s

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?
Use Replace function on the string and replace " )." with ")."

I want to check duplication value during insert time without using unique keyword

i make one table for with some column with nullable.
i already tried with two different query. one using
Register_member::where('passport',$passport)->orWhere('adharcardnumber',$adharcardnumber)->get();
and second DB::table type query.
$row = Register_member::where('passport',$passport)->orWhere('adharcardnumber',$adharcardnumber)->get();
if (!empty($row))
{
return response()->json(["status"=>0, "message"=>"Adharcard or Paasport number already exit."]);
}
if (empty($row))
{
Register_member::insert(['first_name'=>request('first_name'), 'middle_name'=>request('middle_name'), 'last_name'=>request('last_name'), 'adharcardnumber'=>request('adharcardnumber'), 'ocipcinumber'=>request('ocipcinumber'), 'passport'=>request('passport'), 'birthday'=>request('birthday'),
'mobilecode'=>request('mobilecode'), 'mobilenumber'=>request('mobilenumber'), 'email'=>request('email'), 'address'=>request('address'), 'landmark'=>request('landmark'), 'area'=>request('area'),
'gender'=>request('gender'), 'pincode'=>request('pincode'), 'city_name'=>request('city_name'), 'state_id'=>request('state_id'), 'country_id'=>request('country_id'), 'sampraday'=>request('sampraday'), 'other'=>request('other'), 'sms'=>request('sms')]);
return response()->json(["status"=>1, "message"=>"Member register successful."]);
}
if adharcardnumber or passport number are exists in table, then nagetive response. if in both any one in unique then, insert data in table
Let me suggest you something which I think serve you as a good solution. You can use the unique with required and regex. In this way it will use the already recommended ways of Laravel which are the best.
As an example for your adhaar card,
the validation should look like this
$request->validate([
'adhaar ' =>['required','unique:users','regex:/\d{12}/'],
]);
where adhar is the filed name where adhaar number is entered. Be sure to use validator like this use Illuminate\Support\Facades\Validator;. Also $request is the instance of the Request.
Using the required prevent empty field submission and the regex will throw an error if the pattern is not matched completely. so I think it would be a better a way to handle the scenario.
the above solution will work in both adhaar and passport. But for the passport the regex will be different though.
Please note these are all demo examples, you might need to modify it according to your needs. I use https://www.phpliveregex.com/ for regex making and checking and it is good enough.
I hope you get an idea of how to begin but if you need more information then let me know in the comments.

I want to write a query where user Manually Select AND, OR, NOT

Very new to laravel and programming.
I want to write a code where user manually select AND OR NOT two times as given in Image, according to there are 3*3 means 9 conditions but I want one condition.
AS I cant manually write as follows.
//like this
$orwhere = '->orwhere';
$data = AllData::where('BookTitle', 'like', '%'.$request->get('book').$y)
$orwhere('Author', $middle, '%'.$request->get('book').'%')->get();
I know there would be some other way, feels like no Idea.

How to use model events with query builder in laravel

I'm using model events such as static::saving, static::saved, etc in my models' static function boot method, and that works great when users save new posts, but when I do something like this:
$post::where('id', $post_id)->update(array('published'=>1));
Updating in this way does not run those model events. My current solution is to just not use this method of updating and instead do:
$post = Post::find($post_id);
$post->published = 1;
$post->save();
But is there any way to make the model events work with the first example using query builder?
Model events will not work with a query builder at all.
One option is to use Event listener for illuminate.query from /Illuminate/Database/Connection.php. But this will work only for saved, updated and deleted. And requires a bit of work, involving processing the queries and looking for SQL clauses, not to mention the DB portability issues this way.
Second option, which you do not want, is Eloquent. You should still consider it, because you already have the events defined. This way you can use also events ending with -ing.

Resources