Laravel Eloquent ORM

  1. <?php  
  2.   
  3. namespace App;  
  4.   
  5. use Illuminate\Database\Eloquent\Model;  
  6.   
  7. class Flight extends Model {  
  8.   
  9.     /** 
  10.      * The table associated with the model. 
  11.      * 
  12.      * @var string 
  13.      */  
  14.     protected $table = 'my_flights'//默认情况下模型名的复数形式(+s)为数据表名,也可以为模型指定表名  
  15.   
  16.     protected $primaryKey = 'filght_id'//默认情况下使用'id'作为表主键,也可以指定主键名  
  17.       
  18.     public $timestamps = false; //默认情况下,Eloquent 期望数据表中存在 created_at 和 updated_at 字段。也可以取消  
  19.   
  20.     protected $dateFormat = 'U'//定制时间戳的格式  
  21.   
  22.     protected $fillable = ['first_name''last_name''email']; //定义允许批量更新的字段白名单  
  23.   
  24.     protected $guarded = ['id''password']; //定义不允许更新的字段黑名单  
  25.   
  26.   
  27.   
  28.     public function scopePopular($query) {  
  29.         return $query->where('votes''>', 100);  
  30.     }  
  31.     public function scopeWomen($query) {  
  32.         return $query->whereGender('W');  
  33.     }  
  34.     //在controller中,使用$users = Flight::popular()->women()->orderBy('created_at')->get();可以自定义查询范围  
  35.   
  36.   
  37. }  



  1. <?php  
  2.   
  3. namespace App\Http\Controllers;  
  4.   
  5. use App\Flight;  
  6. use App\Http\Controllers\Controller;  
  7.   
  8. class FlightController extends Controller {  
  9.     //查询所有记录  
  10.     public function all() {  
  11.         $flights = Flight::all(); //取出flights表的所有数据  
  12.   
  13.         foreach ($flights as $flight) {  
  14.             echo $flight->name; //获取列的值  
  15.         }  
  16.   
  17.         return view('flight.index', ['flights' => $flights]);  
  18.     }  
  19.   
  20.   
  21.     //多种查询条件:where, order by, limit  
  22.     public function select() {  
  23.         $flights = App\Flight::where('active', 1)  
  24.                ->orderBy('name''desc')  
  25.                ->take(10)  
  26.                ->get();  
  27.     }  
  28.   
  29.   
  30.     //当查询结果有大量数据时,使用分块查询能节省内存资源  
  31.     public function chunkSelect() {  
  32.         Flight::chunk(200, function ($flights) {  
  33.             foreach ($flights as $flight) {  
  34.                 echo $flight->name;  
  35.             }  
  36.         });  
  37.     }  
  38.   
  39.     //查询一条记录  
  40.     public function find() {  
  41.         $flight = App\Flight::find(1); //根据主键值1查找数据。  
  42.         $flight = App\Flight::where('active', 1)->first(); //根据where查询符合条件第一条记录  
  43.           
  44.         //如果下面的查询方法未查到数据,则会抛出404错误  
  45.         $model = App\Flight::findOrFail(1);  
  46.         $model = App\Flight::where('legs''>', 100)->firstOrFail();  
  47.     }  
  48.   
  49.   
  50.     //统计查询  
  51.     public function Aggregates() {  
  52.         $count = App\Flight::where('active', 1)->count();  
  53.         $max = App\Flight::where('active', 1)->max('price');  
  54.     }  
  55.   
  56.   
  57.     //插入一条数据  
  58.     public function insert(Request $request) {  
  59.         $flight       = new Flight;  
  60.         $flight->name = $request->name;  
  61.   
  62.         $flight->save(); //向数据库中插入一条记录,name字段的值为表单传递的name  
  63.     }  
  64.   
  65.   
  66.     //为了更新一条数据,首先你必须从数据库中将其取出,然后为需要更新的属性赋值,最后调用save方法  
  67.     public function save() {  
  68.         $flight       = App\Flight::find(1);  
  69.         $flight->name = 'New Flight Name';  
  70.   
  71.         $flight->save();  
  72.     }  
  73.   
  74.   
  75.     //限定where添加更新数据  
  76.     public function update() {  
  77.         App\Flight::where('active', 1)  
  78.           ->where('destination''San Diego')  
  79.           ->update(['delayed' => 1]);  
  80.     }  
  81.   
  82.   
  83.     //不存在则插入一条记录  
  84.     public function firstOrCreate() {  
  85.         $flight = App\Flight::firstOrCreate(['name' => 'Flight 10']); //如果不存在name为Flight 10,则插入一条  
  86.     }  
  87.   
  88.   
  89.     //删除时,先获取这条记录,然后delete  
  90.     public function delete(){  
  91.         $flight = App\Flight::find(1);  
  92.         $flight->delete();  
  93.   
  94.         App\Flight::destroy(1); //删除主键为1的记录  
  95.         App\Flight::destroy([1, 2, 3]); //删除主键为1,2,3的记录  
  96.   
  97.         $deletedRows = App\Flight::where('active', 0)->delete(); //删除active为0的数据  
  98.     }  
  99.   
  100.   
  101.     //软删除:并不物理删除数据,只是添加删除字段delete_at  
  102.     public function softDelete() {  
  103.         /** 
  104.          *调用 delete 方法时, deleted_at字段会被更新成现在的时间戳。在查询使用软删除功能的模型时,被「删除」的模型数据不会出现在查询结果里。 
  105.          *  
  106.          * 需要在对应的model中,设置 
  107.          * use Illuminate\Database\Eloquent\SoftDeletes; 
  108.          * use SoftDeletes; 
  109.          * protected $dates = ['deleted_at']; 
  110.          *  
  111.          * 在migration中,生成这个字段 
  112.          * Schema::table('flights', function ($table) {$table->softDeletes();}); 
  113.          * 
  114.          * 执行删除时使用delete方法即可。 
  115.          * 
  116.          * 判断某个模型数据,是否已删除: 
  117.          * $flight = Flight::find(1); 
  118.          * if ($flight->trashed()) {...} 
  119.          */  
  120.     }  
  121.   
  122.   
  123.     //查询已删除的,account_id为1的数据  
  124.     public function selectTrashed() {  
  125.         $flights = App\Flight::withTrashed()  
  126.                 ->where('account_id', 1)  
  127.                 ->get();  
  128.     }  
  129.   
  130.     //同上  
  131.     public function selectOnlyTrashed() {  
  132.         $flights = App\Flight::onlyTrashed()  
  133.                 ->where('airline_id', 1)  
  134.                 ->get();  
  135.     }  
  136.   
  137.     //恢复软删除的数据  
  138.     public function resoreTrashed() {  
  139.         //法1  
  140.         $flight = \App\Flight::find(1);  
  141.         $flight->restore();  
  142.   
  143.         //法2  
  144.         Flight::withTrashed()->where('account_id', 1)->restore();  
  145.     }  
  146.   
  147.   
  148.     //彻底删除数据(物理清除软删除的数据)  
  149.     public function forceDelete() {  
  150.         $flight = \App\Flight::find(1);  
  151.         $flight->forceDelete();  
  152.     }  
  153.   
  154.   
  155.     //多重where条件查询含有软删除的数据,需注意查询需不需要group  
  156.     public function selectClauseTrashed() {  
  157.         User::where(function($query) {  
  158.                 $query->where('name''=''John')->orWhere('votes''>', 100);  
  159.             })->get();  
  160.         //select * from `users` where `users`.`deleted_at` is null and (`name` = 'John' or `votes` > 100)  
  161.          
  162.         User::where('name''=''John')->orWhere('votes''>', 100)->get();      
  163.         //select * from `users` where `users`.`deleted_at` is null and `name` = 'John' or `votes` > 100  
  164.     }  
  165.   
  166.   
  167.   
  168. }  

orderBy('Id' , 'DESC')    //排序

猜你喜欢

转载自blog.csdn.net/servicesyy/article/details/80415454