Home > OS >  How to Make Laravel Eloquent "AND" in a Query?
How to Make Laravel Eloquent "AND" in a Query?

Time:04-01

I am making a website in my job and I have an issue.

This is what I want to do; in MySQL (PHPMyAdmin) it works with no problems:

SELECT *
FROM tipo_usuarios
INNER JOIN users
    ON tipo_usuarios.id = users.id AND 
       tipo_usuarios.jerarquia = "Administrador";

PHPMyAdmin successful query run

Well this is my Eloquent in Laravel, it works but only with IDs. I don't know how to add AND in Eloquent.

$visitas = User::join("visitas", "visitas.id_usuario", "=", "users.id")
    ->select("*")
    ->get();

code

CodePudding user response:

You can use function for second argument in join

$visitas = User::join("visitas", function ($join) {
    $join->on("visitas.id_usuario", "=", "users.id")
        ->on("visitas.jerarquia","=","Administrador")
})
    ->select("*")
    ->get();

However, you should read the documentation for creating Eloquent relationship. This is a more convenient and understandable functionality than using the query builder

Eloquent relationship

  • Related