Skip to content

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?



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:

Ref Thread

User contributions licensed under: CC BY-SA
5 People found this is helpful