laravel如果在控制器中使用验证导致白色黑屏

时间:2019-06-02 08:27:47

标签: laravel laravel-5

我正在对请求中的某些字段执行验证。在我的案例“ title”中,当我在此操作中使用任何验证时,它给我没有任何信息的白色黑屏,我尝试使用单独的请求验证文件...如果删除验证,一切都会好的!为什么会这样?

(php错误日志和laravel调试已打开)。

这是目标控制器操作的dd()结果:

array:9 [▼
  "_method" => "POST"
  "_token" => "XW8ifwaPFlnA31mqlF3rjLz0iDyAxSpayr6G11WJ"
  "hotel_id" => "63"
  "lang" => "fa"
  "title" => null
  "meta" => null
  "keywords" => null
  "address" => null
  "cancelterms" => null
]

这是我的表单操作:

public function addFaLang(Request $request)
{

      dd($request->all());


     $this->validate(
       $request,
         ['title' => 'required'],
         ['title.required' => 'please fill this']
     );



    $hotel_id = $request->get('hotel_id');


    $hotel_translates = HotelTranslate::where(['hotel_id' =>  $hotel_id, 'lang' => 'fa'])->count();

    if ($hotel_translates) {
        HotelTranslate::where(['hotel_id' =>  $hotel_id, 'lang' => 'fa'])->delete();
    }

    if ($request->title) {
        $hotel_tra = HotelTranslate::create($request->only( 'hotel_id', 'title', 'meta', 'keywords', 'address', 'cancel_term', 'lang'));
        $hotel_tra->save();
    }


    $result = array('result' => 1, 'idhotel' => $hotel_id, 'message' => 'success');

    return view('adminarea.hotel.create-hotel-ar-lang', $result);
}

这是我的表格:

form method="post" action="{{ route('hotel.addfalang') }}">
    @method('POST')
    @csrf
    <input type="hidden" name="hotel_id" value="{{ $lastid }}">

    <table class="table table-bordered table-striped">
        <div class="progress">
            <div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="40" aria-valuemin="0"
                aria-valuemax="100" style="width: 40%"></div>
        </div>
        <tr>
            <td>
                <label>زبان فارسی
                    <input type="hidden" name="lang" value="fa">

                </label>

            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="title" class="form-control {{ $errors->has('title')? 'is-invalid': ''  }}" placeholder="نام هتل" style="width: 100%">
                @if ($errors->has('title'))
                <div class="invalid-feedback">
                    <strong>{{ $errors->first('title')}}</strong>
                </div>
                @endif
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="meta" class="form-control" placeholder="متا" style="width: 100%">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="keywords" class="form-control" placeholder="کلید واژه"
                    style="width: 100%">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="address" class="form-control" placeholder="آدرس" style="width: 100%">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="cancelterms" class="form-control" placeholder="قوانین کنسلی"
                    style="width: 100%">
            </td>
        </tr>

        <tr>
            <td>
                <input type='submit' value='مرحله بعد' class='btn btn-success' style="float:left">
            </td>
        </tr>
    </table>
</form>

1 个答案:

答案 0 :(得分:1)

尝试:

$validatedData = $request->validate(
    ['title' => 'required']
);

On the documentation验证方法应用于请求实例,而不应用于控制器。