javascript - 为 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')),
]);
解决方案
您根本没有传递该输入。您有表单标签之外的输入标签。
您的输入也是一个复选框。他们只有在被“检查”时才会被提交。如果没有,他们根本不会被发送。
调整您的foreach
循环以处理输入可能不存在的事实:
foreach ($request->input('target_id', []) as $key => $target_id)
推荐阅读
- python - 访问列表 Python 中的一个元素
- python - ret, frame = video_capture.read() ValueError: 要解压的值太多(预期为 2)
- xamarin.forms - 如何停止在 SQLite 数据库中重复的项目?
- java - 需要 6 秒的数据库视图的输出缓慢和提高的原因是什么,突然需要 1.5 秒?
- java - 将 JDBC 接收器连接器连接到 CrateDB
- python - 使用 Jquery AJAX 从服务器获取数据,然后尝试使用 Jinja 2 格式创建一个 url
- ruby-on-rails - 在特定位置向数组哈希 ruby 添加值
- java - Spring Validation 在服务层级别不起作用
- pagination - Jasper-Reports - Pagefitting 子报告在包含在主报告中时被拆分
- php - 如何在 WooCommerce 子主题中更改 FlexSlider 选项