繁体   English   中英

Laravel SoftDelete,删除和更新不起作用

[英]Laravel SoftDelete, delete and Update not working

我正在尝试使用Laravel REST API,但遇到了这个问题。 我似乎无法删除,强制删除甚至更新该模型(尚未尝试使用任何其他模型)。

模型:

<?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');
    }
}

盗版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视图

编辑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永远不会在数据库上更新。 有帮助吗?

您尚未提及要删除或更新的记录的“ id”。

//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);

希望这对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM