Reputation: 75
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!!
Upvotes: 2
Views: 2939
Reputation: 7222
The code block within @foreach
can be modified to serve the purpose of selecting a news type corresponding to each news.
<select name="Type" id="Type" class="form-control">
@foreach($type AS $data)
<option value="{{ $data->idtype }}">{{ $data->name }}
</option>
@endforeach
</select>
The @if
codeblock is not working as you think it is supposed to. So, better remove it.
Now if you want to get the type associated with each news then you can very well specify it in your Controller and get the news as per the type_id
.
Code snippet from your Controller will be more helpful.
Upvotes: 0
Reputation: 6581
The single equals is for assignment while two equals is for comparison. Change this line to be
@if($news->id_type == $type->idtype)
Upvotes: 3