dsu89430 2016-12-21 15:31
浏览 70
已采纳

在Laravel有更好的方法吗?

I am pretty new to Laravel and I have been coding a CMS for a roleplay game, and I have ran in to some code which I see as messy / bad practice, I was just wondering is this really the easiest and best way to do it? What I am mainly looking for is the best practice for doing this in the best possible way without changing too much of my code.

So what I need to do is I need to pass the users business and the users business position to the business overview view/page. I have modals for each of the tables for Business and BusinessPositions which I will post below.

Businesses:

<?php
namespace App\Database\Website\Roleplay\Business;

use Eloquent;

class Businesses extends Eloquent
{
    protected $primaryKey   = 'id';
    protected $table        = 'srp_businesses';
    public $timestamps      = false;
    protected $fillable     = [];

    public function positions()
    {
      return $this->hasMany('App\Database\Roleplay\Business\BusinessPositions', 'business_id');
    }

    public function founder()
    {
      return $this->belongsTo('App\Database\Website\User\Player');
    }
}

BusinessPositions:

<?php
namespace App\Database\Website\Roleplay\Business;

use Eloquent;

class BusinessPositions extends Eloquent
{
    protected $primaryKey   = 'id';
    protected $table        = 'srp_business_positions';
    public $timestamps      = false;
    protected $fillable     = [];
}

And here is the controller function for sending my view

$businesses = Cache::remember('overview.businesses', 1, function() {
    return Businesses::get();
});

$businessPositions = Cache::remember('overview.business.positions', 1, function() {
    return BusinessPositions::get();
});

$acceptedCount = $businesses->where('business_owner_id', '=', Auth::user()->id)
    ->where('business_status', '=', 'accepted')
    ->count();
$pendingCount = $businesses->where('business_owner_id', '=', Auth::user()->id)
    ->where('business_status', '=', 'pending')
    ->count();
$myBusiness = Businesses::find(Auth::user()
    ->roleplay->business_id);
$myBusinessPosition = $businessPositions->where('business_id', '=', $myBusiness->business_id)
    ->where('position_id', '=', Auth::user()->roleplay->business_position)
    ->first();

return view('frontend.business.overview', compact('acceptedCount', 'pendingCount', 'myBusiness', 'myBusinessPosition'));

The part I see as messy is the way I get the business position, and maybe there is a way to get the business better?

How I get the business position (breakdown of code)

$myBusinessPosition = $businessPositions->where('business_id', '=', $myBusiness->business_id)
    ->where('position_id', '=', Auth::user()
        ->roleplay->business_position)
    ->first();

I am using Laravel 5.3, Thanks.

  • 写回答

2条回答 默认 最新

  • dongyu1125 2018-07-31 04:15
    关注

    You can use scopes, click here to go and read the docs on this.

    Doesn't this look much cleaner?

    $user->businesses()->ofStatus('accepted')->count();
    $user->businesses()->ofStatus('pending')->count();
    

    Also, you should fix up your calls to the auth, instead of making raw calls like this.

    ->where('business_owner_id', '=', Auth::user()->id)
    

    Create a variable, and safe it to use it further down the scope of your code.

    $user = Auth::user()
    

    Then, just use the user, not the auth code.

    ->where('business_owner_id', '=', $user->id)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图