Home > other >  return back not work in laravel plz solve
return back not work in laravel plz solve

Time:02-14

return back()-> not work in laravel

When I send data, the data is stored in the database but the return back() function does not work.

screen shot: enter image description here

enter image description here conroller:

function commundityvendordata(Request $request){
            
            $collection = count(collect($request));

            $cvendordata = new commundityvendordata;
            for ($i=0; $i < $collection; $i  ) {
            $new_date = Carbon::parse($request->new_date)->format('Y-m-d');
            $CCode=communitydata::where('c_code','=',$request->ccode[$i])->first();
            $cd_id = $CCode['cd_id'];
            $CName=communitydata::where('c_name','=',$request->cname[$i])->first();
            $cn_id = $CName['cd_id'];
            $CUnit=units::where('unit','=',$request->cunit[$i])->first();
            $unit_id = $CUnit['unit_id'];
            $vender1 = $request->vendor1[$i];
            $vender2 = $request->vendor2[$i];
            $vender3 = $request->vendor3[$i];
            $vender4 = $request->vendor4[$i];


               $commdata = [
                'new_date'  => $new_date,
                'cd_id'     => $cd_id,
                'cn_id'     => $cn_id,
                'unit_id'   => $unit_id,
                'vender1'   => $vender1,
                'vender2'   => $vender2,
                'vender3'   => $vender3,
                'vender4'   => $vender4
            ];
            if($cd_id == ''){
                     return !is_null($cd_id) && $cd_id !== ''; 
                } 
                DB::table('commundityvendordata')->insert($commdata);
            

        }
        
        return back()->with('success','Data Saved...');
        
}

CodePudding user response:

Try this

return redirect()->back()->with('success','Data Saved...');

--- EDIT ----

You are returning if $cd_id is null or empty.

One of your data has $cd_id is null or empty

Insert data if $cd_id is not empty.

Try this...

for ($i=0; $i < $collection; $i  ) {
    $new_date = Carbon::parse($request->new_date)->format('Y-m-d');
    $CCode=communitydata::where('c_code','=',$request->ccode[$i])->first();
    
    ....

    
    // if($cd_id == ''){
    //    /// you are returning here
    //    return !is_null($cd_id) && $cd_id !== ''; 
    // }

    if($cd_id != ''){
        DB::table('commundityvendordata')->insert($commdata);
    }
}

return back()->with('success','Data Saved...');
  • Related