Search code examples
phplaraveleloquentlumen

Laravel's Eloquent: can't edit values


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


Solution

  • I think the problem is with your transaction. You're starting it with \DB::beginTransaction(); But the \DB::commit() (to save your changes to the database) will never be run, because you do Return-Statements before, like return $this->response->statusOk();

    You could try to save your response to a variable and return it after the \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;
        }
    }