Метод Ax ios для обновления поля базы данных с использованием vue. js и Laravel - PullRequest
0 голосов
/ 05 апреля 2020

Я работаю над CRUD-приложением Ax ios с компонентами Laravel и Vue. js. В настоящий момент я получаю

"HTTP422: НЕОБХОДИМЫЙ СУЩНОСТЬ - запрос был правильно сформирован, но не может быть обработан из-за ошибок semanti c. (XHR) POST - http://127.0.0.1: 8000 / profile / 1/4 / false"

в журнале консоли при нажатии кнопки. Что должно произойти, это обновить поле is_completed до true, но в настоящее время я не могу подключить его к базе данных, даже с помощью Create (поле имеет значение null).

CompleteButton. vue

        axios.post('/profile/' + this.userId + '/' + this.item2.id +'/'+ this.item2.is_complete)
            .then(response => {
                this.item2 = ! this.item2;

                console.log(response.data);
            })
            .catch(errors => {
                if (errors.response.item2 == 401) {
                    window.location = '/login';
                }
            }
        );

index.blade. php

<div class="d-flex align-items-center pb-3">
                                                <div class="h4">{{ $task->id }}</div>

                                                <complete-button user-id="{{ $user->id }}" item="{{ $task }}" offautocomplete></follow-button>
                                            </div>

web. php

Route::post('/profile/{user}/{task}/{is_complete}', 'CompleteController@store');

CompleteController. php

<?php

namespace App\Http\Controllers;
use App\Profile;
use App\User;
use App\Task;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class CompleteController extends Controller
{
 public function __construct()
 {
    $this->middleware('auth');
 }



 public function store(User $user, Task $task, Request $request)
 {
    // check if the authenticated user can complete the task

    $data = $this->validate($request, [
        'is_complete' => 'required|boolean',
    ]);



    Auth::user()->tasks()->where('id', $task->id)->update([
        'is_complete' => $data['is_complete']
    // mark the task as complete and save it
    ]);

}

}

1 Ответ

1 голос
/ 05 апреля 2020

Вы смешиваете синтаксис POST и GET в вашем ax ios запросе и вашей конечной точке.

Поэтому вы вообще не отправляете никаких данных в конечную точку POST - это означает, что проверка не выполняется для is_complete, так как required и boolean

Попробуйте:

axios.post(
    '/profile/' + this.userId + '/' + this.item2.id,
    {is_complete : this.item2.is_complete}
)
//.........
Route::post('/profile/{user}/{task}/', 'CompleteController@store');

При отладке может оказаться полезным console.log(error.response.data), чтобы можно было просмотреть ошибки проверки.

...