PHP code example of e282486518 / laravel-table-sync
1. Go to this page and download the library: Download e282486518/laravel-table-sync library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
e282486518 / laravel-table-sync example snippets
namespace App\Services;
use App\Models\Meeting;
use e282486518\LaravelTableSync\SyncService;
/**
* 增量同步数据到c_meeting表
*/
class MeetingSyncService extends SyncService
{
// A服务器API地址
protected string $apiUrl = 'https://api.xxx.com/api/meeting/sync';
// API令牌
protected string $apiToken = 'xxxxxx';
/**
* 更新数据
* @param $id
* @return void
*/
protected function afterUpdate($id): void {
Meeting::getOne($id, true);
}
/**
* 记录信息级别日志
* 子类可以重写此方法来自定义日志行为
*
* @param string $message 日志消息
*/
protected function logInfo(string $message): void
{
logger()->channel('other')->info($message);
}
/**
* 记录错误级别日志
* 子类可以重写此方法来自定义日志行为
*
* @param string $message 日志消息
*/
protected function logError(string $message): void
{
logger()->channel('other')->error($message);
}
}
public function handle() {
$serv = new MeetingSyncService(new Meeting());
if ($serv->sync()) {
echo "更新成功...\n";
} else {
echo "更新出错...\n";
}
return Command::SUCCESS;
}
namespace App\Http\Controllers;
use App\Models\Meeting;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class MeetingController extends Controller
{
/**
* 增量更新数据
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function sync(Request $request)
{
// 最后更新时间
try {
$request->validate([
'time' => 'ut('catid', 0);
// 获取更新的数据
$model = Meeting::query()->where('updated_at', '>', $time);
if (!empty($year)) {
$model->where('year', $year);
}
if (!empty($catid)) {
$model->where('cat_id', $catid);
}
$meetings = $model->orderBy('updated_at', 'desc')->get();
// 最后更新时间
if (!$meetings->isEmpty()) {
$latestUpdate = $meetings->first()->updated_at->format('Y-m-d H:i:s');
} else {
$latestUpdate = $time;
}
// 返回
return response()->json([
'err' => 0,
'msg' => 'success',
'data' => $meetings,
'latest_update' => $latestUpdate
])->setEncodingOptions(320);
}
}