我需要什么:
使用以下DB列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时没有出现错误,但没有得到正确的结果。它的作用是‘is语句不存在。
因此,我尝试添加$sponsortype->clay_plan_type并得到错误:
属性clay_plan_type不存在于此集合实例上。
如果我在一个foreach loop中运行,它可以在视图中工作,但是我在select字段中得到了多个相同项的重复。(见下图)
@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)

希望我能用你理解的方式来解释这件事。如果你需要更多的信息,请告诉我。任何帮助,以了解这一点,将不胜感激。
发布于 2019-03-25 14:51:49
您可以使用contains集合方法并传递一个键/值对。
来自文档
您还可以将一个键/值对传递给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>
@endifhttps://stackoverflow.com/questions/55339860
复制相似问题