duanbu1998 2015-05-22 02:36
浏览 183
已采纳

在Laravel中使用模型的正确方法是什么?

Can you help me with this? I am currently studying Laravel on my own and I followed the tutorials in the Laracasts and it is awesome. Before Laravel I am using CodeIgniter and Opencart in my projects and I started to study Laravel because I want to learn a new framework.

In CI and Opencart all your database queries are in the model. But in Laravel you can perform and queries in Controller?. Is it a proper way to the queries in Laravel?

I have this kind of code in the Controller:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Article;
use Illuminate\Http\Request;

class ArticlesController extends Controller {

    public function index() {

        $articles = Article::all();

        return view('articles.index')->with('articles', $articles);

    }

}
  • 写回答

2条回答 默认 最新

  • dqwh0108 2015-05-22 03:40
    关注

    Repositories is a smart decision to you. But why?
    Basically, repositories is a 'gateway' between your application and your storage.
    With repositories, you'll find your 'database queries' in a single place.

    Let's think about the model Articles.
    Instead of use a static instance of Articles all the times that you need to use it (Articles::find(), Articles::all(), etc), just create a repository of Articles.
    Inject this repo in your controller (e.g.), and use 'features' storaged in your ArticleRepository.

    What do you mean?
    Let's consider a repository of Articles. What I'll use many times in my app of Articles model? I need select all, select by id, insert, update, delete. Basically these 'stuffs'. So, if I have all this stuffs in a place?

    class ArticleRepository {
    
        public function all(){}
        public function getById($id){}
        public function insert($data){}
        public function update($data){}
        public function delete($id){}
    
    }
    

    Inject this ArticleRepository in your controller. To do this, read a about IoC Container here: http://laravel.com/docs/5.0/container

    The construct in your controller will be like this:

    public function __construct(ArticleRepository $articles)
    {
        $this->articles = $articles;
    }
    

    Once all, when you need get all Articles in your controller, just do:

    public function index()
    {
        $articles = $this->articles->all();
        return View::make('articles.index')->with(['articles' => $articles]);
    }
    

    With this practice, you have a clean application with testables controllers and a beautiful organization and design. ;)

    Look, I tried to be as didactic as possible to you understand the concept. The use of repositories is not only a way to do. So I let the links in the comments. And let other references here as well.
    I'm sure you will understand quickly.
    Success in learning! :)

    https://laracasts.com/search?q=repositories&q-where=lessons
    http://ryantablada.com/post/the-repository-pattern-in-action
    http://culttt.com/2014/03/17/eloquent-tricks-better-repositories/
    http://culttt.com/2013/07/15/how-to-structure-testable-controllers-in-laravel-4/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题