A
A
Alexander Ivanov2018-07-30 13:55:26
Laravel
Alexander Ivanov, 2018-07-30 13:55:26

Why is the update method not working in laravel controller?

update(id, params) {
                console.log(params); // отправляются
                ajax.put(`/admin/sites/update/${id}`,params).then(
                    (response) => {
                        console.log(response);
                        console.log('Сохранено')
                    },
                    (err) => {
                        console.log(err.response);
                    }
                );
            },

// приходит success но не сохраняет    
public function update($id, Request $request)
    {
        $site = Site::find($id);
        $site -> fill($request->all());
        return response()->json([
            'success' => $site->save(),
        ]);
    }

//  Integrity constraint violation: 1062 Duplicate entry
// вроде понятно с чем связана ошибка решил её способом выше но не сохраняются изменения
public function update(Request $request, Site $site)
    {
        $site -> fill($request->all());
        return response()->json([
            'success' => $site->save(),
        ]);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-07-30
@cimonlebedev

In order for the fields to be changed through fill, they must be in an array $fillable. Documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question