How To Optimize Laravel Code Eloquently With UpdateOrCreate()
byIn beginning of my Laravel learning, I did write more lines of code to perform my task than I write now. I started learning to adopt more eloquent ways of coding for Laravel encourages us to write. One simple task I performed by writing this many number of lines:
$place = PlaceModel::where('id', "132")->first();
if(isset($place))
{
$place->update(['name' => 'Place Name', 'address' => 'Place Address',]);
}
else
{
$place = PlaceModel::create([
'name' => 'Place Name',
'address' => 'Place Address',
]);
}
This many lines of code can be optimised to single statement of line in Laravel Eloquent way. This is ome of the ways to optimize eloquent query on Laravel: