php - 比较控制器上的字符串
问题描述
我有一个包含 2 个项目(胡萝卜和柠檬)的下拉列表。我想Carrot
比较Lemon
。如果它是蔬菜,则添加被验证,否则它会在表单中被阻止。
在我的代码中,验证不正确...我的 2 个值通过...
public function store(Request $request)
{
$this->validate($request, [
'vegetables' => 'required|min:3'
]);
$listVegetable = Exo::where('vegetables', $request->get('vegetables'))->count();
$word = "Carrot";
if ($listVegetable == $word){
Exo::create($request->all());
return redirect()->route('exos.index')
->with('success', 'new data created successfully');
}
else{
return redirect()->route('exos.index')
->with('error', 'Not vegetable');
}
}
解决方案
随着$listVegetable = Exo::where('vegetables', $request->get('vegetables'))->count();
您将数据库中的蔬菜数量作为整数值。因此,您与carrot
字符串的比较没有意义。你应该写first()
而不是count()
然后我猜你需要访问这个对象的 ->name 属性,这取决于你的模型有哪些行。
推荐阅读
- c# - Unity 资源.负载管理
- javascript - 为什么使用“array[row][col]= usedspace;” 不能根据每个变量(行或列)的增加来增加特定的列和行?
- laravel - Laravel - 如何将参数从 js 传递给动作链接
- objective-c - 仅获取 obj-c 类的公共方法
- javascript - 将数据从反应表传递到反应模式
- angular - 我可以在组件上使用共享模块提供的服务吗?
- google-apps-script - 为什么我在运行 AdminDirectory.Members.remove(groupEmail, userEmail); 时缺少必填字段:memberKey?
- java - 为什么我会遇到这个错误:java.lang.ExceptionInInitializerError
- swift - 在字符串中查找数字并返回位置和长度
- java - 如何以分钟/公里计算速度