dougou8573 2016-10-19 14:09
浏览 39
已采纳

Laravel最佳实践 - 查询

I need to pass some data down to my view. I have two models a user and a tips model.

The User model has a method that returns the user hasMany(Tip::Class) and the Tip model has a method that returns that the tip belongsTo(User::class).

I'm doing a profile page for a user and using route model binding to return a user model when accessing the profile.

public function tipsterProfileShow(User $tipster)
{
  if (!$tipster->isTipster())
  {
    return redirect()->route('home');
  }

  return view('profile.index')->with([
    'tipster' => $tipster,
    'tips' => $tipster->tips(),
  ]);
}

I want to display some data such as the amount of tips that are correct which are indicated by the status column in the tips table.

At the moment in the blade view I'm using

{{$tips->where('status','Won')->count()}}

I feel this isn't best practice but I may be wrong.

Would it be better doing something like the below?

public function tipsterProfileShow(User $tipster)
{
  if (!$tipster->isTipster())
  {
    return redirect()->route('home');
  }
  return view('profile.index')->with([
    'tipster' => $tipster,
    'tips' => $tipster->tips(),
    'wins' => $tipster->tips()->where('status', 'Won')->count()
  ]);
}

That way I would be keeping the queries out of the view. I'm really new to laravel and 'best practice' so trying to get some advice.

  • 写回答

3条回答 默认 最新

  • duanqiao1949 2016-10-19 14:20
    关注

    You're asking about a best practice, which is generally frowned upon, but this really is something essential that every beginner should learn, so I still think it merits an answer.

    In brief: YES! What you're doing is a great first step towards keeping your code separated by logic. Your views should be responsible for displaying data, and your controllers for handling data over to the views. Whether your controllers should actually be responsible for calculating the data is another topic, and one which is constantly debated.

    That said, you could get this down to just a single line in the controller if you apply a little bit of other logic:

    public function tipsterProfileShow(User $tipster)
    {
      return view('profile.index', compact('tipster'));
    }
    

    The first step is to add a method to your User model, something like this:

    public function winCount()
    {
        return $this->tips()->where('status', 'Won')->count();
    }
    

    Now you can access $tipster->winCount() from your view. You can also access $tipster->tips() straight away in your view - most would agree that's perfectly fine.

    The second step is to extract the redirect call for non-tipsters into a middleware, which you can read about here: https://laravel.com/docs/5.3/middleware

    There are further steps you might take from there, but that's a good starting point. Good luck! :)

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题