Support the ongoing development of Laravel.io →
Database Eloquent

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:

  1. Query Builder returns an array while Eloquent returns model objects. Since we are extending each model to include methods, we really don't want to give up the awesomeness of the Eloquent model. And we really don't want to use the Eloquent Project::find($id) on the return results just to get the object again.
  2. We are chaining the 'where' methods to have any number of filters assigned to it as well as for code re-usability. Seems like mixing Eloquent and Query Builder statement together will break our chaining.
  3. For the consistency of this project, we want all database queries to stay in Eloquent connotation.

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:

  1. Is it possible to run a full text search on columns from multiple tables, only in Eloquent. Every piece of information I came across online, mentions using Query Builder.
  2. If not, is it possible to use Query Builder searches and returning Eloquent objects? (without the need to run Project::find($id) on the array results).
  3. And lastly, is it possible to chain Eloquent and Query Builder 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 :)

Last updated 3 years ago.
0

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('...')
Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.