Skip to content

Commit c91a1a9

Browse files
committed
fix primary key being ignored when creating resources using PUT requests
1 parent 6b6e598 commit c91a1a9

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,21 @@ public function __construct(HalApiControllerParameters $parameters, HalApiTransf
101101
*/
102102
public static function getModelBindingCallback()
103103
{
104-
return function () {
104+
return function ($value) {
105105
switch (\Request::getMethod()) {
106106
case Request::METHOD_GET:
107107
throw new NotFoundHttpException;
108108
case Request::METHOD_POST:
109109
throw new NotFoundHttpException;
110110
case Request::METHOD_PUT:
111-
return null;
111+
return $value;
112112
case Request::METHOD_PATCH:
113113
throw new NotFoundHttpException;
114114
case Request::METHOD_DELETE:
115115
throw new NotFoundHttpException;
116+
default:
117+
return null;
116118
}
117-
118-
return null;
119119
};
120120
}
121121

@@ -233,10 +233,14 @@ public function store()
233233
/**
234234
* @inheritdoc
235235
*/
236-
public function update($model = null)
236+
public function update($model)
237237
{
238238
/** @var Model $model */
239-
$model = $model ?: new $this->model;
239+
if (!($model instanceof Model)) {
240+
$id = $model;
241+
$model = new $this->model;
242+
$model->{$model->getKeyName()} = $id;
243+
}
240244

241245
switch ($this->request->getMethod()) {
242246
case Request::METHOD_PUT:

src/Jarischaefer/HalApi/Controllers/HalApiResourceControllerContract.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Exception;
44
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Http\Request;
56
use Jarischaefer\HalApi\Exceptions\BadPostRequestException;
67
use Jarischaefer\HalApi\Exceptions\BadPutRequestException;
78
use Jarischaefer\HalApi\Exceptions\DatabaseConflictException;
@@ -96,12 +97,12 @@ public function store();
9697
* Handles PUT and PATCH requests trying to create or update a model. Parameters are taken from the JSON request
9798
* body. PUT requests must contain all fillable attributes.
9899
*
99-
* @param null $model
100+
* @param Model|mixed $model
100101
* @return array
101102
* @throws BadPutRequestException
102103
* @throws DatabaseSaveException
103104
*/
104-
public function update($model = null);
105+
public function update($model);
105106

106107
/**
107108
* @param $model

0 commit comments

Comments
 (0)