Laravel SoftDelete,删除和更新无法正常工作

时间:2017-09-01 08:20:34

标签: php laravel eloquent

我正在尝试使用Laravel REST API,但我遇到了这个问题。我似乎无法删除,forceDelete甚至更新此模型(没有尝试过任何其他模型)。

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Multimedia extends Model
{
    use SoftDeletes; 

    protected $fillable = ['Path', 'Type'];

    protected $dates = ['deleted_at'];

    public function category()
    {
      return $this->belongsTo('App\Categories');
    }
}

控制器:

命名空间App \ Http \ Controllers;

use App\Multimedia;
use Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Log;
use Carbon;

class MultimediaController extends Controller
{
    public function delete(Multimedia $multimedia)
{

    //Option 1
    $multimedia->delete();
    return response()->json(null, 204);

    //Option 2
    $multimedia->forceDelete();
    return response()->json(null, 204);

    //Option 3
    $multimedia->updated_at = Carbon::now();
    $multimedia->deleted_at = Carbon::now();
    $multimedia->update();
    return response()->json(null, 204);

    //Option 4
    DB::delete('DELETE FROM multimedia WHERE ID=' . $multimedia->ID);
    return response()->json(null, 204);

    //Option 5 (In response to suniti yadav) THIS ONE WORKS.
    $multimedia->where('id', '=', $multimedia->ID)->delete();
    return response()->json(null, 204);
}
}

只有选项4有效(相当于强制删除)。在选项1和3中,如果我将$ multimedia对象发送到JSON,我可以看到updated_at和deleted_at被设置为当前时间。

路线:

Route::delete('multimedia/{multimedia}', 'MultimediaController@delete');

任何想法?

谢谢!

编辑:我在两次迁移中实现了这个

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMultimediaTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('Multimedia', function (Blueprint $table) {
            $table->increments('ID');
            $table->string('Path', 200)->unique();
            $table->enum('Type', ['Image', 'Video'])->default('Image');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('multimedia');
    }
}

Migartion 2

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCategoryOnMultimedia extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('Multimedia', function (Blueprint $table) {
            $table->string('Category', 200);
            $table->foreign('Category')->references('Name')->on('Categories');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

编辑2:添加了PHPMyAdmin的注册表图片

PHPMyAdmin View

编辑3:在suniti yadav的答案之后我继续遇到这个问题事件

我在另一个控制器上有这个:

public function store(Request $request)
{
    $price = new Prices();
    $price['Price'] = $request->input('Price');
    $price['FromDate'] = date_create($request->input('FromDate'));
    if($request->has('ToDate'))
        $price['ToDate'] = date_create($request->input('ToDate'));
    $price['Type'] = $request->input('Type');

    if($price['Type'] == 'Regular')
    {
        $lastRegularPrice = Prices::where('Type', '=', 'Regular')->orderBy('FromDate', 'DESC')->take(1)->get()->first();
        $priceDate = Carbon::parse($price['FromDate']);
        //return response()->json($lastRegularPrice, 406);

        if($priceDate->lte($lastRegularPrice->FromDate))
        {
            $error["error"] = "Cannot add new 'Regular' price starting from " . $priceDate->toDateString() . " due to being before the current 'Regular' price starting from " . $lastRegularPrice->FromDate->toDateString();
            $error["status"] = 406 ;
            return response()->json($error, 406);
        }

        $priceDate->subDay();
        $lastRegularPrice->ToDate = $priceDate;
        $lastRegularPrice->save();
    }
    else if($price['Type'] == 'Promo')
    {

    }
    $price->save();
    return response()->json($price, 201);
}

变量lastRegularPrice永远不会在数据库上更新。任何帮助?

1 个答案:

答案 0 :(得分:1)

你没有提到过&#39; id&#39;要删除或更新的记录。

//Option 1
$multimedia->where('id', '=', $multimedia->ID)->delete();

//Option 1 or 2

    $m = $multimedia->withTrashed()->findOrFail($multimedia->ID);
    if(!m->trashed()){
      $m->delete();
    }
    else {
      $m->forceDelete();
    }

//Option 2
    $multimedia->where('id', '=', $multimedia->ID)->forceDelete();

 //Option 3

    $data = [
       'updated_at' => Carbon::now(),
       'deleted_at' => Carbon::now()
   ];

    $multimedia->where('id', '=', $multimedia->ID)->update($data);

希望这对你有用。