2

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!!

1
  • Please provide the code snippet of your respective Controller's method Commented Nov 25, 2016 at 3:28

2 Answers 2

3

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

@if($news->id_type == $type->idtype)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.