【问题标题】:Get ID from url to insert to WhereIn (Laravel)从 url 获取 ID 以插入 WhereIn (Laravel)
【发布时间】:2019-02-03 00:22:23
【问题描述】:

我像这样插入网址:“export/1-2-18”。 1、2、18是表的id。 ID 由复选框选中,所以它可以是桌子上的任何数字

这样的href

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

$a = 1-2-18;

这是控制器

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');
        }  

我已将网址从“1-2-18”替换为“1,2,18”并插入到whereIn,但我只收到了其中一个结果。

如何获得所有结果?提前致谢。

【问题讨论】:

    标签: laravel where-in


    【解决方案1】:

    whereIn 的第二个参数错误。您必须输入值数组而不是带有字符串的数组(['1,2,18'] 应该是 [1,2,18])。

    你可以使用explode()方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-20
      • 2021-05-16
      • 1970-01-01
      • 2022-11-16
      • 2018-05-26
      • 2014-05-10
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多