Laravel's Eloquent: can't edit values

时间:2018-12-03 13:17:00

标签: php laravel eloquent lumen

I'm using Lumen, trying to edit values, which is the easiest thing to do, for some reason, the updated values aren't being saved

Task.php model

public function taskUsers()
{
    return $this->hasMany('App\Models\Tasks\UserTask')->where('role',1);
}

UserTask.php model contains nothing, an empty model

class UserTask extends BaseModel { }

Migrations

class CreateTasksTable extends Migration
{
    protected $table = 'tasks';
    protected $app_table = true;


    public function up()
    {
        Schema::create($this->getTable(), function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->dateTime('submit_date');
            $table->dateTime('closed_date')->nullable();
            $table->dateTime('due_date')->nullable();
            $table->tinyInteger('is_done')->nullable()->default(0);
            $table->integer('domain_id')->unsigned()->nullable();
            $table->foreign('domain_id')->references('id')
                ->on(self::getTableName('domains'))->onDelete('cascade');
            $table->bigInteger('created_by')->unsigned()->nullable();
            $table->foreign('created_by')->references('id')
                ->on(self::getTableName('auth_users', false))->onDelete('cascade');
            $table->bigInteger('closed_by')->unsigned()->nullable();
            $table->foreign('closed_by')->references('id')
                ->on(self::getTableName('auth_users', false))->onDelete('cascade');
            $table->timestamps();
        });

    }
    public function down()
    {
        Schema::drop($this->getTable());
    }
}

and

class CreateTaskUsersTable extends Migration
{
    protected $table = 'task_user';
    protected $app_table = true;
    public function up()
    {
        Schema::create($this->getTable(), function (Blueprint $table) {
            $table->increments('id');
            $table->integer('task_id')->unsigned()->nullable();
            $table->foreign('task_id')->references('id')
                ->on(self::getTableName('tasks'))
                ->onDelete('cascade');
            $table->bigInteger('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')
                ->on(self::getTableName('auth_users', false))
                ->onDelete('cascade');
            $table->integer('role');
        });
    }
    public function down()
    {
        Schema::drop($this->getTable());
    }
}

The edit action for example is so simple, if I just want to edit the title, that won't work, without even editing the rest.

class EditTaskAction extends BaseAction
{
    protected $verbs = array('POST');
    protected $private = true;

    protected $inputRules = [
        'domain_id' => 'required',
        'task_id' => 'required',
        'title' => '',
        'due_date' => '',
        'assignee_id' => '',
        'is_done' => '',
        'role' => ''
    ];
    public function execute()
    {
        $title = $this->request->get('title');
        $dueDate = $this->request->get('due_date');
        $assigneeId = $this->request->get('assignee_id');
        $taskId = $this->request->get('task_id');
        $isDone = $this->request->get('is_done');
        $role = $this->request->get('role');
        $userId = \Auth::id();
        $domainId = $this->request->get('domain_id');
        \DB::beginTransaction();
        try {
            $task = Task::where('id', $taskId)
                ->where("domain_id", $domainId) ->first();
            $userTask = UserTask::where('task_id', $taskId)->first();

                if (isset($title) && !empty($title)) {
                    $task->title = $title;
                }
                if (isset($dueDate) && !empty($dueDate)) {
                    $task->due_date = $dueDate;
                }
                if (isset($assigneeId) && !empty($assigneeId)) {
                    $userTask->user_id = $userId;
                }
                if (isset($role) && !empty($role)) {
                    if ($role == TaskUserRole::ASSIGNEE) {
                        $userTask->role = $role;
                    }
                }

                if (isset($isDone) && !empty($isDone) ) {
                    if ($isDone == 0) {
                        $task->closed_by = null;
                        $task->closed_date = null;
                        $task->is_done = 0;
                    } else if ($isDone == 1) {
                        $task->closed_by = $userId;
                        $task->closed_date = Carbon::now();
                        $task->is_done = 1;
                    }
                }
                $task->save();
                $userTask->save();
                return $this->response->statusOk();

        } catch (\Exception $exception) {
            \DB::rollBack();
            \Log::error($exception);
            $this->response->addErrorDialog(self::SOMETHING_WENT_WRONG);
            return $this->response->statusFail(self::SOMETHING_WENT_WRONG);
        }
        \DB::commit();
    }
}

Basically all I'm doing

$task = Task::find($taskId); // I tried that too
$task->title = 'something';
$task->save();

It's not working

3 个答案:

答案 0 :(得分:2)

我认为问题出在您的交易中。您以\DB::beginTransaction();开始,但是\DB::commit()(用于将更改保存到数据库)将永远不会运行,因为您之前曾做过return $this->response->statusOk();

您可以尝试将响应保存到变量中,并在\DB::commit();

之后返回
class EditTaskAction extends BaseAction
{
    // ...
    public function execute()
    {
        // ...
        $response = null;
        \DB::beginTransaction();
        try {
            // ...
            $task->save();
            $userTask->save();
            $response = $this->response->statusOk();
        } catch (\Exception $exception) {
            // ...
            $response = $this->response->statusFail(self::SOMETHING_WENT_WRONG);
        }
        \DB::commit();
        return $response;
    }
}

答案 1 :(得分:1)

Did you set the guarded property on the model? You can completely disable guarding by setting it to an empty array.

protected $guarded = [];
// or check this:
protected $fillable = [...];

Otherwise you might find some error in the logs.

答案 2 :(得分:1)

我认为您模型中的问题是您是否将数据存储在可填充数据中