如果数据为空laravel则显示消息

时间:2017-12-20 07:40:03

标签: php laravel

我想在我的刀片中显示一条消息,如果我的地址,姓名或号码为空,请说明这些字段为空请填写,如果已经填写,则不要显示任何内容。

我确实喜欢这个,但是在blade.php中。以前我已经问了类似的东西,但这次我想要显示一条消息。我试着按照我问的问题但是由于某种原因它继续显示消息"请填写它们"即使我的数据包含数据库中的值。

Redirect to page when value is null in another table laravel

这是我的代码:

<?php 
     $additional_info = DB::table('additional_informations') 
                            ->whereNull('address')
                            ->orWhereNull('name')
                            ->orWhereNull('number')
                            ->get();
        if( $additional_info->count())
        echo "test";

 ?>

4 个答案:

答案 0 :(得分:3)

假设您正在处理index.blade.php页面,该怎么回事

InformationController - Controller

public function index($id) {
         $additional_info = DB::table('additional_informations') 
                            ->whereNull('address')
                            ->orWhereNull('name')
                            ->orWhereNull('number')
                            ->where('id', '=', $id)
                            ->first();
          return view('index', compact('id','additional_info '));

}

index.blade.php - 查看

<div>
    @if(count($additional_info) > 0)
        Here are the rows that has Null values: <br/>
             Please go back and fill up the values of
             @if($additional_info->EC_name == null)
                 EC_name 
             @endif
             @if($additional_info->EC_relationship== null)
                 EC_relationship
             @endif
             @if($additional_info->EC_address== null)
                 EC_address
             @endif
             // and so on. There is a shorter way but I'm not in my laptop right now. But this should work fine

    @else
         {{ $id }} has no Null data found
    @endif
</div>

告诉我你是否需要更多的帮助。干杯

答案 1 :(得分:2)

对于这样的路线:

Route::get('/userAddInfo/{id}/AddInfo','VerificationController@AddInfo');

您可以在方法中获取用户ID作为参数,然后在查询中使用它,如下所示:

public function AddInfo($id) {
    $additional_info = DB::table('additional_informations')
                            ->where('user_id', $id)
                            ->where(function ($query) {
                                    $query->whereNull('EC_address')
                                          ->orWhereNull('EC_name')
                                          ->orWhereNull('EC_number');
                                })
                            ->get(); 
    return view('AddVerificationInfo',compact('id','additional_info')); 
}

在视图中:

@if($additional_info->count())
    "Please fill this up"
@endif

答案 2 :(得分:1)

/* In controller */

$additional_info = DB::table('additional_informations') 
                            ->whereNull('address')
                            ->orWhereNull('name')
                            ->orWhereNull('number')
                            ->get()->toArray(); // to convert output into array 

/* In view (blade template) */

@if(!empty($additional_info))

echo "<pre>";
print_r($additional_info);

@else 

echo "No data found";

@endif

答案 3 :(得分:0)

您应该将逻辑移动到控制器中,然后将$noAdditionalInfo之类的变量传递给视图

然后你可以使用刀片标签:

@if($noAdditionalInfo)
    <!-- html goes here -->
@endIf