Yes it is possible, as long as your connection to the database is setup correctly. Make sure you also use this trait, as I see your id is an uuid: https://laravel.com/docs/9.x/eloquent#uuid-and-ulid-keys
Dec 22, 2022
4
Level 1
Create table in db with query and then use eloquent
Hello everyone.
If someone creates in a database (oracle) the tables through queries. Then it is possible not to do migrations but to insert and update the data of the tables using eloquent?
For example, If in the database I create this table:
create table "users"
(
"id" uuid not null,
"prs_id" varchar(255) not null,
"check" varchar(255) not null,
"password" varchar(255) not null,
"name" varchar(255) not null,
"surname" varchar(255) not null,
"created_at" timestamp(0) without time zone null,
"updated_at" timestamp(0) without time zone null,
"deleted_at" timestamp(0) without time zone null
);
alter table "users"
add primary key ("id");
alter table "users"
add constraint "ciudadanos_prs_id_unique" unique ("prs_id");
alter table "users"
add constraint "ciudadanos_check_unique" unique ("check");
Then in the controller method, Can I do a save(insert in the database)?
public function singup(Request $request)
{
$validated = $this->validate($request, [
'check' => 'required',
'name' => 'required',
'surname' => 'required',
'password' => 'required',
]);
$user = new User();
$user->check = $validated['check'];
$user->name = $validated['name'];
$user->surname = $validated['surname'];
$user->password = bcrypt($validated['password']);
$user->save();
}
I understand that the model must be consistent with the query to create table
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable =[
'id',
'prs_id',
'check',
'name',
'surname',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Please or to participate in this conversation.