Laravel,将新功能传递给刀片

时间:2018-04-12 15:11:08

标签: php laravel

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

我实际上并没有做与其他功能不同的事情,但数据没有显示在我的刀片中。占位符在网页上适当间隔,但数据不会显示。查询正在获取结果,并且函数正在传递给刀片,因为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"
]
] 

3 个答案:

答案 0 :(得分:1)

更改你的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>

答案 1 :(得分:0)

在您的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 :(得分:-1)

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

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

为此:

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