我有一个 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/