我试图根据id获取某些用户的详细信息,如果数据库中不存在id,那么如何处理错误以及如何为assets / lang / en / somefile.php显示一些错误消息,说' errorMessage' => '发生了一些错误。请再试一次!。',
>>> from cerberus import Validator
>>> schema = {'name': {'type': 'string', 'required': True}}
>>> v = Validator(schema)
>>> document = {'bla': 'john doe'}
>>> v.validate(document)
False
>>>
答案 0 :(得分:1)
您可以使用find()而不是findOrFail()。控制器的方法应该看起来像......
public function methodName(Request $request, $id)
{
$city = City::with('locations')->find($id);
if ($city === null) {
return redirect()->back()->withErrors(['msg' => 'Can not find city.']);
} else {
return view('admin.city.viewCity', [
'city' => $city,
]);
}
}
或更短的版本:
return $city === null
? redirect()->back()->withErrors(['msg' => 'Can not find city.'])
: view('admin.city.viewCity', [
'city' => $city,
]);
答案 1 :(得分:1)
使用laravel,您可以将错误传递给这样的视图,
控制器,
Redirect::back()->withInput()->withErrors(['msg' => 'try again']);
视图,
<ul class="errors">
@foreach ($errors->all() as $message)
<li>{{ $message }}</li>
@endforeach
</ul>
答案 2 :(得分:0)
请试试这个
$city = City::with('locations')->findOrFail($id);
if(count($city) > 0 ){
return view('admin.city.viewCity',compact('city'))
}else{
return view('your message inside balde')
}
或
$city = City::with('locations')->findOrFail($id);
if(count($city) > 0 ){
return view('admin.city.viewCity',compact('city'))
}else{
$errorMessage = "User not found";
return view('admin.city.viewCity',compact('errorMessage'))
}
答案 3 :(得分:0)
请尝试以前的操作
if(!$city->isEmpty()){
return view('single', compact('city'));
}else{
return view('nodata', compact('city'));
}
注明:nodata表示您的刀片视图向用户显示消息错误