laravel5.5中的刀片文件中未显示错误

时间:2017-12-13 07:55:36

标签: php jquery ajax laravel-5.5

我在表单上应用验证。我在json中收到错误消息但是,刀片文件中没有显示错误。

以下是代码,我已在控制器文件中写入验证: -

namespace App\Http\Controllers\SpecialOffers;

use Illuminate\Support\Facades\Mail;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Auth;

use Validator;

use Illuminate\Support\MessageBag;

class SpecialoffersController extends Controller
{

 /**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function NewPromotion()
{
    return view('new-promotion');
}

public function store(Request $request)
{

   // Validate and store the values....
    $validator = Validator::make($request->all(), [
        'promotion_name' => 'required',
        'service' => 'required',
        'promotion' => 'required',
        'promotion_details' => 'required',

    ]);

    if ($validator->passes()) {
        // Get the currently authenticated user...
    $user = Auth::user();

    // Get the currently authenticated user's ID...
    $id = Auth::id();

    $promotion_name = $_POST['promotion_name'];
    $service = $_POST['service'];
    $promotion = $_POST['promotion'];
    $promotion_details = $_POST['promotion_details'];

    $insert = DB::insert('insert into special_offers_details(user_id,promotion_name,service,promotion_limited,promotion_details,price,sent_to) values(?,?,?,?,?,?,?)', ['',$promotion_name, $service, $promotion, $promotion_details,'','']);

    $result = array();

    if($insert==1)
    {
        $result['message']='Successfully inserted';
        $result['html']='1';
    }
    else
    {
        $result['message']='error';
        $result['html']='0';
    }

    //echo json_encode($result);
    return json_encode($result);
    }

    if($validator->fails()) {
        return response()->json(['error'=>$validator->errors()->all()]);
        //return Redirect::back()->withErrors($validator);
    }

}

} 错误显示响应但未显示在视图文件中。以下代码是视图文件中用于调用错误的代码: -

@if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

我不明白错误没有出现的原因。缺少什么?

1 个答案:

答案 0 :(得分:0)

由于你以JSON格式返回它,因此不会由刀片模板呈现,因为php无法直接读取JSON,只是以php数组格式返回错误响应