dsc80135 2016-06-20 00:31
浏览 15
已采纳

过滤Laravel

i have a project, in there i want to add filter feature. i can make it but i have a bug in my script. when i create new data booking in same title/post the filter list increases and have same title/post. but in there i just want to make a list of them.

Image

wrong

its should be :

  • Kost Alpha Charlie Delta Echo
  • Kost Fanta Bintaro
  • Home Test

here is my controller :

public function getIndex()
    {
        $list = DB::table('data_kos')
        ->orderBy('id','asc')
        ->get();

        $limit = 10;
        $result = DB::table('data_kos')
        ->orderBy('id','asc');
        if(Request::get('q')) {
            $q = Request::input("q");
            $posts->where('title','like','%'.Request::get('q').'%');
            $posts->orwhere("sex","like","%".Request::get("q")."%");
            $posts->orwhere("price","like","%".Request::get("q")."%");
        }
        if(Request::get('title')) {
            $result = $result->where("title",Request::get("title"));
        }
        $data['list'] = $list;
        $data['posts'] = $result->paginate($limit);
        return view('kost', $data);
    }

and this is my view :

@foreach($list as $row)
        <li><a href='{{ url("booking?title=$row->title") }}'>{{ $row->title }}</a></li>
        @endforeach

what improvements do i have to make to the code to achieve my goal?

展开全部

  • 写回答

1条回答 默认 最新

  • douganggu4392 2016-06-20 01:29
    关注

    You should use distinct:

    $list = DB::table('data_kos')
        ->distinct()
        ->select( 'title' )
        ->orderBy('id','asc')
        ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部