laravel - 如果刀片模板中设置了变量,如何避免每次检查?拉拉维尔 8
问题描述
我知道这样的事情:
{{ old('contents', $page->contents ?? null) }}
但是更复杂的情况呢,比如复选框和选择?
<select id="custom_template" name="custom_template">
{{--default empty option, if nothing is selected yet--}}
<option label=" " {{ ($page->custom_template == null)? "selected" : "" }}></option>
@foreach($templates as $template)
<option value="{{ $template->id }}" {{ ($page->custom_template == $template->id)? "selected" : "" }}>{{ $template->name }}</option>
@endforeach
</select>
我需要避免检查@isset($page)
并检查旧输入。我怎样才能在那个选择输入中做到这一点?
解决方案
您可以使用类型提示来避免“isset”。
你的控制器
/**
* Show the form for creating a new resource.
*
* @param \App\Entities\Page $page
*
* @return \Illuminate\View\View
*/
public function create(\App\Entities\Page $page)
{
return view('page', compact('page'));
}
在创建函数中使用“类型提示”将创建页面实体的空集合。无需在视图中检查 isset 条件,$page->custom_template
现在会给你 null,而不是错误。
和你的观点,检查旧的输入。
<select id="custom_template" name="custom_template">
<option value="">Select Option</option>
@foreach($templates as $template)
<option value="{{ $template->id }}" {{ (old('custom_template', $page->custom_template) == $template->id) ? 'selected' : '' }}>{{ $template->name }}</option>
@endforeach
</select>
通过使用上述条件,您可以使用相同的视图来创建和编辑功能。
希望,这将解决您的问题。
推荐阅读
- android - 单击登录按钮时应用程序崩溃(简单登录注册测试)
- spring-boot - Spring thymeleaf 模板引擎找不到模板文件
- php - elFinder - 如何从 AWS S3 存储桶打开/显示特定文件夹
- jquery - Ajax 请求正常,但在错误响应文本中接收数据
- amazon-web-services - AWS ECS:如何在不使用 ALB 的情况下在 ECS 集群内的服务之间进行通信?有办法吗?
- go - 如何在 go 中 time.Parse() 逗号分隔毫秒
- html - 右上角的引导按钮
- python - 在梯度更新期间更新张量矩阵的特定行?
- java - HMACSHA256.ComputeHash(byte[] arr, int , int ) 与 Java Mac.doFinal(byte[] arr) 的输出不匹配
- javascript - 在赛普拉斯,我的餐桌数来不及评估