Home > database >  Data as JSON Laravel
Data as JSON Laravel

Time:04-29

Hi i have a search in which it gets data from table candidates

My Search Code

public function advance(Request $request)
    {
    $data1 = ['LoggedUserInfo'=>Admin::where('id','=', session('LoggedUser'))->first()];
    $data = \DB::table('candidates');
    if( $request->name){
        $data = $data->where('name', 'LIKE', "%" . $request->name . "%");
    }
     if( $request->location){
        $data = $data->where('location', 'LIKE', "%" . $request->location . "%");
    }
     if( $request->key_skills){
        $data = $data->where('key_skills', 'LIKE', "%" . $request->key_skills . "%");
    }
    if( $request->gender){
        $data = $data->where('gender', 'LIKE', "%" . $request->gender . "%");
    }
     if( $request->pref_loc){
        $data = $data->where('pref_loc', 'LIKE', "%" . $request->pref_loc . "%");
    }
    if( $request->phoneno){
        $data = $data->where('phoneno', 'LIKE', "%" . $request->phoneno . "%");
    }
     if( $request->email){
        $data = $data->where('email', 'LIKE', "%" . $request->email . "%");
    }
    $min_np = $request->min_np;
    $max_np = $request->max_np;
     if ($min_np || $max_np) {
     $data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(notice_period,' ', 1),UNSIGNED INTEGER) >= {$min_np}");
     $data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(notice_period,' ', 1),UNSIGNED INTEGER) <= {$max_np}");
 }
    $min_ctc = $request->min_ctc;
    $max_ctc = $request->max_ctc;
    
 
    if ($min_ctc || $max_ctc) {
     $data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(salary,' ', 1),UNSIGNED INTEGER) >= {$min_ctc}");
     $data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(salary,' ', 1),UNSIGNED INTEGER) <= {$max_ctc}");
 }

     if ($request->min_exp || $request->max_exp) {
     $data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(experrience,' ', 1),UNSIGNED INTEGER) >= {$request->min_exp}");
     $data = $data->whereRaw("CONVERT(SUBSTRING_INDEX(experrience,' ', 1),UNSIGNED INTEGER) <= {$request->max_exp}");
 }
    // $dataa = $data ;
    $data = $data->paginate(10);
    $data2 = $data->total();
    $dataa = $data->toJson();

    $data4 = \DB::table('admins')->get();
    $setting = DB::select('select * from logo where id=?',[1]);

    return view('search', compact('data2','data4','setting','dataa'))->with('data',$data);
}

Now i want this $data to be converted into an 'Array or JSON'

but when i try this $data->toJson(); it give me this data in pagination with all pages link & paginated data

but i want all data in a Array or JSON

Note - I want all data at once bcz to export the data into csv file

enter image description here

CodePudding user response:

If you don't want the pagination don't use $data = $data->paginate(10); instead use $data = $data->get();

CodePudding user response:

According to this link https://scoutapm.com/blog/php-json_encode-serialize-php-objects-to-json, you can try this:

$dataJson = json_encode($data);
  • Related