mercredi 27 janvier 2016

Retriving old values with many-to-many association

I usually use old function at my view files to retrieve old values when editing an entity. For example:

    {!! Form::model($shop, [
'action' => ['ShopController@update', 
$shop->id], 
'method' => 'patch']) !!}
            {!! Form::text('name', old('name')) !!}
            {!! Form::submit('Save', array('class' => 'btn')) !!}
    {!! Form::close() !!}

This works fine to simple fields, but this does not seem to work with 'many to many' (belongsToMany) relations. Supposing that a relation of this kind results in an array of objects, how can I retrieve them in the view without generating a lot of code?

I was thinking something like this, in a multiselect, for a shop that has a many-to-many relation with products:

{!! Form::select('products[]', $allProducts, old('products[]')) !!}

This does not seem to work because there are no products in the model, but through a relation model like: ShopProduct.

Anyone know a clever way to do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire