Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 1 year ago.
0

I don't think any dry concepts are necessarily violated here. If this is the only place where you access this data and in each case you are always just getting all of the rows in the table, you probably don't need to be bringing eloquent into the mix, and could be doing this with the query builder:

DB::table('index_slider')->get();
Last updated 1 year ago.
0

Thanks for getting back to me! Yes, this seems like a much better solution for me. Where would I utilise query builder, in the view or controller? I know the above example works, but would I do this before my loop?

Last updated 1 year ago.
0

I'd put it in the controller, so the result of the query is sent to the view.

Last updated 1 year ago.
0

Panoplr said:

Thanks for getting back to me! Yes, this seems like a much better solution for me. Where would I utilise query builder, in the view or controller? I know the above example works, but would I do this before my loop?

Panoplr - Depending on the size of your project, you may want to consider creating a class (not one that inherits from Eloquent/Model) that runs the above functions and returns the correct object and passing that into the controller. This would prevent the controller from knowing too much about the database functions.

Here is a quick example:

class PageElements
{
  public function indexSlider()
  {
    return DB::table('index_slider')->get();
  }
  public function indexFeature()
  {
    return DB::table('index_feature')->get();
  }
  public function footerBoxes()
  {
    return DB::table('footer_boxes')->get();
  }
}

You could implement it in your controller like this:

class YourController extends BaseController
{
  public function __construct(PageElements $pageElements)
  {
    $this->pageElements = $pageElements;
  }
  public function index()
  {
    return View::make('index')
             ->with('index_slider', $this->pageElements->indexSlider())
             ->with('index_feature', $this->pageElements->indexSlider())
             ->with('footer_boxes', $this->pageElements->indexSlider());
  }
}

The nice thing about this approach is that if you later decide to change the way you access the data (such as going back to Eloquent, then you can just update your class file and not have to touch your controller. This may not be the most elegant example, but I think the basic concept is sound.

Last updated 1 year ago.
0

Thank you for adding this example. I am getting the grasp of laravel more then ever now. Starting to experience the power, this forum and community really just adds to it all!

Last updated 1 year ago.
0

pyrello said:

Panoplr said:

Thanks for getting back to me! Yes, this seems like a much better solution for me. Where would I utilise query builder, in the view or controller? I know the above example works, but would I do this before my loop?

Panoplr - Depending on the size of your project, you may want to consider creating a class (not one that inherits from Eloquent/Model) that runs the above functions and returns the correct object and passing that into the controller. This would prevent the controller from knowing too much about the database functions.

Here is a quick example:

class PageElements
{
 public function indexSlider()
 {
   return DB::table('index_slider')->get();
 }
 public function indexFeature()
 {
   return DB::table('index_feature')->get();
 }
 public function footerBoxes()
 {
   return DB::table('footer_boxes')->get();
 }
}

You could implement it in your controller like this:

class YourController extends BaseController
{
 public function __construct(PageElements $pageElements)
 {
   $this->pageElements = $pageElements;
 }
 public function index()
 {
   return View::make('index')
            ->with('index_slider', $this->pageElements->indexSlider())
            ->with('index_feature', $this->pageElements->indexSlider())
            ->with('footer_boxes', $this->pageElements->indexSlider());
 }
}

The nice thing about this approach is that if you later decide to change the way you access the data (such as going back to Eloquent, then you can just update your class file and not have to touch your controller. This may not be the most elegant example, but I think the basic concept is sound.

Last updated 1 year ago.
0

pyrello said:

Panoplr said:

Thanks for getting back to me! Yes, this seems like a much better solution for me. Where would I utilise query builder, in the view or controller? I know the above example works, but would I do this before my loop?

Panoplr - Depending on the size of your project, you may want to consider creating a class (not one that inherits from Eloquent/Model) that runs the above functions and returns the correct object and passing that into the controller. This would prevent the controller from knowing too much about the database functions.

Here is a quick example:

class PageElements
{
 public function indexSlider()
 {
   return DB::table('index_slider')->get();
 }
 public function indexFeature()
 {
   return DB::table('index_feature')->get();
 }
 public function footerBoxes()
 {
   return DB::table('footer_boxes')->get();
 }
}

You could implement it in your controller like this:

class YourController extends BaseController
{
 public function __construct(PageElements $pageElements)
 {
   $this->pageElements = $pageElements;
 }
 public function index()
 {
   return View::make('index')
            ->with('index_slider', $this->pageElements->indexSlider())
            ->with('index_feature', $this->pageElements->indexSlider())
            ->with('footer_boxes', $this->pageElements->indexSlider());
 }
}

The nice thing about this approach is that if you later decide to change the way you access the data (such as going back to Eloquent, then you can just update your class file and not have to touch your controller. This may not be the most elegant example, but I think the basic concept is sound.

Last updated 1 year ago.
0

To prello : Could you give me example how to call that Controller from views?

Last updated 1 year ago.
0

muhqori said:

To prello : Could you give me example how to call that Controller from views?

muhqori, I am not sure I entirely understand your question.

If asking how to access the index_slide, index_feature, and index_boxes variables from a view, then you would just do it using:

// app/views/index.blade.php

<?php

<div class="slider">{{ $index_slider }}</div>
<div class="feature">{{ $index_feature }}</div>
<div class="footer">{{ $footer_boxes }}</div>
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Panoplr panoplr Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.