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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等