what is Laravel Model ?

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.

php artisan make:model Book

php artisan make:model Book -m

php artisan make:model Book -mcr

It will create a model class Book in App\Book.php file. If you open your model class, it would look like this.

namespace App;
use Illuminate\Database\Eloquent\Model;

class Book extends Model {
public $timestamps = FALSE;
protected $fillable = ['title', 'author', 'publisher', 'publish_year','description'];

}

Create Books Migration

php artisan make:migration create_books_table

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBooksTable extends Migration
{
public function up() {
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('author');
$table->string('publisher');
$table->text('description');
$table->integer('publish_year');
$table->boolean('available');
$table->timestamps();
});
}

public function down() {
Schema::drop('books');
}
}

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.

namespace App;
use Illuminate\Database\Eloquent\Model;

class Book extends Model {

protected $fillable = ['title', 'author', 'publisher', 'publish_year'];
}



Eloquent send values of only fillable attribtues when inserting a new record in table. The id, available, created_at and updated_at columns would contain default values.

How to create database table

php artisan make:migration create_users_table

when you run this command then it create a CreateUsersTable file in your project database/migrations directory.


Running Migrations

php artisan migrate

when you run migrate command then it create a table in your database.


How to create model and migration table both?

File name : index.php

php artisan make:model Customer -m


######### OR ##########


php artisan make:model Customer --migration


it create a model with migration table


How to migrate specific model

File name : index.php


Database: Seeding

php artisan make:seeder UsersTableSeeder

Running Seeders

composer dump-autoload

php artisan db:seed
Or
php artisan db:seed --class=UserSeeder

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

File name : App/Providers/AppServiceProvider.php.php

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;

public function boot()
{
Schema::defaultStringLength(191);
}



Best Solution :-

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:

File name : index.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{

protected $table = 'my_customers';
}

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:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{

protected $primaryKey = 'customer_id';

}

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{

public $timestamps = false;

}

Date Format

et the $dateFormat property on your model. This property determines how date attributes are stored in the database,

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{

protected $dateFormat = 'U';

}

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{

const CREATED_AT = 'creation_date';
const UPDATED_AT = 'last_update';

}





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial