mardi 1 mars 2016

Laravel 5 - Blade casting collection to array and checking its value

I pass my view a Campaign Collection. If I do

{{ dd($campaign->campaignCreatives->campaignCreativesData) }}

I get something like

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"
      ]
    }
  ]
}

If I cast it to an array

{{ dd($campaign->campaignCreatives->campaignCreativesData->toArray()) }}

I get something like

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"
  ]
]

Now I have several checkboxes, and if the value is in the array, I need to check the checkbox. At the moment I have something like this

{!! Form::checkbox('creativeOptions[]', 'Animated GIF', in_array('Animated GIF', $campaign->campaignCreatives->campaignCreativesData->toArray()), ['class' => 'styled', 'id' => 'checkbox1']) !!}

As you can see, Animated GIF is in the array, but the checkbox is not checked.

How can I get the checkbox checked based on what is in the array?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire