Laravel 5 Tutorial : Eloquent ORM (Object Relation Maping) in Laravel 5.3

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 :
  1. Database Migration & Schema in Laravel 5.3
  2. How to Connect Database in laravel 5.3
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,

Eloquent ORM (Object Relation Maping) in Laravel 5.3

Eloquent ORM (Object Relation Maping) in Laravel 5.3
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');
}
  1. namespace App; defines the model namespace 
  2. class Biodata extends Model defines the category class that extends the Model 
  3. 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.
  4. protected $table = 'biodata'; explicitly defines the table name. The default is the plural form of the model name.
  5. 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';
});
  1. Biodata::create(); will executes the create static function of the model. 
  2. return 'Biodata Created'; output if biodata has been created.
Now, following and execute the URL : http://localhost:8080/insert you wil get the result :
"Biodata Created" and see at your database at the Biodata Tables :

Eloquent ORM (Object Relation Maping) in Laravel 5.3

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>';
    }
});
  1. new Biodata(); will create an instance variable of Biodata model
  2. $biodata->all(); will calls the method of the model to show all data from database.
  3. 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.
Following this URL to show all data, http://localhost:8080/read

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>';
    }
});
  1. Biodata::find(2); will call the find function from the model and pass in 2 as the primary key parameter value.
  2. $biodata->titles= 'The First Post Title was Update'; assigns the value The First Post Title was Update to the filed name.
  3. $category->save(); will saves the changes made to the our database
  4. $data = $biodata->all(); will show all data from database
  5. foreach ($data as $list){.....} will loops through all the records and display the value in the web browser.
following the URL : http://localhost:8080/update and you will see our data where id=2 has changed.

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>';
    }
});
  1. Biodata::find(1); will call the find function from the model and pass in 1 as the primary key parameter value.
  2. $biodata->delete(); will deletes the record that was retrieved via the find method, that is id=1
  3. $data = $biodata->all(); will show all data from database
  4. foreach ($data as $list){.....} will loops through all the records and display the value in the web browser.
Now, just execute the URL : http://localhost:8080/delete and your data that have id=1 will gone.

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

Next Recomended


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,84,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,5,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Laravel 5 Tutorial : Eloquent ORM (Object Relation Maping) in Laravel 5.3
Laravel 5 Tutorial : Eloquent ORM (Object Relation Maping) in Laravel 5.3
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
https://4.bp.blogspot.com/-dMxmqOyzjaY/V8RhWirAwlI/AAAAAAAACW4/1vV4zgTN8943wk-62mRHM1O6rQD4XLEBQCLcB/s320/laravel-tutorial-for-beginner.png
https://4.bp.blogspot.com/-dMxmqOyzjaY/V8RhWirAwlI/AAAAAAAACW4/1vV4zgTN8943wk-62mRHM1O6rQD4XLEBQCLcB/s72-c/laravel-tutorial-for-beginner.png
KODE AJAIB
https://www.kodeajaib.com/2016/08/laravel-5-tutorial-eloquent-orm-laravel-53.html
https://www.kodeajaib.com/
https://www.kodeajaib.com/
https://www.kodeajaib.com/2016/08/laravel-5-tutorial-eloquent-orm-laravel-53.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy