I created a simple mobile app using React Native and for the data source, I created a simple API in PHP which fetches the data from a website and provides it to the users. Now I wanted to add notifications feature in my app and for that, I always need to know which data has been updated, I solved this problem by storing my data in two different tables in my DB and then comparing the changes, which is a lengthy procedure and not very efficient in my opinion. All my previous code was written in pure PHP without using any framework. Now I wanted to implement my whole project into Laravel based backend and don't know how can I achieve the functionality means getting updated data and then using that data to further send notifications to the users.
Summary of the functionality I want: - Parse data from a website - Look for new data - If there is new data send notifications -setting cron job to repeat
My old compare script looks like this:
<?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);
Can anyone please help me with this?
</div>
For storing you should use Model observer or if you want a package may be Laravel Auditing. this way you can avoid cron.
You should not make database connection like that even cron should be part of your laravel app not separated, there is Job Scheduler, you can use for it.
To run a job scheduler to update database, you can define an artisan command, that should hold your logic to update your DB and it will also help you in testing as you can just run the artisan command and test the query.
To generate an artisan command
php artisan make:command YourTableUpdate
And the class will be generated in console/ directory like
<?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
}
}
Now to create the scheduler
In your App\Console\Kernel
class
<?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();
}
}
you only need to add the following Cron entry to your server.
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
If you are using SSH and having ubuntu server, you can do something like
sudo crontab -e