I have a Laravel application made by someone else.
In the controller I have this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Result;
class StoreInDB extends Controller
{
function store($uuid, $session){
// Insert data
$r = new Result;
if($r->updateOrInsert(['user_id' => $uuid], ['user_id' => $uuid, 'answers' => $session])){
return true;
}
else{
return false;
}
}
}
In the Model Result I have this:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Result extends Model
{
//
}
Why does it work? There is no $fillable to tell which column can be used to insert.
Datas are inserted. But why? The only issue I have is that created_at and updated_at are null.
updateOrInsert uses the query builder and not eloquent, therefore it wont trigger model events or respect the
guarded
orfillable
properties and won't populate thecreated_at
andupdated_at
columns.If you want to use the mentioned eloquent features you should use updateOrCreate.
Something like this:
Note that
updateOrCreate
returns a model instance andupdateOrInsert
returns aboolean
.updateOrCreate:
updateOrInsert: