2016-09-16 55 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