Laravel 5在选择查询中选择

时间:2017-03-05 17:10:40

标签: php mysql laravel laravel-5 laravel-5.2

我有一个php网站。我想在laravel 5中重写它。问题在于:

    $getAllTrainings = $db->getAll('SELECT * from add_timetable WHERE active_before >= CURDATE() AND complete = ?i', 0);

        foreach ($getAllTrainings as $training) {
            $currentTraining = $db->getOne('SELECT service_id from add_people WHERE service_card = ?s AND training_id = ?s', $service_card, $training['code']);
            if($currentTraining) {
                   $reg_button = <<<ECHO BUTTON_1
            }  else {
                   $reg_button = <<<ECHO BUTTON_2
            }
            echo <<<TRAINING
        <div class="training__item" id="{$training['code']}" >
        <h3>{$training['header']}</h3>
        <p>{$training['description']}</p>
        <div class="datetime is-flex">
            <div class="date">
                {$training['date']}
            </div>
            <div class="time">
                {$training['time']}
                <div class="small">
                    {$training['place']}
                </div>
            </div>
        </div>
        {$reg_button}
    </div>
TRAINING;
        }

问题是回显不同的按钮(按查询查询)

if($currentTraining) {
                   $reg_button = <<<ECHO BUTTON_1
            }  else {
                   $reg_button = <<<ECHO BUTTON_2
            }

如何在Laravel中完成?

1 个答案:

答案 0 :(得分:0)

首先,您需要创建名为&#39; Training&#39; (运行命令:php artisan make:model Training),然后在控制器中加载模型(使用App \ Training; ),然后运行集合:< / p>

$trainings = Training::where('complete', 1)->whereDate('active_date', '>=', Carbon::now()')
// note the Carbon usage, you will need to add it by adding use Carbon;

foreach($trainings as $training){
   // do something with your collection here.
}

当然,您需要在数据库中建立表格并建立连接,如果您不需要了解有关迁移的更多信息,请制作表格&#39;培训&#39;并迁移它。