简体   繁体   English

如何在laravel where子句中使用“ CONCAT”而不使用模型

[英]How to Use “CONCAT” in laravel where clause without using model

I have a table ('mbo_party') where has two column first and last (I have used first to store firstname, last to store lastname). 我有一个表('mbo_party'),其中有第一列和最后两列(我曾用过第一个存储名字,最后一个存储姓氏)。 Suppose I store Debanjan in column 'first' and Ganguly in 'last'. 假设我将Debanjan存储在“ first”列中,将Ganguly存储在“ last”中。 So in search field if one search by 'Debanjan Ganguly', how do I get all the data from that column. 因此,在搜索字段中,如果通过“ Debanjan Ganguly”进行一次搜索,我如何从该列中获取所有数据。 I am using laravel 5.4, I am not using any model. 我正在使用laravel 5.4,未使用任何模型。 I write query in my controller. 我在控制器中编写查询。 Here's my code so far... 到目前为止,这是我的代码...

Controller 调节器

    $party_name = $request->get('party_name');
        $query = DB::table('mbo_party')->where('concat(first," ",last)' , '=' , $party_name)->first();

View 视图

    <form id="party_search_it" method="GET" action="{{route('mbo-index')}}"> 
        {{ csrf_field() }}
        <input type="text" class="form-control" name="party_name" placeholder="Search for Party Name..." autocomplete="off">
    </form>

My Table View Here's my Table View 我的表格视图 这是我的表格视图

In this method when I am using echo in controller , it is showing blank. 在这种方法中,当我在控制器中使用echo时,它显示为空白。

    echo"<pre>";print_r($query);die;

I want to know what is correct syntax for performing this kind of operation 我想知道执行这种操作的正确语法是什么

尝试这个

$query = DB::table('mbo_party')->where(DB::raw("CONCAT(first,' ',last)"), 'LIKE', '%' . $party_name . '%')->first();

这应该工作:

$query = DB::table('mbo_party')->whereRaw('CONCAT(`first`," ",`last`) = "' . $party_name . '"'))->first();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM