1. Defining the Model
<?php
namespace App;
Use Illuminate\database\eloquent\model;
class Flight extends model{
protected $table = ' my_flights ';
protected $primaryKey = ' my_id ';
Public $timestamps = false;
}
2. Get all the data in the table All/get
$flights = Flight::all ();
$flights = App\flight::where (' active ', 1)
->orderby (' name ', ' desc ')
->take (10)
->get ();
3.find and first get a single record
//
$flight = App\flight::find (1);
$flight = App\flight::where (' active ', 1)->first ();
4. Get aggregations
$count = app\flight::where (' active ', 1),Count();
$max = app\flight::where (' active ', 1),Max(' Price ');
5. New
To insert a new record into the database, simply create a new model instance, set the properties of the model, and then call the Save method:
$flight New Flight;
$flight $request->name;
$flight->save ();
The Save method can also be used to update a model that already exists in the database. To update a model, you should get it, set the property you want to update, and then call the Save method.
$flight = app\flight::find (1);
$flight->name = ' New flight name ';
$flight->save ();
The Create method inserts a new record into the database that returns the model instance that was inserted, first set up in the model:
Properties that can be assigned in bulk
Protected$fillable = [' name '];
$flight = app\flight::create ([' name ' = ' Flight 10 ']);
6. Delete
To delete a model, call the Delete method on the model instance:
$flight = app\flight::find (1);
$flight->delete ();
If you know the primary key of the model, you can delete it directly without needing to get it:
App\flight::d Estroy (1);
App\flight::d Estroy ([1, 2, 3]);
App\flight::d Estroy (1, 2, 3);
Delete multiple models from a query
$deletedRows = app\flight::where (' active ', 0)->delete ();
Laravel using the eloquent ORM to manipulate the database