I want to pass data to select box (Larvel)

Asked 2 years ago, Updated 2 years ago, 42 views

What you want to do

What's in the select box (hobby)
·Walking
·Cooking
·Movies

If you have a 1 ID (hobby: walking), I would like to make the select box (hobby) a walk.

Current Code (★★★★.blade.php)

<select name="type">
  @foreach($typeOption as$type=>$displayName)
    <option value="{{$type}}}">{{$displayName}}</option>
  @endforeach
</select>


in this state: I can make a select box, but
The item for each ID is not selected.
(The ID1 person's select box is not a walk)

I look forward to hearing from someone.

html laravel

2022-09-29 22:12

1 Answers

We will proceed with the assumption that the user with ID 1 has the following data in the variable $userdata.

//ID1 user data
$userdata=[
  'id' = > 1,
  'hobby' = > 1,
];

In such a case, you can place a particular choice in the selected state as follows:

<select name="type">
  @foreach($typeOption as$type=>$displayName)
    <option value="{{$type}}"@if($userdata['hobby']===$type)selected@endif>{{$displayName}}</option>
  @endforeach
</select>

If the hobby data is specified in the users table and Auth::user() can retrieve the information, you may want to rewrite $user['hobby'] in the above code as Auth::user()->hobby.


2022-09-29 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.