Search code examples
phplaravellaravel-7

How to populate a Multiple Select with fixed option in Laravel 7


I have an edit.blade.php ( User Profile ) where I have a multiple Select that should show fixed options. when i Edit the profile for the first time, i receive error.

Error :

Call to a member function toArray() on null (View: /Users/mnamazi/Sites/localhost/serresokhan/resources/views/panel/profile/edit.blade.php)

edit.blade.php :

<div class="col-xl-6">
     <div class="form-group">
         <label for="favorite">علاقمند شرکت در مسابقه</label>
         <select name="favorite[]" class="form-control kt-selectpicker @error('favorite') is-invalid @enderror" multiple>
             <option @if(in_array(1, $user->favorite)->toArray()) selected @endif value="1">گویندگی</option>
             <option @if(in_array(2, $user->favorite)->toArray()) selected @endif value="2">اجرا</option>
             <option @if(in_array(3, $user->favorite)->toArray()) selected @endif value="3">سخنوری</option>
         </select>
         @error('favorite')
            <div class="invalid-feedback"> {{ $message }}</div>
         @enderror
     </div>
</div>

Solution

  • Not sure what the purpose of the in_array and ->toArray() is for as there is not much information on if the favorite is a column or a relation on the model.

    If the favorite is a column on the users table, then you can try

    <div class="col-xl-6">
         <div class="form-group">
             <label for="favorite">علاقمند شرکت در مسابقه</label>
             <select name="favorite[]" class="form-control kt-selectpicker @error('favorite') is-invalid @enderror" multiple>
                 <option @if($user->favorite == 1) selected @endif value="1">گویندگی</option>
                 <option @if($user->favorite == 2) selected @endif value="2">اجرا</option>
                 <option @if($user->favorite == 3) selected @endif value="3">سخنوری</option>
             </select>
             @error('favorite')
                <div class="invalid-feedback"> {{ $message }}</div>
             @enderror
         </div>
    </div>
    

    But if it is a relation on the model, then

    <div class="col-xl-6">
             <div class="form-group">
                 <label for="favorite">علاقمند شرکت در مسابقه</label>
                 <select name="favorite[]" class="form-control kt-selectpicker @error('favorite') is-invalid @enderror" multiple>
                     <option @if($user->favorite->id == 1) selected @endif value="1">گویندگی</option>
                     <option @if($user->favorite->id == 2) selected @endif value="2">اجرا</option>
                     <option @if($user->favorite->id == 3) selected @endif value="3">سخنوری</option>
                 </select>
                 @error('favorite')
                    <div class="invalid-feedback"> {{ $message }}</div>
                 @enderror
             </div>
        </div>
    

    Based on your comment that it is an array, you can try this.

    Please make sure you have the favorite cast to an array on your model.

    public class User extends Model
    {
        protected $casts = [
           'favorite'=>'array',
        ];
    }
    

    Once this is done, Laravel will automatically cast the favorite attribute into an array and can be accessed on your view using in_array.

    <div class="col-xl-6">
         <div class="form-group">
             <label for="favorite">علاقمند شرکت در مسابقه</label>
             <select name="favorite[]" class="form-control kt-selectpicker @error('favorite') is-invalid @enderror" multiple>
                 <option @if(in_array(1,$user->favorite)) selected @endif value="1">گویندگی</option>
                 <option @if(in_array(2,$user->favorite)) selected @endif value="2">اجرا</option>
                 <option @if(in_array(3,$user->favorite)) selected @endif value="3">سخنوری</option>
             </select>
             @error('favorite')
                <div class="invalid-feedback"> {{ $message }}</div>
             @enderror
         </div>
    </div>