2016-09-16 133 views
0

我试图将deleted_at字段添加到mysql数据库中。我使用laravel 5.2 & https://github.com/nilportugues/laravel5-jsonapi在我的APIsoftDeletes()要求deleted_at存在于保存数据的请求中

迁移文件有这些:

$table->timestamps(); 
$table->softDeletes(); 

模型类:

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Database\Eloquent\SoftDeletes; 

class Scene extends Model 
{ 
    use SoftDeletes; 

    /** 
    * The attributes that should be mutated to dates. 
    * 
    * @var array 
    */ 
    protected $dates = ['deleted_at']; 

1 - 这一个数据保存到数据库的POST请求:

{ 
"data": { 
"type": "scene", 
"attributes": { 
    "title": "scene_N9DbG After S", 
    "version": "rLEjNpeebq", 
    "deleted_at": null 
    } 
} 
} 

2 - 这个不会将数据保存到数据库上:

{ 
"data": { 
"type": "scene", 
"attributes": { 
    "title": "scene_N9DbG After S", 
    "version": "rLEjNpeebq" 
    } 
} 
} 

我得到这个错误:

DataException in DataObject.php line 62: 
An error with the provided data occured. 

at DataObject::assertPost(array('type' => 'scene', 'attributes' => array('title' => 'scene_N9DbG After S', 'version' => 'rLEjNpeebq', 'created_at' => '2016-09-16 05:46:51', 'updated_at' => '2016-09-16 05:46:51')), object(JsonApiSerializer), 'App\Scene', object(ErrorBag)) in CreateResource.php line 58 

这是造成的,因为laravel或我使用的包?如何在不发送deleted_at的情况下保存数据?我想我不必每次发送请求都发送deleted_at。请让我知道是否需要更多数据。

其次文档: https://laravel.com/docs/5.2/eloquent#soft-deleting

回答

1

这是从你的包。从文档:

POST requires all member attributes to be accepted, even those hidden by the mapper.

如果你想改变这一点,它看起来像你需要你的Transformer对象上定义一个getRequiredProperties()方法。

/** 
* List the fields that are mandatory in a persitence action (POST/PUT). 
* If empty array is returned, all fields are mandatory. 
*/ 
public function getRequiredProperties() 
{ 
    return ['title', 'version']; 
} 

我以前从未使用过的包装,因此,如果实际工作我无法测试,不过这是我已经能够通过文档,代码看后收集,并发出一一会儿。

+0

虽然没有工作。仍然得到相同的错误 – maestar

+0

@ red-cannibal在2.4.4版本中,对这个功能的支持看起来只是添加到了[nilportugues/json-api](https://github.com/nilportugues/php-json-api) ,这是在14天前被标记的。你必须使用[nilportugues/laravel5-json-api](https://github.com/nilportugues/laravel5-jsonapi/blob/2.4.4/composer.json)version> = 2.4.4(这是第一个版本这取决于nilportugues/json-api^2.4),并且最近更新了你的依赖关系('composer update nilportugues/laravel5-json-api')。 – patricus

+0

这一个抛出不同的错误'不能在类App \ Model \ Database \ Scene中添加所需的属性0,因为它不存在' – maestar