htantruc htantruc - 12 days ago 6
PHP Question

Show selected option from database in laravel

I am a newbie in laravel.
In my database, I have two tables, News(id, id_type) and Type(idtype,name). And in post News page, I use dropdown to select Type for my new news.
So, when I want to show the type name of this News, I do:

<div class="controls">
<select multiple name="Type">
@foreach($type as $type)
<option
@if($news->id_type == $type->idtype)
selected="selected"
@endif
value="{{$type->idtype}}">{{$type->name}}</option>
@endforeach

</select>
</div>


But I have wrong result for all type in Type tables.
Help me,please and thanks for your help!!

Answer

The single equals is for assignment while two equals is for comparison. Change this line to be

@if($news->id_type == $type->idtype)