我想每1小时更新相关表格中的1列。例如:
表cars
女巫hasMany
comments
在cars
表中我有列:
id
datetime
name
company
因此,car表已经每隔1小时从外部SQL文件自动更新。
我有comments
表女巫属于cars
表。 comments
表包含列:
id
car_id
datetime
name
company
body
所以我要做的是每comments
个datetime
列cars
列def fibonacci():
a, b = 0, 1
while True:
yield b
a, b = b, a + b
列。{/ p>
基本上我想同步两列到不同的表。
我能做到的任何想法?
答案 0 :(得分:0)
我设法找到解决方案:https://laravel.com/docs/5.4/scheduling
所以我究竟做了什么:
所以这是我的代码:
应用\控制台\ Krenel.php
<?php
namespace App\Console;
use App\Comment;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
// \App\Console\Commands\Inspire::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$comments = Comment::all();
foreach ($comments as $comment)
{
if (empty($comment->cars->datetime)) {
//If the row do not exist, Do nothing
} else {
$comment->datetime = $comment->cars->datetime;
$comment->save();
}
}
})->hourly();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}