Php 自动同步Laravel中的相关表

Php 自动同步Laravel中的相关表,php,mysql,laravel-5.4,Php,Mysql,Laravel 5.4,对不起,我是新手 我想每1小时更新相关表中的1列。例如: 表carswitch有许多注释 在cars表i中有列: id datetime name 公司 因此,car表已经每1小时从外部SQL文件自动更新一次 我有评论表格,该表格位于汽车表格之下。注释表有以下列: idcar\u id日期时间名称公司车身 因此,我想做的是每小时为cars中的datetime列复制comments表中的数据 基本上,我想将两列同步到不同的表中 有什么想法我能做到吗 我设法找到了一个解决方案: 所以我所做的就是:

对不起,我是新手

我想每1小时更新相关表中的1列。例如:

cars
witch
有许多
注释

cars
表i中有列:

id
datetime
name
公司

因此,car表已经每1小时从外部SQL文件自动更新一次

我有
评论
表格,该表格位于
汽车
表格之下。
注释表有以下列:

id
car\u id
日期时间
名称
公司
车身

因此,我想做的是每小时为
cars
中的
datetime
列复制
comments
表中的数据

基本上,我想将两列同步到不同的表中


有什么想法我能做到吗

我设法找到了一个解决方案:

所以我所做的就是:

  • 创建了一个一次手动更新一行的函数
  • 将此函数放入每行的循环中
  • 然后添加一个计划程序,每1小时执行一次此功能
  • 在我的服务器上设置crontab
  • 这是我的代码:

    App\Console\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');
        }
    }