Laravel 5 Tutorial for beginners : how to create Defining Models, Retrieving Models Eloquent ORM (Object Relation Maping) Create, Read, Update, Delete in Laravel 5.3
Eloquent ORM (Object Relation Maping) - Eloquent ORM (Object Relation efficiency of mapping) is a feature from Laravel, using Eloquent we can easily Run a Database Queries, for example to create Insert, Update, Delete, and Read Data Queries.
Please Read :
In the default installation Laravel 5.3, you can see your "User" model at \app\User.php folder.
Model User.php in Laravel 5.3
We can create a Models using "Artisan" CLI in laravel, that is an example :
so, we will create a new "Biodata" model, following this command
your Biodata model will placed at \app\Biodata.php
Before, we have a database (MySQL Database) and "biodata" tables,
So you must edit you model and configure with your database, just edit your biodata models look like above :
at the Routes file at routes\web.php modife default code following this example source code
"Biodata Created" and see at your database at the Biodata Tables :
now, we will add new route to our routes\web.php, Just add this following source code
Here Complete Routes file (Insert, Read, Update, Delete)
web.php
Stay update and subscibe us To create more project using CRUD basics in laravel.
See you Next Lessons.......
Please Read :
In the default installation Laravel 5.3, you can see your "User" model at \app\User.php folder.
Model User.php in Laravel 5.3
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
We can create a Models using "Artisan" CLI in laravel, that is an example :
php artisan make:model
so, we will create a new "Biodata" model, following this command
c:\server\htdocs\k13new>php artisan make:model Biodata
Model created successfully.
your Biodata model will placed at \app\Biodata.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Biodata extends Model
{
//
}
Before, we have a database (MySQL Database) and "biodata" tables,
So you must edit you model and configure with your database, just edit your biodata models look like above :
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Biodata extends Model
{
protected $primaryKey = 'id';
protected $table = 'biodata';
protected $fillable = array('titles','descriptions', 'created_at', 'updated_at');
}
- namespace App; defines the model namespace
- class Biodata extends Model defines the category class that extends the Model
- protected $primaryKey = 'id'; explicitly defines the primary key field name. The default is id so in this case it’s not really necessary to specify it. Personally I prefer setting the name explicitly.
- protected $table = 'biodata'; explicitly defines the table name. The default is the plural form of the model name.
- protected $fillable = array('titles', 'descriptions', 'created_at,'updated_at''); defines field names that can be mass assigned. This is a security measure that ensures only authorized fieldnames are affected
Eloquent ORM INSERT
now, we will create Method insert to save our data into database table, we will use "biodata" models to add data in the database.at the Routes file at routes\web.php modife default code following this example source code
<?php
use AppBiodata;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::get('/insert', function() {
Biodata::create(['titles'=>'First Post','descriptions'=>'This is descriptions']);
return 'Biodata Created';
});
- Biodata::create(); will executes the create static function of the model.
- return 'Biodata Created'; output if biodata has been created.
"Biodata Created" and see at your database at the Biodata Tables :
Eloquent ORM READ
To read all data from any tables or columns from our database, you can use all(); Method.now, we will add new route to our routes\web.php, Just add this following source code
// read routes
Route::get('/read', function() {
$biodata = new Biodata();
$data = $biodata->all();
// looping here
foreach ($data as $post) {
echo $post->id . '-' . $post->titles . '<br>';
}
});
- new Biodata(); will create an instance variable of Biodata model
- $biodata->all(); will calls the method of the model to show all data from database.
- foreach ($data as $list){.......} you can do loops in the Views folder, but for now we will in the routes file and show it to te browser without views, loops through the returned results and displays the rows in the browser.
Eloquent ORM UPDATE
than, we will create the Update statement using Eloquent ORM in Laravel, the update data will using id field, following this source code and add to your Routes file routes\web.php
// update routes
Route::get('/update', function() {
$biodata = Biodata::find(2);
$biodata->titles = 'The First Post Title was Update';
$biodata->save();
$data = $biodata->all();
// looping here
foreach ($data as $list) {
echo $list->id . ' ' . $list->titles . '<br>';
}
});
- Biodata::find(2); will call the find function from the model and pass in 2 as the primary key parameter value.
- $biodata->titles= 'The First Post Title was Update'; assigns the value The First Post Title was Update to the filed name.
- $category->save(); will saves the changes made to the our database
- $data = $biodata->all(); will show all data from database
- foreach ($data as $list){.....} will loops through all the records and display the value in the web browser.
Eloquent ORM DELETE
How to delete a biodata using id? we will create new routes in routes\web.php
// delete route
Route::get('/delete', function() {
$biodata = Biodata::find(1);
$biodata->delete();
$data = $biodata->all();
foreach ($data as $list) {
echo $list->id . ' - ' . $list->titles . '<br>';
}
});
- Biodata::find(1); will call the find function from the model and pass in 1 as the primary key parameter value.
- $biodata->delete(); will deletes the record that was retrieved via the find method, that is id=1
- $data = $biodata->all(); will show all data from database
- foreach ($data as $list){.....} will loops through all the records and display the value in the web browser.
Here Complete Routes file (Insert, Read, Update, Delete)
web.php
<?php
use AppBiodata;
// insert routes
Route::get('/insert', function() {
Biodata::create(['titles'=>'First Post','descriptions'=>'This is descriptions']);
return 'Biodata Created';
});
// read routes
Route::get('/read', function() {
$biodata = new Biodata();
$data = $biodata->all();
// looping here
foreach ($data as $post) {
echo $post->id . ' - ' . $post->titles . '<br>';
}
});
// update routes
Route::get('/update', function() {
$biodata = Biodata::find(2);
$biodata->titles = 'The First Post Title was Update';
$biodata->save();
$data = $biodata->all();
// looping here
foreach ($data as $list) {
echo $list->id . ' - ' . $list->titles . '<br>';
}
});
// delete route
Route::get('/delete', function() {
$biodata = Biodata::find(1);
$biodata->delete();
$data = $biodata->all();
foreach ($data as $list) {
echo $list->id . ' - ' . $list->titles . '<br>';
}
});
Video Tutorial How to Create Eloquent ORM in laravel 5.3
Stay update and subscibe us To create more project using CRUD basics in laravel.
See you Next Lessons.......
COMMENTS