当前位置:网站首页>Notes for laravel model

Notes for laravel model

2022-06-24 23:12:00 Wang Daochang's way of programming

One 、 Time to show

Define the time format in the model , otherwise model return "2020-08-13T03:36:53.000000Z" Format

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Models extends Model{
    
    protected function serializeDate(DateTimeInterface $date)
    {
    
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }
  	
  	// Or used within the model attr Method 
    public function getCreateTimeAttribute($value){
    
     		 return $value;
    }
}

Two 、 newly added

Use model1::firstOrCreate() It is necessary to define in the model fillable, Such as

class Model1 extends Models{
    
		protected $connection = 'connection_name';// Database connection 
    
    public $timestamps = false;// If the table does not contain created_at  and  updated_at Field , Or just one , Must be set  $timestamps=false. Or through  CREATED_AT  and  UPDATED_AT  Constant setting custom field name :
    const CREATED_AT = 'create_time';
    const UPDATED_AT = null;
    
		protected $dateFormat = 'U';// Default time storage format  Y-m-d H:i:s, Or through $dateFormat Property to customize the timestamp format , The value of this property is determined by date()  Function analysis 

    protected $fillable = ['name', 'user_id', 'status'];
    protected $dates = ["creat_at"]

		protected $casts = [	// Define field properties 
      'is_directory' => 'boolean',
    ];
	...
}

3、 ... and 、 Model Pagination

Sort and page the query results in the model query operation :

$posts = Post::where('views', '>', 0)->orderBy('id', 'desc')->offset(10)->limit(5)->get();
// Or use a pager 
$users = DB::table('users')->paginate(15);

Four 、 preloading

???

5、 ... and 、user Model authorization

Laravel Of User The model provides two for authorizing actions :can and cant.can Method receives the action to be authorized and the corresponding model as parameters . as follows , Determine whether the user is authorized to update a given Post Model :

if ($user->can('update', $post)) {
    }

6、 ... and 、 Model listening events

Method function
retrieved Trigger after getting the model instance
creating Triggered before inserting into the database
created Triggered after inserting into the database
updating Triggered before updating to the database
updated Triggered after updating to the database
saving Triggered before saving to the database ( Insert / Before updating , Both insert and update will trigger )
saved Triggered after saving to the database ( Insert / After the update , Both insert and update will trigger )
deleting Triggered before deleting a record from the database
deleted Triggered after deleting a record from the database
restoring Triggered before restoring soft delete records
restored Triggered after recovering soft delete records
class Model1 extends Models{
    
	...
    public static function boot(){
    
        parent::boot();
        static::creating(function (Area $area) {
    
            info("Model [Area] creating...");
        });
    }
	...
}

notes : The corresponding event will not be triggered during batch update , Because the query builder is used directly , Bypassing the model approach .

原网站

版权声明
本文为[Wang Daochang's way of programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241719271208.html