I have a couple of tables with special names that I imported through a SQL file (this is required, it's for school, all tables must have our initials before the actual table name), I have seen that most of the tables through Laravel are created with a migration, I want to use Eloquent for the CRUD but I don't want to use migrations, what's the approach for it? what should I investigate to make this? I'm a bit confused.
2 Answers
In laravel migrations need for make your databse easly. If you already make database you can use Eloquent model like this
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class School extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'school';
}
https://laravel.com/docs/6.x/eloquent#eloquent-model-conventions
-
if i want to make for example an insert using eloquent do i have to declare fillables with the same names as my columns? like:
protected $fillable = ['my1', 'my2'];
Commented Dec 3, 2019 at 2:03 -
if you want to use
create()
orupdate()
method then yes Commented Dec 3, 2019 at 2:08 -
other case you can use
$model = new Model(); $model->my1 = 'val1'; $model->my2 = 'val2'; $model->save();
Commented Dec 3, 2019 at 2:09
Inside your model you can define the table for that model. for example if you have a table named a_school, in the A_SchoolModel.php you can define the table as below.
protected $table = 'a_school';
Also if the database engine for app is InnoDB, go to config->database and set mysql => [ 'engine' => 'innoDB',]