我试图在php文件中的单引号之间编写php代码。但是我收到了错误
解析错误:语法错误,意外'如果' (T_IF)
我的代码段
foreach($posts as $r){
$nestedData['action'] = '
<a href="'.url('/admin/post/').'/'.$r->id.'" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
'.if(Auth::user()->havePermission('post-delete')){.'
<form method="POST" action="'.url('/admin/post').'/'.$r->id.'" accept-charset="UTF-8" id="frm_'.$r->id.'" class="form form-delete-btn" title="Delete">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="'.csrf_token().'">
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete'.$r->id.'"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
'.}.'
<div>
...............
</div>';
}
答案 0 :(得分:2)
您正在尝试使用不受支持的subscription.ApplyChanges();
。
如果必须使用if clause
,则可以将语句分成2个语句或使用三元运算符:
使用If Clause
if clause
使用三元运算符
foreach($posts as $r){
$nestedData['action'] = '
<a href="'.url('/admin/post/').'/'.$r->id.'" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
';
if(Auth::user()->havePermission('post-delete')){
$nestedData['action'] .= '<form method="POST" action="'.url('/admin/post').'/'.$r->id.'" accept-charset="UTF-8" id="frm_'.$r->id.'" class="form form-delete-btn" title="Delete">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="'.csrf_token().'">
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete'.$r->id.'"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>';
}
$nestedData['action'] .= ' <div>
...............
</div>';
}
答案 1 :(得分:1)
您正在控制器中编写HTML代码。
制作一个刀片视图文件的效果更好。 for resources / views / post / action.blade.php
下的ex- action.blade.php然后您可以在控制器中获取该视图的数据
$nestedData['action'] = \View::make('posts.action')->with('r',$r)->render();
只需复制并粘贴其余部分即可。如下action.blade.php
<a href="{{url('/admin/post/')}}/{{$r->id}}" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
@if(\Auth::user()->havePermission("post-destroy"))
<form method="POST" action="{{url('/admin/post/')}}/{{$r->id}}" accept-charset="UTF-8" id="frm_{{$r->id}}" class="form form-delete-btn" title="Delete">
<input name="_method" type="hidden" value="DELETE">
@csrf
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete{{$r->id}}"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
@endif
<div> ... </div>
此处$ data是视图中所需的数据,如$ posts等。
现在您可以在变量$ nestedData [&#39; action&#39;]中查看。因此,您可以将它传递给您的视图,无需在控制器中编写完整的HTML。
答案 2 :(得分:0)
看起来你可能有&#39;或&#34;你的if之前不匹配。它很可能是一个语法。
答案 3 :(得分:0)
你应该把(!Auth :: user() - &gt; havePermission(&#39; post-delete&#39;))?: replace for .if(Auth :: user() - &gt; havePermission(& #39;后删除&#39;。)){
<?php
foreach($posts as $r) {
$nestedData['action'] = '
<a href="' . url('/admin/post/') . '/' . $r->id . '" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
' . (!Auth::user()->havePermission('post-delete')) ?: '
<form method="POST" action="' . url('/admin/post') . '/' . $r->id . '" accept-charset="UTF-8" id="frm_' . $r->id . '" class="form form-delete-btn" title="Delete">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="' . csrf_token() . '">
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete' . $r->id . '"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
<div>';
$nestedData['action'] .= ' <div>
...............
</div>';
}?>
您将需要使用a ternary运算符作为缩短的IF / Else语句: