Reputation: 15
I tried to accomplish a multiple where clause but failed. I want to check if the current Date of the user is equal to created_at and the second clause would be if the user has an entry by user id. I am working on a fitness app where the User can track the km he has run. And rather to create in a database table new entries just add them to the existing entries.
My Question is focused on the problem with the if clause because the variable $hasUserEntries
is not equal to null but there is no entry in the database table. It is empty.
I tried instead of using get()
I used first()
. But the problem is that I wasn't able to use Carbon::today()
or it was maybe that I use 3 values in the where clause which I need because I can't get the created_at
date only the Date YYYY-MM-DD. At the first()
Statement I used a hardcoded DateTime to check with created_at
and it worked. But I think I must not explain why hardcode is not optimal.
I searched on Stackoverflow and find that most answers were about using get()
. It is fine but why does my else
get triggered because from my point of view the database is empty(Null) so the if($hasUserEntries==null)
should be triggered.
public function add_km_to_progressbar(Request $request, TrackKM $trackKM)
{
$track_dailies = new TrackDaily();
$track_dailies->user_id = \Auth::user()->id;
$amount_runned_km = $request->input('amount_runned_km');
$amount_runned_km = (int)$amount_runned_km;
$track_dailies->amount = (int)$amount_runned_km;
$track_dailies->type = 1;
$check_requirements = [
'user_id'=>\Auth::user()->id,
'created_at'=>'Carbon::today()'
];
$hasUserEntries = DB::table('track_dailies')
->where('user_id','=',\Auth::user())
->where('created_at','>=',Carbon::today())
->get();
if ($hasUserEntries == null) {
return dd('does not work');
} else {
return dd('does work');
}
}
Expected Result should be the triggering of the if statement because if the database table is empty, the user id does not exist or the date of created_at is not the same as the current date then should be triggered if($hasUserEntries==null)
. I want to create there a new row if this condition == null in the database.
Actual Result if($hasUserEntries==null)
is true even though that the database table is empty. I think that the method get()
has values saved that are not related to the database.
I hope that you can help me out.
Upvotes: 1
Views: 86
Reputation: 902
$checkifuserExist= DB::table('track_dailies')->where('user_id','=',\Auth::user())->where('created_at','>=',Carbon::today())->count();
if($checkifuserExist>0)
{
//if user really exists
if( $hasUserEntries = DB::table('track_dailies')->where('user_id','=',\Auth::user())->where('created_at','>=',Carbon::today())->first())
{
//update the value of the amount
}
else{
//insert new record
}
}
else
{
// if the user does not exist, do something else...
}
remember theres no way the record for user_id would be null because first there has to be record inside the db
Upvotes: 0
Reputation: 902
i think what you should have done is checking to see if the record exist in the database before proceeding...
$checkifuserExist= DB::table('track_dailies')->where('user_id','=',\Auth::user())->where('created_at','>=',Carbon::today())->count();
if($checkifuserExist>0)
{
//proceed to query for fitness
}
else
{
//do something else...
}
with this, it will not throw error!
Upvotes: 1