鍍金池/ 問答/PHP  Python/ Laravel 5.4 如何將一個(gè)字段制空?

Laravel 5.4 如何將一個(gè)字段制空?

我數(shù)據(jù)庫會(huì)將一個(gè)字段,比如remark 設(shè)置為默認(rèn)值 string?,F(xiàn)在我添加數(shù)據(jù),remark為"你好",現(xiàn)在我需要將remark設(shè)置為空字符串。 api調(diào)用方 傳了空字符串,但是ConvertEmptyStringsToNull中間件 將empty轉(zhuǎn)為了null , 我應(yīng)該怎么處理。
目前我的Content-Type 是 application/json,我看國外也在討論這個(gè)問題。https://laracasts.com/discuss...

需要你的幫助,謝謝。

回答
編輯回答
過客

ConvertEmptyStringsToNull 中間件是 Laravel 5.4 才開始加入的。

By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the AppHttpKernel class. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null. This allows you to not have to worry about these normalization concerns in your routes and controllers.
If you would like to disable this behavior, you may remove the two middleware from your application's middleware stack by removing them from the $middleware property of your AppHttpKernel class.

看官方描述的意思就是為了規(guī)范化數(shù)據(jù)。

如果你確實(shí)不想這樣處理,可以在 app/Http/Kernel.php 文件中注釋掉此 middleware

    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, //注釋掉此行
    ];

或者:

從數(shù)據(jù)庫層面,把 remark 字段的默認(rèn)值設(shè)置為 空字符串

2017年5月29日 03:45