0

I have a table ('mbo_party') where has two column first and last (I have used first to store firstname, last to store lastname). Suppose I store Debanjan in column 'first' and Ganguly in 'last'. So in search field if one search by 'Debanjan Ganguly', how do I get all the data from that column. I am using laravel 5.4, I am not using any model. 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"<pre>";print_r($query);die;

I want to know what is correct syntax for performing this kind of operation

6
  • Possible duplicate of Laravel concat in query (where condition) Commented Sep 29, 2018 at 13:10
  • no, I want to use it via DB::table('mbo_party')->where() method, without using model, want to know correct syntax for this. Commented Sep 29, 2018 at 13:29
  • DB::raw in where() is the key here man. Same question, different methods, same solution. Commented Sep 29, 2018 at 13:36
  • how to know which table I am using then ?? Commented Sep 29, 2018 at 13:44
  • 1
    $query = DB::table('mbo_party')->where(DB::raw('concat(first," ",last)') , '=' , $party_name)->first(); Just like Sunil kumawat anwser and the same solution as the duplicate page... He uses LIKE instead of =, that's the only diff. Tested on my laravel install (changed table name and columns to match mine though) but works. Why don't you try dd($query) on the line after to see exactly what is returned? Commented Sep 29, 2018 at 13:58

2 Answers 2

1

try this

$query = DB::table('mbo_party')->where(DB::raw("CONCAT(first,' ',last)"), 'LIKE', '%' . $party_name . '%')->first();
Sign up to request clarification or add additional context in comments.

4 Comments

What did you change? ("CONCAT(first," looks like invalid PHP.
It is Showing Parse error: syntax error, unexpected '",last)"' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')'
It doesn't showing any error, but on doing echo, $query isn't returning any value, I mean to say echo"<pre>";print_r($query);die; showing blank page
Welcome @iamawesome
0

This should work:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.