Redirect to page when value is null in another table laravel

前端 未结 2 825
孤城傲影
孤城傲影 2021-01-21 17:55

I\'m trying to redirect the user to another page if my additional_infos table contain some stuff that are empty in this case, contact, name and address.

I\'ve done somet

相关标签:
2条回答
  • 2021-01-21 18:19

    Try transform your query to SQL to check what are you doing with database first

    $additional_info = DB::table('additional_infos')->where('address',NULL)->orWhere('name', NULL)->orWhere('number')->toSql();
    dd($additional_info);
    
    0 讨论(0)
  • 2021-01-21 18:34

    You will never get null so that you can test it in if like this if( $additional_info) it will alawys evaluate to true, because it will always return a collection even if there is no elements that fulfil the conditions it will return a empty collection not null.

    You have three choices :

    • Use ->get() and add ->count() in the if statment if($additional_info->count())

    • Replace ->get() by ->first() and leave if($additional_info)

    • Replace ->get() by ->count() and if($additional_info > 0)

    Try it using whereNull and orWhereNull like this :

    public function test(Request $request){
        $additional_info = DB::table('additional_infos') 
                                ->whereNull('address')
                                ->orWhereNull('name')
                                ->orWhereNull('number')
                                ->get();
        //request input //ignore this part
        if( $additional_info->count())
            return redirect(url('/user/showupdate6/'.$id->id.'/edit6') );
        else{
        return redirect('/home');
    }
    
    0 讨论(0)
提交回复
热议问题