I am using Laravel 4 and the Eloquent model for a project we are working on. The database conforms to 3NF and everything works great. Also all MySQL tables were switched back from InnoDB to MyISAM since the MySQL version < 5.6 (full text search in InnoDB is only supported from 5.6 and up).
While creating some database search filters I am finding some shortage with using the Eloquent model vs the Query Builder. In specifics, especially when trying to do a full text search on columns from multiple tables (and staying within the Eloquent's object context).
For simplicity, we have the following database structure:
--projects
--id
--name
--status
--...
--users
--id
--...
--roles
--id
--project_id
--user_id
--...
--notes
--id
--project_id
--user_id
--note
--....
The following code (simplified and minimized for the question) currently works fine, but the full text search only works for one table (projects
table).
if (Request::isMethod('post'))
{
$filters = array('type_id','status','division','date_of_activation','date_of_closure');
foreach ($filters as $filter) {
$value = Input::get($filter);
if (!empty($value) && $value != -1) {//-1 is the value of 'ALL' option
$projects->where($filter,'=',$value);
}
}
$search = Input::get('search');
if (!empty($search)) {
$projects->whereRAW("MATCH(name,description) AGAINST(? IN BOOLEAN MODE)",array($search));
}
}
// more code here...
// some more filters...
// and at the end I am committing the search by using paginate(10)
return View::make('pages/projects/listView',
array(
"projects" => $projects->paginate(10)
)
);
I need to extend the full text search to include the following columns - projects.name
,projects.description
and notes.note
.
When trying to find how to make it with Eloquent we keep on coming back to Query Builder and running a custom query, which will work fine but then we will face these problems/cons:
Project::find($id)
on the return results just to get the object again.Reading Laravel's documentation and API, I could not find a method to run raw SQL queries using Eloquent. There is whereRAW()
but it is not broad enough. I assume that this is a restriction made by design, but it is still a restriction.
So my questions are:
Project::find($id)
on the array results).where
methods together, while only committing using get()
or paginate(10)
at a later point.I understand that Eloquent and Query Builder are different creatures. But if mixing both was possible, or using Eloquent to run raw SQL queries, I believe that the Eloquent model will become much more robust. The other option (using only Query Builder) seems to me a bit like under-using the Laravel framework.
Hope to get some insights about this since it seems as community of Laravel is still evolving, even though I find it to be an amazing framework!
Thanks and I appreciate any input you may have :)
I think "orWhereRaw" is what you are looking for:
$projects
->whereRAW("MATCH(name,description) AGAINST(? IN BOOLEAN MODE)",array($search))
->orWhereRAW('id IN (SELECT project_id FROM roles JOIN users ON user.id = roles.user_id WHERE MATCH(username, first_name, last_name) AGAINST(? IN BOOLEAN MODE))', array($search));
->orWhereRaw('...')
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community