M
M
Maxim2020-07-09 11:51:55
Laravel
Maxim, 2020-07-09 11:51:55

Check existing items when listing user data?

there is a table of interests
5f06da10c18fd403062527.png

$interests = Interest::where('published','=',1)->get();

and a table of user interests
5f06da3018aa1089653426.png

$userInterests = UserInterest::where('user_id', '=', Auth::user()->id)->get();


how to mark checked those that the user already has when displaying a list of interests?

@forelse($user_interests as $interest)
     <li>
        <div class="checkbox-input">
           <input type="checkbox" id="interest_{{$interest->id}}" name="interests[]" @isset($user_interests){{ in_array( $interest->id, $interests ) ? ' checked' : '' }} @endisset value="{{$interest->id}}">
           <label for="interest_{{$interest->id}}">{{$interest->title}}</label>
      </div>
   </li>
@empty
@endforelse

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-07-09
@Aslero

$userInterests = UserInterest::where('user_id', Auth::id())->pluck('interest_id');

{{ $userInterests->contains($interest->id) ? ' checked' : '' }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question