2

I need to write a fluent query for full text search in Laravel 3.0. The search query goes like this

SELECT * FROM posts WHERE MATCH (tags) AGAINST ('Lorem Ipsum Dolor Sit Amet');

I had been using Raw query to perform this operation but the results from a raw query could not be paginated. Can anyone tell me the right way to write a fluent query for this query?

Laurence
  • 58,936
  • 21
  • 171
  • 212
aayush shrestha
  • 1,858
  • 2
  • 17
  • 33

2 Answers2

2

Maybe try this....??

public static function find_tags($searchtags, $take = 20)
{
    $results = Posts::raw_where("match (`tags`) against (?)", array($searchtags))
        ->take($take)
        ->get();

    return $results;
}

Or just...

  $searchtags = "Lorem ipsum blah";
  $results = Posts::raw_where("match (`tags`) against (?)", array($searchtags))
        ->paginate(10);

Im just pulling this out of the air though, can't test right now....not at my server...

Kylie
  • 11,421
  • 11
  • 47
  • 78
  • Hi again! Can you tell me why this doesnt work? $results = Posts::raw_where("match (`tags`) against (?)", array($searchtags))->get(); – aayush shrestha Jun 12 '13 at 06:38
  • I thought it did?? doesn't it? or does it only work with take() and paginate()....which I cant see – Kylie Jun 12 '13 at 06:42
  • Yeah. It doesnt seem to be working with ->get() and even with ->all(). Or may be the result of the query is just of different structure which got messed up in the view... I am trying to figure that out first. Isnt there a prope documentation for this? I couldnt find it anywhere?? – aayush shrestha Jun 12 '13 at 07:34
  • no ...the raw_where is about the only undocumented thing in laravel for some reason....maybe you dont have to get()...like when you do a find(), its just available.....somehow i doubt it, but maybe. – Kylie Jun 12 '13 at 07:40
  • `raw_where` not working for me. It throws error `Call to undefined method Illuminate\Database\Query\Builder::raw_where()` :( anyone ? – Jayesh Chandrapal Apr 17 '14 at 20:24
0

Actually you don't need to perform a raw select statement, all that would have to be written in a raw query is the where() statement. So why don't you try something like this, I'm currently having some issues with my local server so I can't test this by myself but this should work.

// If you're using laravel models 
Posts::where_raw(sprintf("MATCH (tags) AGAINST ('%s')", $search))->paginate(10);

where_raw is one of laravel's undocumented functions :D, also keep in mind when using raw queries escaping is not performed so be careful.

Kreshnik Hasanaj
  • 4,815
  • 1
  • 15
  • 18
  • This did not work out. Its actually looking for a column named "raw" and searching for "MATCH (tags) AGAINST (\'thought\')" – aayush shrestha Jun 11 '13 at 20:47
  • This is the error message that I got : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'raw' in 'where clause' SQL: SELECT * FROM `posts` WHERE `raw` = ? Bindings: array ( 0 => 'MATCH (tags) AGAINST (\'thought\')', ) – aayush shrestha Jun 11 '13 at 21:00