简体   繁体   中英

How to Make Laravel Eloquent "AND" in a Query?

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 成功查询运行

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();

代码

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM