Qcoder
11/11/2016 - 12:39 PM

CRUD with laravel model

CRUD with laravel model

Laravel CRUD with MODEL

[Written by @kingRayhan ]

Table of contents

Raw sql CRUD

C

Create

[Back to top ↑ ]

Method 1


$data = new MODEL_NAME;
$data->column1 = 'value1';
$data->column2 = 'value2';
$data->column3 = 'value3';
.........................
.........................
$data->save();

Method 2

Number::create([
    'column1' => 'value1',
    'column2' => 'value2',
    'column3' => 'value3',
     [....] => [....],
     ...................
     ...................
]);

to push data in table by this way , you have to write a protected property in the model class.

protected $fillable = [ 'column1' , 'column2' , 'column3' , [....] ];

[Back to top ↑ ]


R

Read

[Back to top ]

Get all data from model

Get all row of a table corresponding with a Model

MODEL_NAME::all();

Example

Route::get('/name', function () {
    $names = Name::all();
    foreach ($names as $name) {
        echo "ID :".$name->id." ";
        echo "Name :".$name->name;
        echo "<br/>";
    }
});


Fetch first row

MODEL_NAME::first();


Fetch a row by Primary Key

MODEL_NAME::find( ..id.. );

Also multiple row(s) can be fetch by find() method

MODEL_NAME::find( [ 1 , 2 , 3 , .... ] );

you must pass an array in find() method for getting multiple row.


Get row(s) by matching conditional query constraints.

MODEL_NAME::where('column' , 'value')
           ->where('column' , 'value')
           ->where('column' , 'value')
           ...........................
           ...........................
           ->get();

get() method must have to with where() method to fetch data

Example

Fetch row(s) which has active column's value is 1

MODEL_NAME::where('active' , 1)->get()

Fetch row(s) which has active column's value is 1 and role column's value is administration

MODEL_NAME::where('active' , 1)->where('role','administration')->get()


Some fetch filtering methods

first()

[Back to top ↑ ]

This method is used for fetching first row from multiple rows

Fetch first row from all rows

MODEL_NAME::all()->first();

BTW you can easily fetch first row from all rows by this MODEL_NAME::first() :smile:

Fetch first where role is administration

MODEL_NAME::where('role','administration')->first()->get();

Since I used where() method , so I must have to use get() method with it to get row data


limit() / take()

[Back to top ↑ ]

Take a certain number of row(s) from multiple rows

Take 3 row from all rows

MODEL_NAME::all()->limit(3);
MODEL_NAME::all()->take(3);

Example

Fetch first 3 row which has active = 1 and age column's value is between 18 to 25

MODEL_NAME::where('active',1)
          ->where('age' , '>' , 18)
          ->where('age' , '<' , 25)
          ->limit(3)
          ->get();


count()

[Back to top ↑ ]

Get total number of row(s)


Count number of rows

    MODEL_NAME::all()->count();

Count number rows which has active = 1 and age column's value is between 18 to 25

MODEL_NAME::where('active',1)
          ->where('age' , '>' , 18)
          ->where('age' , '<' , 25)
          ->count();


sum('COLUMN_NAME')

[Back to top ↑ ]

Get summation of a certain column

MODEL_NAME::all()
          ->sum('amounts');

max('COLUMN_NAME')

[Back to top ↑ ]

Fetch a row which has maximum value of specific column

MODEL_NAME::where('role','student')
          ->max('CGPA');




U

Update

[[Back to top ↑](#) ]

Method 1

$data = MODEL_NAME::find( ... PrimaryKeyID  ....); // select a row
$data->coulmn_name = 'new value';   
$data->coulmn_name = 'new value';   
$data->coulmn_name = 'new value';   
................................
................................
$data->save();

By this way can only select a row by find() method. You can't select row by other selecting methods


Method 2

MODEL_NAME:: find() or where() or first() or [ other selecting method ]  // select a row
      ->update([
          'column_name' => 'new value',
          'column_name' => 'new value',
          ............................
          ............................
      ]);

[Back to top ↑ ]


D

Delete

[Back to top ↑ ]

Method 1

MODEL_NAME:: find() or where() or first() or [ other selecting method ] -> delete()

Example

User::find(2)->delete();
User::where('active',0)->delete();


Method 2 Delete row(s) by primary key

MODEL_NAME::destroy(1);

MODEL_NAME::destroy([1, 2, 3]);

MODEL_NAME::destroy(1, 2, 3);

[Back to top ↑ ]

Raw SQL CRUD

DB::insert('insert into TABLE_NAME (id, name) values (?, ?)', [1, 'Dayle']);
DB::select('select * from users TABLE_NAME active = ?', [1]);
DB::update('update users set votes = 100 where name = ?', ['John']);
DB::delete('delete users where name = ?', ['John']);

[Back to top ↑ ]