【问题标题】:Laravel: Show data WHEREIN 'Imploded Id' With ArrayLaravel:使用数组显示数据 WHEREIN 'Imploded Id'
【发布时间】:2020-12-09 06:11:05
【问题描述】:

我有同学

+====+=========+============+ |编号 |程序 |全名 | +====+=========+============+ | 1 | 2|4|7 |卢卡斯 | +----+---------+------------+ | 2 | 1 |亚当 | +----+---------+------------+ | 3 | 4|5 |夏娃 | +----+---------+------------+

$val = 数组(4,7,10); 我想在 $val 中的 'program' 中显示 'id' 和 'fullname'。

我在 Controller 中试过这个: $val = 数组(4,7,10); return DB::table('students')->whereIn(explode('|','program'), $val)->get();

返回的是 (id:1, full_name:Lucas) 和 (id:3, full_name:Eve)。 当然,它不起作用。感谢您的帮助。

【问题讨论】:

    标签: arrays laravel eloquent


    【解决方案1】:

    您可以在查询中使用like

    $val = array(4,7,10);
    
    return DB::table('students')
        ->where('program','like','%'.implode('|',$val).'%')
        ->first(); 
    

    【讨论】:

      【解决方案2】:

      你可以使用find_in_set的sql

      $val = array(4,7,10);
      
      return DB::table('students')
           ->whereRaw("find_in_set(implode('|',$val),replace('|',',',program)")
          ->first();
      

      find_in_set 默认通过, 搜索,但借助replace() 函数我们可以搜索

      参考链接https://www.mysqltutorial.org/mysql-find_in_set/

      【讨论】:

        猜你喜欢
        • 2017-05-30
        • 2015-08-22
        • 1970-01-01
        • 2020-04-24
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        • 2019-07-11
        • 1970-01-01
        相关资源
        最近更新 更多