4

I am new to Laravel 5.4 and working on some query manipulation. Now I have created an query using query builder which is shown below:

$view = DB::table('blocks')
    ->leftjoin('programmes', 'blocks.programme_id', '=', 'programmes.id')
    ->select('blocks.id', 'blocks.programme_id', 'blocks.name', 'blocks.colour', 'blocks.year', 'programmes.title AS programme');  

I have two more table "dates" and "modules". Each dates as well as module belongs to blocks.

Now I want to fetch all blocks with programmes, dates and modules. I know i can use with() method to get all of these. But as per my knowledge on Laravel, I can use with() method only if I have model file of each table and have relationship between them.

But do not want to use model and define relationship between them. I just want to know How can I get block data with programmes, dates and modules without creating model and defining relationship betwen them in model? Is there any other way to use with() method without model?

Block dates and modules are conditional, Sometime I dont want get data of this table with block.

Please help me on this.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Dhirender
  • 604
  • 8
  • 23

1 Answers1

4

You can't do it automatically. Eager loading is only for Eloquent model so you cannot use it with query builder. However in most cases you can use Eloquent also for getting more complicated queries (you can also use joins when using Eloquent) so you will be able to use eager loading.

But if you don't want to use Eloquent at all, obviously you will need to create some custom mechanism for eager loading.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291