我需要什么:
使用以下数据库列clay_plan_type
+----+------------------+
| id | clay_plan_type |
+----+------------------+
| 5 | Single Shooter |
| 6 | Lunch Sponsor |
| 7 | Beverage Sponsor |
+----+------------------+
如果我的选择字段的值与clay_plan_type
中的值匹配,我想禁用表单中的选择选项
查看示例:
@if ($sponsortype === "Beverage Sponsor")
<option value="" disabled>Beverage Sponsor: SOLD OUT</option>
@else
<option value="Beverage Sponsor">Beverage Sponsor: $250</option>
@endif
etc...
控制器示例:
$sponsortype = DB::table('clay_shoots')->select('clay_plan_type')->get();
return view('clayshoot.sponsorform',compact('sponsortype'));
dd($sponsortype);
出现以下内容:
Collection {#855 ▼
#items: array:3 [▼
0 => {#801 ▼
+"clay_plan_type": "Single Shooter"
}
1 => {#802 ▼
+"clay_plan_type": "Lunch Sponsor"
}
2 => {#803 ▼
+"clay_plan_type": "Beverage Sponsor"
}
]
}
在视图中运行if statement
时没有错误,但是没有得到正确的结果。它充当`if语句不存在。
所以我尝试添加$sponsortype->clay_plan_type
并收到错误消息:
此集合实例上不存在属性[clay_plan_type]。
如果我在foreach loop
中运行,则可以在“视图”中运行,但在选择字段中会重复获得同一项目的多次重复。 (见下图)
@foreach($sponsortype as $st)
@if ($st->clay_plan_type == "Beverage Sponsor" )
YUP
@else
NOPE
@endif
@endforeach
// OUTPUT: NOPE NOPE YUP ("Beverage Sponsor" is the last record in the table)
答案 0 :(得分:3)
您可以使用contains
收集方法并传递键/值对。
来自docs:
您还可以将键/值对传递给contains方法,该方法 将确定给定对是否存在于集合中:
$collection = collect([
['product' => 'Desk', 'price' => 200],
['product' => 'Chair', 'price' => 100],
]);
$collection->contains('product', 'Bookcase');
// false
在您的情况下:
@if ($sponsortype->contains("clay_plan_type", "Beverage Sponsor"))
<option value="" disabled>Beverage Sponsor: SOLD OUT</option>
@else
<option value="Beverage Sponsor">Beverage Sponsor: $250</option>
@endif