首页 > 解决方案 > 为 foreach() 提供的参数无效 - 错误 Laravel

问题描述

我为多条消息创建了一个函数,但是当我尝试发送消息时,我收到了这个错误:Invalid argument supplied for foreach()。我target_id[]在表单中命名了该字段,但我不知道为什么不起作用。

这是我的看法:

     <h3>Share with your contacts</h3>

                                 <button type="button"  class="btn btn-primary " data-toggle="modal" href="#myModal2" style="margin-bottom: 20px;background-color: #18ba9b;border-color: #18ba9b;color: white">
                            Select contacts
                        </button>
                                 {!! Form::open(['class' => 'sky-form','url' => 'mailbox/compose-message','id' => 'sky-form4']) !!}

                                     <dd>

                             <div id="span"></div>

                             <dd>
                                 <section>
                                     <label class="input">
                                         <input type="text" placeholder="Subject" name="subject" value="Shared with you - {{ $noticeboard_data['subject'] }}">
                                     </label>
                                 </section>
                             </dd>

                             <dd>
                                 <section>
                                     <label class="textarea">
                                             <textarea  id="textarea" name="message" rows="6">Look what I found...
                        <span>
                        <a href="{{ url($noticeboard_data['type_url']) }}/{{ $noticeboard_data['id'] }}_{{ Slugify::slugify($noticeboard_data['subject']) }}" class="noticeboard-subject">{{ $noticeboard_data['subject'] }}</a>
                        </span>


                                                 </textarea>
                                     </label>
                                 </section>
                             </dd>

                             <hr>
                         @if(Sentinel::check())
                             @if(Sentinel::check()->premium == 0)
                                 @if(Sentinel::check()->email_count < 25)
                                     {!! Form::submit('Send message' ,['class' => 'btn-u']) !!}
                                 @else
                                     {!! Form::submit('Send message' ,['class' => 'btn-u','disabled']) !!}
                                 @endif
                             @else
                                 {!! Form::submit('Send message' ,['class' => 'btn-u']) !!}
                             @endif
                         @endif
                         {!! Form::close() !!}

                             </div>
                                     </div>


                             </div>
                         </div>
                     </div>
                 </div>
                 <div class="modal fade rotate" id="myModal2">
                     <div class="modal-dialog">
                         <div class="modal-content">
                             <div class="modal-header">
                                 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                    <h4 class="modal-title">Modal 2</h4>

                             </div>
                             <div class="container"></div>
                             <div class="modal-body">
                                             <ul class="nav nav-tabs">
                                 <li class="active"><a  data-toggle="tab" href="#individuals" id="my-individual">Individuals</a></li>
                                 <li><a  data-toggle="tab" href="#organization" id="my-organization">Organizations</a></li>

                             </ul>
                             <div class="tab-content">
                                    <div id="individuals" class="tab-pane fade in active">






                                <input type="text" class="form-control" id="populerNameKey" placeholder="Search for contacts" style="margin-top: 20px; margin-bottom: 20px; width: 38%">

                                <section>




                             <ul style="list-style: none;right: 40px; position: relative" id="destPopuler">
                             @foreach($contact_username as $user)


                             <li class="country" style="list-style: none;margin-bottom: 15px">
                             <div class="products">
                             <h3  style="font-size: 14px;margin-bottom: 5px"><a href="{{ url('') }}/{{$user->username }}"><input class="my_div" id="{{$user->username}}" name="target_id[]" type="checkbox" value="{{ $user->id }}" <?php if(isset($replyMessage)){ if($replyMessage == $user->id) { echo "checked"; }} ?>>
                                 <?php $img = "thumbnail/".$user->profile_picture; ?>
                                    @if(@getimagesize($img))
                                    <img style="position: relative;border-radius: 50%;height: 30px;width: 30px;right: -5px;top: 4px"  src="{{ url(''.$img) }}" alt="">
                                    @endif
                                 <span  style="padding: 5px" class="name">{{ $user->username }}</a></h3>
                             </div>
                             </li>


                             </ul>
                             @endforeach
                             </section>

                                    </div>
                                </div>



                             </div>
                             <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a>
                    <a href="#" class="btn btn-primary">Save changes</a>

                             </div>
                         </div>
                     </div>
                 </div>

这是我的控制器:

public function composeMessage(Request $request)
    {
        if ($user = Sentinel::check())
        {
            // return $request->all();
            $data = $this->data;
            $id = $user->id;

            foreach ($request['target_id'] as $key => $target_id)
            {

                $user = User::findOrfail($id);
                $target = User::findOrfail($target_id);

                $mailbox = Mailbox::create([
                    'sender_id' => $id,
                    'target_username' => $target->username,
                    'target_id' => $target->id,
                    'subject' => $request['subject'],
                    'message' => $request['message'],
                    'date' => strtotime(date('Y-m-d')),
                ]);

标签: javascriptphphtmllaravel

解决方案


您根本没有传递该输入。您有表单标签之外的输入标签。

您的输入也是一个复选框。他们只有在被“检查”时才会被提交。如果没有,他们根本不会被发送。

调整您的foreach循环以处理输入可能不存在的事实:

foreach ($request->input('target_id', []) as $key => $target_id)

推荐阅读