Home > Software design >  How do I use 2 variable in foreach in laravel blade
How do I use 2 variable in foreach in laravel blade

Time:07-19

One variable is used for searching and Another variable is used to display name based on id from DB

public function recorddisplay(Request $request)
 {

    $search =$request['search'] ??"";
    if($search != "")
    {
        $covidrecord=Covidrecord::where('fullname','LIKE',"%$search%")
        ->orWhere('province','LIKE',"%$search%")
        ->orWhere('district','LIKE',"%$search%")
        ->orWhere('localgovernment','LIKE',"%$search%")->paginate(15);
    }
    else{
        $covidrecord= Covidrecord::paginate(15);
    }

    $data = compact('covidrecord','search');

     $dataCovid=DB::table('coviddeathrecord')
     ->join('district','coviddeathrecord.district','=','district.id')
     ->join('province','coviddeathrecord.province','=','province.id')
     ->join('localgovernment','coviddeathrecord.localgovernment','=','localgovernment.id')
     ->get(['coviddeathrecord.*','district.district','province.province','localgovernment.localgovernment']);

    return view('admin.dashboard.display', compact('dataCovid'))->with($data);

}

This is the blade

 @foreach(array_merge($covidrecord,$dataCovid) as $data)

            <tr>
              <td>
                <div >
                  
                  <div >
                    {{--  <label>{{ $data->fullname }}</label>  --}}

                    <h6 >{{$data->fullname}}</h6>
                    <p >{{$data->gender }} ,{{$data->age }} वर्ष</p>
                  </div>
                </div>
              </td>
              <td>

                <p >{{ $address->province }}</p>
                <p >{{ $address->district }}, {{$address->localgovernment }}<br/> {{ $data->tole }}</p>


            </td>    
            @endforeach

As I am using array_merge, it is throwing the error //array_merge(): Expected parameter 1 to be an array, object given

CodePudding user response:

You can pass variables in a compact function:

return view('admin.dashboard.display', compact('data', 'dataCovid'));

Also please see compact function

CodePudding user response:

return like this

    return view('website.condo_living.index')->with([
        'var1'=>$var1,
        'var2'=>$var2,
    ]);

CodePudding user response:

to use 2 variables in foreach in your blade. It´s better that you pass you data from controller

return view('yourSubFolderView.View')->with('yourVariable')->with('yourVariable')

Also you can use

return view('yourSubFolderView.View')->compact('yourVariable', 'yourVariable')

UPDATE

in this line:

return view('admin.dashboard.display', compact('dataCovid'))->with($data);

change to:

return view('admin.dashboard.display', compact('dataCovid'))->with('data',$data);

and in your blade, you can access to $data or $dataCovid with for-each

in your code was missing your variable name 'data', $data

CodePudding user response:

FYI, the a covidrecord variable returns collections of data and a dataCovid variable that returns an array of data that you have to standardize the types of variables to deal with in the view array only or collection only

I convert the data type to collection of array

and passing collection correctly from your method:


public function recorddisplay(Request $request)
 {

    $search = $request->get('search') ?? "";
    if($search != "") {
        $covidrecord = DB::table('covidrecord')->where('fullname','LIKE',"%$search%")
               ->orWhere('province','LIKE',"%$search%")
               ->orWhere('district','LIKE',"%$search%")
               ->orWhere('localgovernment','LIKE',"%$search%")
               ->paginate(15)
               ->items();
    } else {
        $covidrecord= DB::table('covidrecord')->paginate(15)->items();
    }

     $dataCovid = DB::table('coviddeathrecord')
     ->join('district','coviddeathrecord.district','=','district.id')
     ->join('province','coviddeathrecord.province','=','province.id')
     ->join('localgovernment','coviddeathrecord.localgovernment','=','localgovernment.id')
     ->get(['coviddeathrecord.*','district.district','province.province','localgovernment.localgovernment']);

    $items = collect($users)->concat($data);

    return view('admin.dashboard.display', compact('items'));

}

and within the blade you can looping normal with collection of array like:


     @foreach($items as $data)
    
                <tr>
                  <td>
                    <div >
                      
                      <div >
                        {{--  <label>{{ $data['fullname'] }}</label>  --}}
    
                        <h6 >{{$data['fullname']}}</h6>
                        <p >{{$data['gender'] }} ,{{$data['age'] }} वर्ष</p>
                      </div>
                    </div>
                  </td>
                  <td>
    
                    <p >{{ $address->province }}</p>
                    <p >{{ $address->district }}, {{$address->localgovernment }}<br/> {{ $data['tole'] }}</p>
    
    
                </td>    
                @endforeach

  • Related