Laravel study notes (5) model, additions and deletions, soft deleted

Laravel model add, delete, change operation original
soft delete text

  1. increase

    App\User::insert(
        ['email' => '[email protected]', 'votes' => 0]
    );
    
    \App\User::insert([
        ['email' => '[email protected]', 'votes' => 0],
        ['email' => '[email protected]', 'votes' => 0]
    ]);
    
    // 存在则不插入
    \App\User::insertOrIgnore([
        ['id' => 1, 'email' => '[email protected]'],
        ['id' => 2, 'email' => '[email protected]']
    ]);
    
    // 插入并返回ID
    $id = \App\User::insertGetId(
        ['email' => '[email protected]', 'votes' => 0]
    );
    # 注意:当使用 PostgreSQL 时,insertGetId 方法将默认把 id 作为自动递增字段的名称。如果你要从其他「序列」来获取 ID ,则可以将字段名称作为第二个参数传递给 insertGetId 方法。
    
    // 在这个例子中,我们把来自 HTTP 请求中的 name 参数简单地指定给 App\Flight 模型实例的 name 属性。当我们调用 save 方法时,就会添加一条记录到数据库中。created_at 以及 updated_at 时间戳将在 save 方法被调用时会被自动设置,因此我们不需要去手动设置它们
    $flight = new Flight;
    $flight->name = $request->name;
    $flight->save();
    
  2. change

    $numbersOfRowsAffected = \App\User::where('id', 1)->update(['votes' => 1]);
    // 当通过模型批量更新时,saving, saved, updating, and updated 模型事件将不会被更新后的模型触发。这是因为批量更新时,模型从来没有被取回。
    
    // 搜索主键为1的数据并进行更新
    $flight = App\Flight::find(1);
    $flight->name = 'New Flight Name';
    $flight->save();
    
    # json
    \App\User::where('id', 1)->update(['options->enabled' => true]);
    
    App\User::increment('votes');        // update users set votes = votes+1       votes字段自增1
    \App\User::increment('votes', 5);        // update users set votes = votes+5       votes字段自增5
    \App\User::increment('votes', 1, ['name' => 'John']);        // update users set votes = votes+1 where name = John      votes字段自增1
    \App\User::decrement('votes');        // update users set votes = votes-1       votes字段自减1
    \App\User::decrement('votes', 5);       // update users set votes = votes-5      votes字段自减5
    
  3. delete

    $numbersOfRowsAffected = \App\User::delete();
    $numbersOfRowsAffected = \App\User::where('votes', '>', 100)->delete();
    \App\User::truncate();
    
    
    $flight = App\Flight::find(1);  // 取回模型再删除
    $flight->delete();
    
    // 或者
    App\Flight::destroy(1);     // 直接删除
    App\Flight::destroy([1, 2, 3]);
    App\Flight::destroy(1, 2, 3);
    App\Flight::destroy(collect([1, 2, 3]));
    
    当使用 Eloquent 批量删除语句时,`deleting` 和 `deleted` 模型事件不会在被删除模型实例上触发。因为删除语句执行时,不会检索模型实例。
    
  4. Soft-deleted
    first model to be used in SoftDeletestrait, which provides a range of related trait approach to soft delete specific reference source Illuminate \ Database \ Eloquent \ SoftDeletes, Moreover, you should set the $ date property array, which will placed deleted_at:

    use Illuminate\Database\Eloquent\SoftDeletes;
    
    class User extends Model
    {
        use SoftDeletes;
    
        protected $datas = ['deleted_at'];
    

    Delete usual manner (already soft-deleted data will then delete the Call to a member function delete () on null)

    //1、第一种方法
    Post::find(5)->delete();
    //2、第二种方法
    Post::destroy(5)
    //3、第三种方法
    Post::where('views', 0)->delete();
    

    Soft delete query needs to be called before a general inquiry withTrashed

    Post::withTrashed()->find('1');
    
    Post::withTrashed()->where('id','>',1)->get();
    
    Post::withTrashed()->all();
    

    Soft-deleted data recovery

    //恢复单个
    Post::withTrashed()->find(1)->restore();
    //恢复多个模型
    Post::withTrashed()->where('id','>',1)->restore();
    //恢复所有模型
    Post::withTrashed()->restore();
    

    Forced to delete

    //删除单个
    Post::withTrashed()->find(1)->forceDelete();
    //删除多个模型
    Post::withTrashed()->where('id','>',1)->forceDelete();
    //删除所有模型
    Post::withTrashed()->forceDelete();
    

    After the data is soft-deleted want the associated data to associated data, and needs later belongsTo withTrashed

    public function user()
    {
        return $this->belongsTo('App\User')->withTrashed();
    }
    
Published 40 original articles · won praise 0 · Views 788

Guess you like

Origin blog.csdn.net/qj4865/article/details/104138307