Answer the question
In order to leave comments, you need to log in
How to return a model, but not write to the database using the firstOrNew, firstOrCreate or similar methods?
Good evening. Roughly speaking, there is something like this code.
public function GenerateUser($first_name, $last_name)
{
$isset = User::where('first_name', $first_name)
->where('last_name', $last_name)->first();
$response = null;
if (isset($isset)) {
$response = User::where('first_name', $first_name)
->where('last_name', $last_name)->first();
} else {
$response = new User();
$response->first_name = $first_name;
$response->last_name = $last_name;
$response->save();
}
return response()->json($response);
}
public function GenerateUser($first_name, $last_name)
{
$response = User::firstOrNew([
'first_name' => $first_name,
'last_name' => $last_name,
]);
$response->save();
return response()->json($response);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question