Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

julianov's avatar

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',
    ];
}
0 likes
4 replies
julianov's avatar

@Nakov thanks and in the model, I must delete the id?

 protected $fillable =[ 
    'id',
    'prs_id',
    'check',
    'name',
    'surname',
    'password',
    ];

So deleting the id it will be:

class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; use HasUuids;

protected $fillable =[ 
    'prs_id',
    'check',
    'name',
    'surname',
    'password',
];

protected $hidden = [
    'password',
    'remember_token',
];

protected $casts = [
    'email_verified_at' => 'datetime',
];

}

It's correct?

Nakov's avatar

@julianov Yes, the id should be out of the fillable array. The trait will take care of it.

1 like
webrobert's avatar

Don't fight the framework.

Building and tearing down databases is one of the cool features of Laravel. It's all built in.

Use migrations!

Please or to participate in this conversation.