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();
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?
I'd put it in the controller, so the result of the query is sent to the view.
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.
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!
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.
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.
To prello : Could you give me example how to call that Controller from views?
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>
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community