Redirect to page when value is null in another table laravel

前端 未结 2 826
孤城傲影
孤城傲影 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: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');
    }
    

提交回复
热议问题