Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据查询结果显示链接_Php_Html_Laravel - Fatal编程技术网

Php 根据查询结果显示链接

Php 根据查询结果显示链接,php,html,laravel,Php,Html,Laravel,如果查询有结果,我想显示一个链接。当表为空时,我得到一个错误。提前谢谢 html: ... <div class="notification"> <header><h3>Notification</h3></header> @if(Auth::user()->friends()->where(['status', 'received'])->first())

如果查询有结果,我想显示一个链接。当表为空时,我得到一个错误。提前谢谢

html:

...
<div class="notification">
            <header><h3>Notification</h3></header>
            @if(Auth::user()->friends()->where(['status', 'received'])->first())
                <a href="" class="">You have a new request!</a>
                @endif
        </div>
...

开始时,表为空,除非用户在表中保存一些信息。如何解决此问题?

除了
where
子句外,查询没有问题,您不应该在那里传递这样的数组,因此请尝试用以下内容替换条件:

@if(Auth::user()->friends()->where('status', 'received')->first())
first
返回null,如果未找到
false,则返回null,它将正常工作

作为建议,我不会将查询放在视图中,我会更好地从控制器传递它。因此,您可以存储到变量中,例如:

$newFriends = Auth::user()->friends()->where('status', 'received')->first();

return view('your_view_name', compact('newFriends'));
然后在视图中

@if($newFriends)
...
@endif

问题在于查询中的where子句,您正在传递一个数组,但没有在索引和值之间使用array=>运算符,因此php会认为
status
received
都是数组值,不会使用
status
作为索引,以了解更多细节

所以为了解决这个问题,您可以使用where子句而不使用数组,因为您只检查一个索引

Auth::user()->friends()->where('status', 'received')->first()
也可以使用数组,但请确保使用=>运算符

Auth::user()->friends()->where(['status' => 'received'])->first()

可以作为数组传递。检查文档。关于变量的观点很好,但它不会改变结果。像他那样传递数组真的很好吗?他传递了一个没有任何值的项数组。正如文档所述,有一个数组数组,每个项目数组包含一列、一个等式和一个值。如果未指定,则假定运算符为
=
。也许数组中不是这样?我现在无法测试,但我希望它也能以同样的方式工作。从上面的错误中可以看出,它尝试执行
0=status
1=received
操作,而他尝试执行的是
status=received
,因此他只需要删除数组,因为他只使用了一列作为where子句,不是多个列。我想是的,但是他需要做数组的一项,因为现在它使用索引作为列名,而不是第一项。但在任何情况下,在进行单个比较时都不需要数组。
Auth::user()->friends()->where(['status' => 'received'])->first()