Итак, я работаю с API Laravel 5.2, и я работаю над созданием метода обновления для конкретной модели. До сих пор методы индекса, show ($ id) и хранилища отлично работают. Однако я получаю следующее сообщение об ошибке на моей реализации метода обновления:Laravel 5 API: ошибка записи обновления
BadMethodCallException in Macroable.php line 81:
Method save does not exist.
Вот метод обновления:
public function update($id, CreateZoneRequest $request)
{
$zones = Zone::where('_id', '=', $id) -> get();
if(sizeof($zones) == 0){
return response()->json(['message' =>'That zone number is invalid', 'code' => 404], 404);
}
$description = $request->get('description');
$zones ->description = $description;
$zones ->save();
return response() -> json(['message' => 'The zone has been updated'], 200);
}
Вот код CreateZoneRequest:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateZoneRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return
[
'description' => 'required',
];
}
public function response (array $errors){
return response() -> json(['message' => 'You are missing a required field', 'code' => 422], 422);
}
}
Здесь индекс метод (для справки). Это один работает без проблем:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Zone;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\CreateZoneRequest;
class ZoneController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$zones = Zone::all();
if(sizeof($zones) == 0){
return response()->json(['message' =>'There are no zones', 'code' => 404], 404);
}
return response() -> json(['data' => $zones], 200);
}
Вот подробное сообщение об ошибке при попытке использовать PUT на этой конечной точке API/v1/зоны/1 описание = Бла
Используйте '$ зоны = зоны :: где ('_ ID', '=', $ ID) -> первый();' или '$ зоны = зона :: найти ($ id); 'вместо' get' в противном случае вы получите «массивы» назад, – apokryfos
$ zone -> description = $ request-> description; более красивый способ для замены этих строк. $ description = $ request-> get ('description'); $ zone -> description = $ description; – aimme
@apokryfos, если я использую это, теперь получаю ошибку в SQL-запросе: SQLSTATE [42S22]: Столбец не найден: 1054 Неизвестный столбец 'id' в 'where clause', который является странным, так как я говорю, что предложение where использовать _id –