我使用React Native创建了一个简单的移动应用,对于数据源,我用PHP创建了一个简单的API,该API从网站上获取数据并将其提供给用户。现在,我想在我的应用程序中添加通知功能,为此,我总是需要知道已更新了哪些数据,我通过将数据存储在数据库中的两个不同表中并比较更改来解决此问题,这很长程序,在我看来不是很有效。我以前的所有代码都是用纯PHP编写的,没有使用任何框架。现在,我想将我的整个项目实施到基于Laravel的后端中,不知道如何实现该功能意味着获取更新的数据,然后使用该数据进一步向用户发送通知。
我想要的功能摘要: -解析网站数据 -寻找新数据 -如果有新数据发送通知 设置cron作业重复
我以前的比较脚本如下:
<?PHP
include '../config/dbData.php';
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$connect = mysqli_connect($HostName, $HostUser, $HostPass);
mysqli_select_db($connect, $DatabaseName);
mysqli_query($connect, "SET NAMES 'UTF8'") or die("ERROR: " . mysqli_error($connect));
$fetch3 = mysqli_query($connect, "SELECT * FROM timetable
WHERE id NOT IN (SELECT id FROM TimetableStudentCompare)
UNION
SELECT * FROM TimetableStudentCompare
WHERE id NOT IN (SELECT id FROM timetable)
UNION
SELECT * FROM timetable
WHERE Vertreter NOT IN (SELECT Vertreter FROM TimetableStudentCompare)
UNION
SELECT * FROM TimetableStudentCompare
WHERE Vertreter NOT IN (SELECT Vertreter FROM timetable)
UNION
SELECT * FROM timetable
WHERE Std NOT IN (SELECT Std FROM TimetableStudentCompare)
UNION
SELECT * FROM TimetableStudentCompare
WHERE Std NOT IN (SELECT Std FROM timetable)
UNION
SELECT * FROM timetable
WHERE Klasse NOT IN (SELECT Klasse FROM TimetableStudentCompare)
UNION
SELECT * FROM TimetableStudentCompare
WHERE Klasse NOT IN (SELECT Klasse FROM timetable)");
$array3 = array();
while ($row3 = mysqli_fetch_assoc($fetch3)) {
$array3[] = $row3;
}
echo json_encode($array3, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
// Fetching Klassen to a new array
$KlasseArray = array();
foreach ($array3 as $value) {
array_push($KlasseArray, $value['Klasse']);
}
// array only with classes
print_r($KlasseArray);
//Removing Duplicates
$list = $KlasseArray;
sort($list);
foreach ($list as $k => $v) {
if (isset($check)) {
if ($check === $v) {
unset($list[$k]);
}
}
$check = $v;
}
$noDuplicate = array_values($list);
// Result klassen with no duplicates
print_r($noDuplicate);
mysqli_close($connect);
有人可以帮我吗?
答案 0 :(得分:0)
对于存储,您应该使用模型观察器,或者如果您想打包,可以使用Laravel Auditing。这样可以避免cron。
您不应建立数据库连接,即使cron不属于您的laravel应用程序的一部分,也可以使用Job Scheduler。
要运行作业调度程序以更新数据库,您可以定义一个artisan命令,该命令应保留您的逻辑以更新您的数据库,它还可以帮助您进行测试,因为您只需运行artisan命令并测试查询。
生成工匠命令
php artisan make:command YourTableUpdate
该类将在控制台/目录中生成,例如
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class YourTableUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'your-table:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Updates your table';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @param
* @return mixed
*/
public function handle()
{
//logic to update your table
}
}
现在创建调度程序
在您的App\Console\Kernel
班上
<?php
namespace App\Console;
use Illuminate\Support\Facades\DB;
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\YourTableUpdate
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('your-table:update')->daily();
}
}
您只需要将以下Cron条目添加到您的服务器。
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
如果您使用SSH并拥有ubuntu服务器,则可以执行类似的操作
sudo crontab -e