Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

Models Introduction:


We create Models typically in the app directory, but you can place them anywhere that can be auto-loaded according to your composer.json file.

We can create our model by using the following command:

php artisan make:model bike

But if you wish to generate a database migration while generating the model, you can use the following code:

php artisan make:model bike --migration

php artisan make:model bike -m

 

 

Eloquent Model Conventions:

The below example (bike model) is use to retrieve and store information from our bikes database table:

Code:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Bike extends Model
{
//
}

 

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 bike model stores records in the bikes table. You may specify a custom table by defining a table property on your model as in the following code:

Code:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Bike extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'my_bikes';
}

 

Primary Keys:

Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention.
Apart from this, the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically.

 

Timestamps:

Eloquent provides created_at and updated_at columns on your tables automatically. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false:

Code:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Bike extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
}

 

If you need to customize the format of your timestamps, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

Code:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Bike extends Model
{
/**
* The storage format of the model's date columns.
*
* @var string
*/
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:

Code:

<?php
class Bike extends Model
{
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'last_update';
}

 

Database Connection

All  Eloquent models use the default database connection configured for your application. If you would like to specify a different connection for the model, use the $connection property:

Code:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Bike extends Model
{
/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'connection-name';
}

ASSIGNMENT : What are Laravel eloquent? MARKS : 10  DURATION : 10 minutes

ASSIGNMENT : How to create a record in Laravel using eloquent? MARKS : 10  DURATION : 10 minutes

SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X