Eloquent ORM Model of Laravel database operation

Source: Internet
Author: User
Tags echo date

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.