如何使用eloquent查询Laravel 5.2中的行数?

时间:2016-05-14 12:31:05

标签: php laravel laravel-5.2

我希望查询行数作为if语句的一部分。

我使用的代码如下:

 $company = DB::table('customers')->where('business', 'Auth::user()->name')->value('business');   
@if (count($company==0)

<div class="alert alert-danger" role="alert">
<strong>Please click here to register full company details</strong>

@endif

更新

{{$countCompany = App\customers::where('business', Auth::user()->name)->count()}}
@if ($countCompany==0)
<div class="alert alert-danger" role="alert">
<strong>Please click here to register full company details</strong>
@endif

谢谢

3 个答案:

答案 0 :(得分:0)

使用->count()方法

$count = $company->count();   

此处您还有错误,错过了)

@if (count($company==0))

答案 1 :(得分:0)

尝试使用Eloquent

$countCompany = App\Customer::where('business', Auth::user()->name)->count();

答案 2 :(得分:0)

我设法做了一个解决方法

<?php
$user = Auth::user()->name;
$company = DB::table('customers')->where('business', $user )->value('business');

?>

@if (is_null($company))

<div class="alert alert-danger" role="alert">
<strong>Please click here to register full company details</strong>
</div>
@endif 

适用于我需要的东西。