我正在向用户管理页面添加搜索功能,并且正在使用AJAX进行搜索,但是我无法在页面上显示数据。它给出了一个错误。
我尝试在每侧添加单引号或双引号,但这每次都会给我一个错误
这是控制器功能:
public function search(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data =User::where('name', 'like', '%'.$query.'%')->get();
}
else
{
$data =User::orderBy('created_at' ,'desc')->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $merchant)//That how i am showing data on the page
{
$output .=
"<tr>
<td>"{$users->name}"
"@if($users->allowded == 0)" //I need to add these if else functionality to my code but these thing are giving error
"@endif"
</td>
<td>{{$users->email}}</td>
<td>{{$users->permission->permission}}</td>
<td>
<a href="#" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Salaried User
</span>
</a>
<a href="#" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Commesioned User
</span>
</a>
<a href="#" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Salaried and Commesionded User
</span>
</a>
</td>
<td>
@if($users->user_status)
<a href="" class="btn btn-xs btn-danger">
<span style="padding-right: 10px;">Make Him Unpaid User
</span>
</a>
@else
<a href="" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Paid User
</span>
</a>
@endif
</td>
<td>
@foreach($cashtransfer as $cash)
@if($cash->user_id == $users->id)
{{ $cash->amount}}
@endif
@endforeach
</td>
<td>
@if($users->allowded)
<a href="" class="btn btn-xs btn-danger">
<span style="padding-right: 16px;">Bloack User</span>
</a>
@else
<a href="" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Allow User</span>
</a>
@endif
</td>
<td>
</td>
<td>
<a href="" class="btn btn-xs btn-danger">
<span style="padding-right: 16px;">Delete User</span>
</a>
</td>
</tr>";
}
}
else
{
//This is The Out Variable which is taking data to the page
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($data);
}
}
这些是我每次都会遇到的错误:
// ->when($request->has('query'), function($q){
// $q->where('name','like','%'. request('query').'%');
// })->get();
$data =User::where('name', 'like', '%'.$query.'%')->get();
}
else
{
$data =User::orderBy('created_at' ,'desc')->get();
// $pagi = "<div> {$data->links()}</div>";
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $merchant)
{
$output .= "
<tr>
<td>"{$users->name}"
"@if($users->allowded == 0)"
"@endif"
</td>
<td>{{$users->email}}</td>
<td>{{$users->permission->permission}}</td>
<td><a href="#" class="btn btn-xs btn-success"> <span style="padding-right: 16px;">Make Him Salaried User</span></a><a href="#" class="btn btn-xs btn-success"> <span style="padding-right: 16px;">Make Him Commesioned User</span></a><a href="#" class="btn btn-xs btn-success"> <span style="padding-right: 16px;">Make Him Salaried and Commesionded User</span></a></td>
<td>
@if($users->user_status)
<a href="" class="btn btn-xs btn-danger"> <span style="padding-right: 10px;">Make Him Unpaid User</span></a>
@else
为什么我不能只发布它?
我不断收到错误消息,例如我需要更多详细信息
答案 0 :(得分:0)
您不能在普通琴弦内使用刀片。您必须使用普通的PHP来组成输出:
foreach($data as $merchant)
{
$optionalOutput = $users->allowded == 0 ? 'output if true' : 'output if false';
$output .= "<tr>
<td>{$users->name}
{$optionalOutput}
</td>";
// ...
}
或者您可以将代码移至刀片视图:
foreach($data as $merchant)
{
$output .= view('path.to.view', [$merchant, $users])->render();
}