Eloquent is ORM that is used by Laravel.
Create Model Class
By default the model classes are located in 'App' folder. User.php
but you are free to place them anywhere that can be auto-loaded according to your composer.json file.
its already created model class by Laravel. Migration for users table are also created created by default, as user is a part of many projects. Lets create another model class Book along with migration so that we can also create a table to store books records by running the migration.
Create Books Migration
php artisan make:migration create_books_table
Defining Fillable Attributes in Book Model Class
By default all of the Book attributes are open to initialize to insert new record in books table. Laravel discourge keeping all attribtues open or fillable. So we define an attribute called fillable in the model class that contains the names of table columns that can be set when inserting a new record. Columns which are not fillable are not effected when adding new record i.e. default values are used for those columns. For example, we don't want user to send the value of id attribute as its auto generated primary key.
How to create database table
when you run this command then it create a CreateUsersTable file in your project database/migrations directory.
Running Migrations
when you run migrate command then it create a table in your database.
How to create model and migration table both?
it create a model with migration table
How to migrate specific model
Database: Seeding
Running Seeders
php artisan migrate errors :
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: a lter table `users` add unique `users_email_unique`(`email`))
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email)) [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
change to the default database character set, and it’s now utf8mb4
set a default string length in boot method.
use Illuminate\Support\Facades\Schema;please specify the string size.
$table->string('email',64)->unique();
Table Names
plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Customer model stores records in the Customers table. You may specify a custom table by defining a table property on your model:
Primary Keys
Eloquent assume that each table has a primary key column named id by default. but You may also define a protected $primaryKey property to override this convention:
Timestamps
By default, Eloquent expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false
Date Format
et the $dateFormat property on your model. This property determines how date attributes are stored in the database,
If you need to customize the names of the columns used to store the timestamps, you may set the CREATED_AT and UPDATED_AT constants in your model
Trending Tutorials