2

I'm trying to do a query with 2 where clauses like:

select * from table1 where `name` = 'paul' AND `id` = 1

in Laravel with Eloquent, but I don't know the correct syntax.

0

2 Answers 2

11

Simple, use another where

Model::where('name', '=', 'paul')->where('id', '=', 1);

Then you may use get() or first() to fetch the row(s).

If you want to use just Query Builder(Fluent) then replace Model:: with DB::table('table1')->.

Note

  • = is optional here. Here you can use other operators.

Update

From Laravel 4.2 you can also use array:

Model::where([
               'name' => 'paul', 
               'id' => 1
             ]);
Sign up to request clarification or add additional context in comments.

2 Comments

another question about queries, I want to execute in Eloquent a query something like "SELECT r.id, avg( p.puntuacio ) FROM receptes AS r, puntuacio_receptes_usuaris AS p WHERE r.id = p.recepta_id GROUP BY r.id" but I don't know how I could do
Can you please post this as a new question and I will answer it on my free time?
2

You have to have an object that corresponds to table1.

Eloquent object:

class User extends Eloquent {

    protected $table = 'table1';

    ...
}

ORM query:

$user = User::where('name', 'paul')
              ->where('id', 1)
              ->first();

2 Comments

Not strictly true, you can use DB::table('table1') instead of an Eloquent model. Furthermore, you need only where or orWhere for chaining (not andWhere).
That's true, you can do that with fluent, but he was asking for Eloquent. Fixed where, thanks for pointing.

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.