phplaravelwhere-in

Get ID from url to insert to WhereIn (Laravel)


I insert the url like this: "export/1-2-18". 1, 2, 18 are ids of table. The ID is checked by checkbox so, it can be any numbers on the table

The href like this

<a href ="{{ url('export',['id'=> $a ]) }}" class="btn btn-info export" id="export-button"> Export file </a> 

with $a = 1-2-18;

And this is the controller

public function exportFile($id){
            $rp = str_replace('-',',',$id);
            echo ($rp);
            $products = DB::table('duan')
                ->whereIn('MaDA', [$rp])
                ->get(); 
            dd($products);
            $products= json_decode( json_encode($products), true);
            return Excel::create('ThongTinDuAn', function($excel) use ($products) {
                $excel->sheet('sheet name', function($sheet) use ($products)
                {
                    $sheet->fromArray($products);
                     $sheet->getStyle('A2:E2')->getAlignment()->setWrapText(true);
                });

            })->export('xlsx');
        }  

I have replaced the url from "1-2-18" to "1,2,18" and insert to whereIn but I just received one of the results.

enter image description here

How to get all of the results? Thanks in advance.


Solution

  • 2nd parameter for whereIn is wrong. You have to input array of values not an array with a string (['1,2,18'] should be [1,2,18]).

    You can use explode() method.

    $rp = explode('-',$id);
    $products = DB::table('duan')
                ->whereIn('MaDA', $rp)
                ->get();