Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Hi everyone how are you, i hope you’ll well.
Well i am making a web site in my job and i have a trouble.

This is my code :
this is what i want to do, in Mysql ( PHPMyADmin) it works with no problems, look:

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Well this is my eloquent Code in laravel, it works too BUT, only with ID’s i dont how to add the " AND " in eloquent.

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

enter image description here

I hope you can help me with that little trouble 🙂

enter image description here

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading