我将视图传递给广告系列。如果我做
{{ dd($campaign->campaignCreatives->campaignCreativesData) }}
我得到像
这样的东西Collection {#348 ▼
#items: array:2 [▼
0 => CampaignCreativesData {#349 ▼
#attributes: array:7 [▼
"id" => "5"
"name" => "creativeOption"
"label" => "Other"
"value" => "sdfsdfsdfsdfsdf"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
}
1 => CampaignCreativesData {#350 ▼
#attributes: array:7 [▼
"id" => "4"
"name" => "creativeOption"
"label" => "checkboxSelection"
"value" => "Animated GIF"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
}
]
}
如果我把它投射到数组
{{ dd($campaign->campaignCreatives->campaignCreativesData->toArray()) }}
我得到像
这样的东西array:2 [▼
0 => array:7 [▼
"id" => "5"
"name" => "creativeOption"
"label" => "Other"
"value" => "sdfsdfsdfsdfsdf"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
1 => array:7 [▼
"id" => "4"
"name" => "creativeOption"
"label" => "checkboxSelection"
"value" => "Animated GIF"
"campaignCreativesId" => "5"
"created_at" => "2016-03-01 10:24:38"
"updated_at" => "2016-03-01 10:24:38"
]
]
现在我有几个复选框,如果值在数组中,我需要选中复选框。目前我有类似的东西
{!! Form::checkbox('creativeOptions[]', 'Animated GIF', in_array('Animated GIF', $campaign->campaignCreatives->campaignCreativesData->toArray()), ['class' => 'styled', 'id' => 'checkbox1']) !!}
如您所见,动画GIF位于数组中,但未选中复选框。
如何根据数组中的内容选中复选框?
由于
答案 0 :(得分:1)
您可以在Laravel集合中使用方法contains
。
{!! Form::checkbox('creativeOptions[]',
'Animated GIF',
$campaign->campaignCreatives->campaignCreativesData->contains('value', 'Animated GIF'),
['class' => 'styled', 'id' => 'checkbox1']) !!}