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

DB::table($table)->with($relation); it's possible?

If I use models this is easily resolved, but if I don’t have the availability of using models, would it be possible to get the result equivalent to ->with() with DB::table?

>Solution :

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

No, Here is why

Laravel models are using Eloquent, which is an ORM library.

For example, a class model could have many teachers relations, and if you want to fetch a class with all the teachers, you could do something like:

$class = Classes::with('teachers')->find($id);

// $class->teachers contain all the teachers in the given class

With the query builder, you would need to do something like

$class = DB::table('classes')->find($id);

$teachers = DB::table('teachers')->where('class_id', $class->id)->get();

You can look into the documentation: https://laravel.com/docs/5.6/eloquent

Ref Thread

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