Because of this line, you always get query builder instance, never null.
$user = User::where('name', '=', $noSpacesName);
You should execute the query..
$user = User::where('name', '=', $noSpacesName)->first();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So I have this function that used to work great couple days ago but now out of nowhere, it stopped working correctly even though I didn't change any code in the whole app. I am confused on why it stopped working on both local machine and hosting server. Here's the code of the function (It is supposed to add a record if the username is not found in the database and not add if found) :
public function addipH(Request $request)
{
$noSpacesIP = str_replace(' ', '', $request->HackersIP);
$noSpacesName = str_replace(' ', '', $request->name);
$user = User::where('name', '=', $noSpacesName);
if ($user == null) {
$nIP = new hackers;
$nIP->HackersIP = $noSpacesIP;
$nIP->name = $noSpacesName;
$nIP->description = $request->description;
$nIP->rep = $request->rep;
$nIP->miners = $request->miners;
$nIP->save();
Session::flash('flash_message', 'IP successfully added!');
}
else
{
Session::flash('flash_message2', 'IP is for a current user, it can not be added!');
}
$name = hackers::all();
return view('manageH')->with('name',$name);
}
I am always getting the second flash message without any addition to the db(mySql). As I said before, It was working completely right couple days ago but all of a sudden stopped both locally and on host. Thanks for any help.
Because of this line, you always get query builder instance, never null.
$user = User::where('name', '=', $noSpacesName);
You should execute the query..
$user = User::where('name', '=', $noSpacesName)->first();
Please or to participate in this conversation.