Creation of Laravel Framework Model and Example of Model Operating on Data
- 2021-12-09 08:34:29
- OfStack
This paper describes the creation of Laravel framework model and the operation of the model on data. Share it for your reference, as follows:
Model creation:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model{
// Specify a table name
protected $table = 'wd_user';
// Specify the fields that allow bulk copying
protected $fillable = ['username'];
// Specify id
protected $primaryKey = 'uid';
// Automatic maintenance timestamp
public $timestamps = true;
// Get time format
protected function getDateFormat()
{
return time();
}
// Get a timestamp
protected function asDateTime($val)
{
return $val;
}
}
The model manipulates the data:
public function orm(){
// Look up all records of the table
//$user = Admin::all();
//dd($user);
// Inquire about 1 A record
//$user = Admin::find(2);
//dd($user);
//findOrFail() Find according to the primary key and throw an exception if it is not found
//$user = Admin::findOrFail(1);
//dd($user);
// Query all records
//$user = Admin::get();
//dd($user);
// Add Criteria Query
//$user = Admin::where('uid','>=',4)->orderBy('uid','desc')->first();
// Segmented query
//Admin::chunk(2,function($user){
//var_dump($user);
//});
// Aggregate function
// Gets the number of records
//$count = Admin::count();
//dd($count);
// Get the maximum value
//$max = Admin::where('uid','>=',5)->max('age');
// Using a model to add data
//$user = new Admin();
//$user-> username = 'haha';
//$bool = $user->save();
//dd($bool);
// Acquisition time
//$user = Admin::find(1);
//echo date('Y-m-d H:i:s',$user->create_at);
// Object that uses the model Create Method to add data
//$user = Admin::create(['username'=>'meimei']);
//dd($user);
// Find users by attributes, and add if not
//$user = Admin::firstOrCreate(['username'=>'imooc']);
//dd($user);
// Find users by attributes , If not, add it, but do not save it to the database
//$user = Admin::firstOrNew(['username'=>'imooc']);
//dd($user);
// Update data through models
//$user = Admin::find(1);
//$user->username = 'jack';
//$bool = $user->save();
//dd($bool);
// Adding conditions
//$num = Admin::where('id','>','1')->update(['age'=>21]);
//dd($num);
// Delete through model
//$user = Admin::find(6);
//$bool = $user->delete();
//dd($bool);
// Delete by primary key
//$num = Admin::destroy(6);
//dd($num);
// Delete multiple records
//$num = Admin::destroy(1,2,3);
//$num = Admin::destroy([1,2,3]);
//dd($num);
// Delete operation of adding conditions
//$num = Admin::where('uid','>','4')->delete();
//dd($num);
}
More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
Hope that this article is based on the framework of Laravel PHP programming help.