【问题标题】:Laravel, passing new function to bladeLaravel,将新功能传递给刀片
【发布时间】:2018-09-22 19:34:42
【问题描述】:

我有一个显示不同数据样本的工作类文件、控制器和视图,但我目前正在添加一个新函数,该函数从数据库中获取数据,将其传递给控制器​​并定义它​​,然后将其传递给刀片/视图。

我并没有真正做任何与其他功能不同的事情,但数据没有显示在我的刀片中。占位符在网页上的间距适当,但数据不显示。查询正在获取结果,并且函数正在传递给刀片,因为 foreach 正在创建额外的行。我在想我对刀片中的变量使用了不正确的语法,这就是我的数据实际上没有显示的原因。

任何帮助在这里表示赞赏:

EMPLOYEE.php

class employeeCalls
{
    public function __construct()
    {
        $this->yyyy = date('Y');
        $this->pyyy = $this->yyyy - 1;
        $this->from = "{$this->pyyy}-01-01";
        $this->through = $this->pyyy . '-' . date('m-d');
        $this->fullYear = "{$this->pyyy}-12-31";
        $this->newFrom = "{$this->yyyy}-01-01";
        $this->newThrough = date('Y-m-d'); 
    }

public function sample($employee)
    {
        $employee = (int) $employee;
        $from = $this->from;
        $through = $this->through;
        $newFrom = $this->newFrom;
        $newThrough = $this->newThrough;
        $FullYear = $this->fullYear;

        $sql = "
                select employee, 'PRIOR' as Range, count(*) as count
                from empCalls
                where employee = {$employee}
                AND fordate between '{$from}' and '{$through}'
                group by employee
            union all
                select employee, 'CURRENT' as Range, count(*) as count
                from empCalls
                where employee = {$employee}
                AND fordate between '{$newFrom}' and '{$newThrough}'
                group by employee
            union ALL
                select employee, 'FULL' as Range, count(*) as count
                from empCalls
                where employee = {$employee}
                AND fordate between '{$from}' and '{$FullYear}'
                group by employee
        ";

        return Connection::runQuery($sql);
    }
}

EmployeeController.php

    $employeeCalls = new employeeCalls();

    $samples = $employeeCalls->sample($this->slsno);

    return view('Employee.index')
            ->with('slsno', $this->slsno)
            ->with('samples', $samples);
    }

blade.php

<div class="md-card-content">
    @foreach($samples as $sample)
            <div class="uk-float-right uk-margin-small-right"><span class="uk-text-medium uk-text-center">{{ $sample['PRIOR'] }}</span></div>
            <span class="uk-text-muted uk-text-medium">2017 YTD</span>
            <div class="clearfix"></div>
            <div class="clearfix"></div>
            <hr />
            <div class="uk-float-right uk-margin-small-right"><span class="uk-text-medium uk-text-center">{{ $sample['CURRENT'] }}</span></div>
            <span class="uk-text-muted uk-text-medium">2018 YTD</span>
            <div class="clearfix"></div>
            <div class="clearfix"></div>
            <hr />
            <div class="uk-float-right uk-margin-small-right"><span class="uk-text-medium uk-text-center">{{ $sample['FULL'] }}</span></div>
            <span class="uk-text-muted uk-text-medium">2017 Full Year</span>
            <div class="clearfix"></div>
            <div class="clearfix"></div>
            <hr />
    @endforeach
</div>

更新:

这是从 $samples 转储的内容

array:3 [▼
0 => array:3 [▼
"employee" => "495"
"RANGE" => "PRIOR"
"COUNT" => "119"
]
1 => array:3 [▼
"employee" => "495"
"RANGE" => "CURRENT"
"COUNT" => "68"
]
2 => array:3 [▼
"employee" => "495"
"RANGE" => "FULL"
"COUNT" => "440"
]
] 

【问题讨论】:

  • 把$samples里的东西都倒出来,有什么东西吗?
  • 很难说。目前还不清楚Connection::runQuery 做了什么或返回了什么。你为什么要像这样绕过 Laravel 的所有模型和数据库功能来调用你自己的数据库?
  • @Andrew 我在刀片上做了一个 dd 预期值,发布在我的更新中
  • 所以它有数据??你检查过页面吗?也许 md-card-content 被隐藏了??
  • 您的刀片模板命名是否正确? .blade.php

标签: php laravel


【解决方案1】:

在您的 EmployeeController.php 上

声明一个私有变量 protected $data = array();如下所示

class EmployeeController extends Controller
{
    protected $data = array();

并在函数上修改

$employeeCalls = new employeeCalls();
$samples = $employeeCalls->sample($this->slsno);
return view('Employee.index')
       ->with('slsno', $this->slsno)
       ->with('samples', $samples);
}

如下图

$employeeCalls = new employeeCalls();
$this->data['samples'] = $employeeCalls->sample($this->slsno);
$this->data['slsno'] = $this->slsno;
return view('Employee.index')
        ->with($this->data);
}

这将解决我希望的问题

【讨论】:

    【解决方案2】:

    问题可能在于链接 with() 方法。 尝试改变这个:

    return view('Employee.index')
            ->with('slsno', $this->slsno)
            ->with('samples', $samples);
    

    为此:

    return view('Employee.index', [
      'slsno' => $this->slsno, 
      'samples' => $samples
    ]);
    

    【讨论】:

    • 但是里面的其他东西都没有问题
    【解决方案3】:

    改变你的blade.php

    <div class="md-card-content">
                <div class="uk-float-right uk-margin-small-right"><span class="uk-text-medium uk-text-center">{{ $samples[0]['COUNT'] }}</span></div>
                <span class="uk-text-muted uk-text-medium">2017 YTD</span>
                <div class="clearfix"></div>
                <div class="clearfix"></div>
                <hr />
                <div class="uk-float-right uk-margin-small-right"><span class="uk-text-medium uk-text-center">{{ $samples[1]['COUNT'] }}</span></div>
                <span class="uk-text-muted uk-text-medium">2018 YTD</span>
                <div class="clearfix"></div>
                <div class="clearfix"></div>
                <hr />
                <div class="uk-float-right uk-margin-small-right"><span class="uk-text-medium uk-text-center">{{ $samples[2]['COUNT'] }}</span></div>
                <span class="uk-text-muted uk-text-medium">2017 Full Year</span>
                <div class="clearfix"></div>
                <div class="clearfix"></div>
                <hr />
    
    </div>
    

    【讨论】:

    • 哇,完美!而且比我想象的要简单得多..非常感谢
    • 问题:如果我想获得先前和当前之间的百分比变化,您建议在 div 的刀片中还是在控制器中这样做?
    • 如果仅出于显示目的我更喜欢视图而不是控制器
    • 这只是为了显示,但这只是简单的数学运算,所以我不知道在视图中是否可以。基本上只是(current-prior)/prior
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2019-08-01
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多