Related code in the model
namespace App;
Use Illuminate\database\eloquent\model;
class Student extends model{
//default corresponding to the model complex number, that is, students, if not, you need to specify the table name
protected $table = ' student ';//Specify Table name
//Default primary key is ID, if not, you need to specify
protected $primaryKey = ' id ';
//Automatic Maintenance timestamp
Public $timestamps = true;
protected function GetDateFormat ()
{
return time ();
}
//Do not format timestamps
protected function Asdatetime ($val)
{
return $val;
}
//Specify fields that allow bulk assignment
protected $fillable = [' name ', ' age '];
//Specify fields that do not allow bulk assignment
protected $guarded = [' name ', ' age '];
}
Related Code of the controller
ORM Using Model Queries
Public function Orm1 () {
//all (). Returns the object of the model
$student = Student::all ();
//find ()
$student = Student::find (+);
//findorfail (), can not find the error
$student = Student::findorfail (+);
//get ()
$student = Student::get ();
//first ()
$student = student::where (' id ', ' > ', ten)
->orderby (' age ', ' desc ')
->first ();
}
//orm using model queries
Public function Orm2 () {
//Use model to add data
$student = new Student ();
$student->name = ' Hello ';
$student->age = ' Hello ';
$bool = $student->save ();
Var_dump ($bool);
//Formatted date
$student = student::find (1);
echo Date (' y-m-d h:i:s ', $student->addtime);
//Use the model's Create method to add data,
$student = student::create ([' name ' = ' haha ', ' age ' =>10]);
//firstorcreate, search first, if not, add
$student = student::firstorcreate ([' name ' = ' haha ']);
//firstornew, search first, if not return the instance, you need to call save to add
$student = student::firstornew ([' name ' = ' haha ']);
$bool = $student->save ();
//Update data by model
$student = Student::find (ten);
$student->name = ' happy ';
$bool = $student->save ();
Var_dump ($bool);
//batch update, number of rows returned
$num = student::where (' id ', ' > ', ten)->update (
[' Age ' =>20]
);
Var_dump ($num);
//Through model deletion, no error will be found
$student = Student::find (ten);
$bool = $student->delete ();
Var_dump ($bool);
//Delete by primary key
$num =student::d Estroy (ten);
Var_dump ($num);
$num =student::d Estroy ([10,11,20]);
Var_dump ($num);
$num =student::where (' id ', ' > ', '->delete ');
Var_dump ($num);
}
Eloquent ORM Model of Laravel database operation