Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I query raw via Eloquent?

I am trying to do a query in my Laravel app and I want to use a normal structure for my query. This class either does use Eloquent so I need to find something to do a query totally raw.

Might be something like Model::query($query);. Only that doesn't work.

like image 856
Chilion Avatar asked Apr 08 '14 00:04

Chilion


People also ask

How do I select RAW?

How to Get into RAW. It is tough to get recruited in the organization as the process includes a written examination followed by an interview. The Intelligence Bureau recruitment process needs the candidates to appear for CGPE ( Combined Graduate Preliminary Exam) conducted by SSC (Staff Selection Commission).

How do you do an eloquent query?

The first method to get the query of an Eloquent call is by using the toSql() method. This method returns the query without running it – good if you don't want to alter data and only get the query – but this method doesn't show the whole query if your query is more complex or if there are sub-queries.

What is DB :: Raw in Laravel?

DB::raw() is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection.


3 Answers

You may try this:

// query can't be select * from table where
Model::select(DB::raw('query'))->get();

An Example:

Model::select(DB::raw('query'))
     ->whereNull('deleted_at')
     ->orderBy('id')
     ->get();

Also, you may use something like this (Using Query Builder):

$users = DB::table('users')
                 ->select(DB::raw('count(*) as user_count, status'))
                 ->where('status', '<>', 1)
                 ->groupBy('status')
                 ->get();

Also, you may try something like this (Using Query Builder):

$users = DB::select('select * from users where id = ?', array(1));
$users = DB::select( DB::raw("select * from users where username = :username"), array('username' => Input::get("username")));

Check more about Raw-Expressions on Laravel website.

like image 162
The Alpha Avatar answered Oct 18 '22 05:10

The Alpha


You can use hydrate() function to convert your array to the Eloquent models, which Laravel itself internally uses to convert the query results to the models. It's not mentioned in the docs as far as I know.

Below code is equviolent to $userModels = User::where('id', '>', $userId)->get();:

$userData = DB::select('SELECT * FROM users WHERE id > ?', [$userId]);
$userModels = User::hydrate($userData);

hydrate() function is defined in \Illuminate\Database\Eloquent\Builder as:

/**
 * Create a collection of models from plain arrays.
 *
 * @param  array  $items
 * @return \Illuminate\Database\Eloquent\Collection
 */
public function hydrate(array $items) {}
like image 22
Orkhan Alikhanov Avatar answered Oct 18 '22 07:10

Orkhan Alikhanov


use DB::statement('your raw query here'). Hope this helps.

like image 21
Ray Avatar answered Oct 18 '22 06:10

Ray