skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. No this is not possible Now,
    Because DB row not supported elequent relation
    you should use by ORM formate.
    Here is some example :-
    $user = user::with(‘school’)->get();

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search