php - Laravel 5 在选择查询中选择

标签 php mysql laravel laravel-5

我有一个 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 中做到这一点?

最佳答案

首先,您需要创建名为“Training”的模型(运行命令:php artisan make:model Training),然后将模型加载到 Controller 中(use App\Training; em>),然后运行集合:

$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.
}

当然,您需要在数据库中拥有该表并建立连接,如果没有,那么您需要了解有关迁移的更多信息,创建一个表“培训”并迁移它。

关于php - Laravel 5 在选择查询中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42611378/

相关文章:

php - 限制 codeigniter 分页中的链接数量

php - Laravel 中的密码确认

laravel - 表之间的关系不起作用

php - CLI CURL -> PHP CURL

php - 使用 PHP simpleXml 将 child 添加到 xml

php - range() 和 in_array() 行为,我在这里做错了什么?

wordpress - 如何使用漂亮的永久链接(SEO 友好的 URL)在 Nginx 上与 Laravel 一起安装 WordPress?

php - While 循环内 While 循环

php - 连接行返回数组内爆错误

php - Mysql ON DUPLICATE KEY 错误