How I can create automatically record daily with laravel task scheduling without running artisan command. In my case I just want when the date is 28-02-2020 create record in db, when date is 29-02-2020 create another record and etc...
Just check Laravel docs on Task Scheduling. First add this cron entry:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Then, You may define all of your scheduled tasks in the schedule method of the App\Console\Kernel class:
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
DB::table('your_table')->insert([
'first_column' => 'first_value',
// List other columns
]);
})->daily();
}
Related
I have a migration that create a view in MySQL. when I run
PHP artisan db:wipe
it drops all the tables but not the view.
when I run
php artisan migrate
it says base table or view already exists.
my migration
public function up()
{
DB::statement("
CREATE VIEW contacts_view
AS
SELECT
*
FROM
accounts
where isContact='yes';
");
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Schema::dropIfExists('contacts_view');
DB::statement("DROP VIEW contacts_view");
}
To avoid errors when running your migration with existing view, sue CREATE OR REPLACE
public function up()
{
DB::statement("
CREATE OR REPLACE VIEW contacts_view
AS
SELECT
*
FROM
accounts
where isContact='yes';
");
}
The view will always be recreated.
I'm working on a system for payments for a school and I need to automatically add a new monthly debt for students, related to the month price. I was thinking about using scheduled events but it hasn't worked when I'm testing.
Please could anyone tell me if this is a good approach or give me any piece of advice for another approach? Thanks in advance.
This is what I was testing but it seems that it's not running since there's no new value on the debts table.
delimiter $$
create definer=`root`#`localhost` event registrar_mensualidad
on schedule every 5 second
on completion preserve
enable
do
begin
insert into debts(auto, amount, student_id, institution_payment_reason_id)
values (1, 100, 1, 1);
end;$$
I checked if the events_scheduler is enabled and it is in fact.
The system is being developed with laravel connected to a mysql database.
You might consider cron jobs.
CRONTAB (at every 1st day of the month)
#: sudo nano /etc/crontab
0 0 1 * * root php /var/www/html/your_php_query.php > output.txt
PHP
$qry = "insert into debts(auto, amount, student_id, institution_payment_reason_id)
values (1, 100, 1, 1);"
return $con->query($qry);
LOG
ouput.txt
1 // if insert is true
Or Laravel
App\Console\Kernel
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
.........
// add this
$schedule->call(function () {
DB::insert('insert into debts(auto, amount, student_id, institution_payment_reason_id) values (?, ?, ?, ?)', [1, 100, 1, 1]);
})->monthly();
}
}
To check/validate the schedule, type: php artisan schedule:list
Output:
0 0 1 * * Closure at: app\Console\Kernel.php:22 Next Due: 3 weeks from now
since the last update of my server which happened last month, I face an unusual problem with Doctrine.
When I run doctrine:schema:update --force, I the result "54 queries executed". The same result happens even if I don't change my entities.
When I run doctrine:schema:update --dump-sql to see the queries, I can see that same queries are run. Ex :
ALTER TABLE artisan CHANGE creator_id creator_id INT DEFAULT NULL;
ALTER TABLE user_connection CHANGE date date DATETIME DEFAULT NULL;
ALTER TABLE device CHANGE device_type device_type VARCHAR(500) DEFAULT 'ios' NOT NULL;
The problem is that columns are from the same type that the one changed by the query. I don't know why Doctrine want to change the type.
MariaDB version is 10.4.13 - Doctrine version is 2.5.14 - Symfony version is 2.8.42
Entity examples :
Device : device_type
/**
* #var string
*
* #ORM\Column(name="device_type", type="string", length=500, options={ "default":"ios" })
*/
private $device_type = "ios";
UserConnection : date
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
Artisan : Creator
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="MiddlewareBundle\Entity\User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="creator_id", referencedColumnName="id", nullable=true)
* })
*/
private $creator;
There has been a pull request some years ago, have a look at https://github.com/doctrine/dbal/pull/2825 - it clearly states your problem:
Doctrine currently does not support MariaDB 10.2.7+. Schema creation/update is triggering infinite schema diffs
The code from this pull request has been included since doctrine/dbal:2.7.0, so you should consider updating at least that single package
For a new Laravel project, I need to use an existing MySQL database (176 tables). I don't want to create a Laravel migration for each existing table, so I've made an export of the database structure to a sql file.
In a migration I want to execute the SQL file, like so:
public function up()
{
DB::unprepared(file_get_contents('/path/to/file.sql'));
}
unprepared returns true but it seems the import will not be (fully) executed. No error, no effect (sometimes, there are 1 or 2 tables created, for example after dropping and recreating the database before executing the sql file).
When I execute this file with mysql source /path/to/file.sql, the import works fine (some errors by version difference will be reported, but the execution continues).
My question: for testing purposes, I want to creating the 176 old/existing tables from an SQL file during the migration process. I need to alter some tables during the migration process.
I don't want to create a migration for each table.
You can do reverse migration for all your tables by following these steps:-
1)composer require --dev "xethron/migrations-generator"
2)in bootstrap/app -$app->register(\Way\Generators\GeneratorsServiceProvider::class);
$app->register(\Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class);
3)In bootstrap/app.php add
class Application extends Laravel\Lumen\Application
{
/**
* Get the path to the application configuration files.
*
* #param string $path Optionally, a path to append to the config path
* #return string
*/
public function configPath($path = '')
{
return $this->basePath.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}
if (!function_exists('config_path')) {
/**
* Get the configuration path.
*
* #param string $path
* #return string
*/
function config_path($path = '')
{
return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
}
}
if (!function_exists('app_path')) {
/**
* Get the path to the application folder.
*
* #param string $path
* #return string
*/
function app_path($path = '')
{
return app('path') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
}
}
class_alias('Illuminate\Support\Facades\Config', 'Config');
$app = new Application(
realpath(__DIR__.'/../')
);
4) write php artisan migrate:generate in terminal
5) change
$app = new Application(
realpath(__DIR__.'/../')
);
to
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);
in bootstrap/app.php
I have built some REST API in Lumen micro framework and it's working fine. Now I want to integrate Swagger into it so the API will be well documented on future use. Has anyone done this?
Steps to follow for Laravel Lumen 5.7 with swagger using OpenApi 3.0 specs (this governs the way you write annotations so that swagger documentation is generated)
The steps were written a while ago but they still work with Laravel Lumen 6.X, 7.X and 8.X
I reached this adjusting on #black-mamba answer in order to make it work.
1. Install swagger-lume dependency (which also installs swagger)
composer require "darkaonline/swagger-lume:5.6.*"
2. Edit bootstrap/app.php file
make sure $app->withFacades(); is NOT commented (around line 26)
in Create The Application section add the following before Register Container Bindings section
$app->configure('swagger-lume');
in Register Service Providers section add
$app->register(\SwaggerLume\ServiceProvider::class);
3. Publish configuration file for swagger-lume
php artisan swagger-lume:publish
4. Add annotations to your code
For example in your app/Http/Controllers/Controller.php you could have the general part of the documentation
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController
{
/**
* #OA\Info(
* title="Example API",
* version="1.0",
* #OA\Contact(
* email="support#example.com",
* name="Support Team"
* )
* )
*/
}
And inside each of your controllers you should have the appropriate annotations above each public method
<?php
namespace App\Http\Controllers;
class ExampleController extends Controller
{
/**
* #OA\Get(
* path="/sample/{category}/things",
* operationId="/sample/category/things",
* tags={"yourtag"},
* #OA\Parameter(
* name="category",
* in="path",
* description="The category parameter in path",
* required=true,
* #OA\Schema(type="string")
* ),
* #OA\Parameter(
* name="criteria",
* in="query",
* description="Some optional other parameter",
* required=false,
* #OA\Schema(type="string")
* ),
* #OA\Response(
* response="200",
* description="Returns some sample category things",
* #OA\JsonContent()
* ),
* #OA\Response(
* response="400",
* description="Error: Bad request. When required parameters were not supplied.",
* ),
* )
*/
public function getThings(Request $request, $category)
{
$criteria= $request->input("criteria");
if (! isset($category)) {
return response()->json(null, Response::HTTP_BAD_REQUEST);
}
// ...
return response()->json(["thing1", "thing2"], Response::HTTP_OK);
}
}
5. Generate swagger documentation
php artisan swagger-lume:generate
Run this each time you update the documentation
see:
https://zircote.github.io/swagger-php/
https://github.com/DarkaOnLine/SwaggerLume
https://swagger.io/specification/
I had really hard time in learning how to use it. Here I'm going to share some of the things I did to get it working
Use this wrapper
Run this command in your terminal:
composer require "darkaonline/swagger-lume:5.5.*"
Or older version from the repo linked if this doesn't work for you
Then from the repo follow these steps:
Open your bootstrap/app.php file and:
uncomment this line (around line 26) in Create The Application
section:
$app->withFacades(); add this line before Register Container Bindings section:
$app->configure('swagger-lume'); add this line in Register Service Providers section:
$app->register(\SwaggerLume\ServiceProvider::class);
Then you'll need to use annotations for your project instead of YAML or JSON
For more
Create a Annotation.php file in your app folder add the following a sample
/**
* #SWG\Swagger(
* basePath="/",
* schemes={"http"},
* #SWG\Info(
* version="1.0.0",
* title="HAVE MY BOOKS",
* #SWG\Contact(
* email="example#test.com"
* ),
* )
* )
*/
/**
* #SWG\Get(
* path="/",
* summary="Version",
* #SWG\Response(
* response=200,
* description="Working"
* ),
* #SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
Then run
php artisan swagger-lume:publish
Then run
php artisan swagger-lume:generate
This generates JSON which can be accessed from your localhost:8000 or whichever port you are serving your LUMEN service
Note: after creating an issue in the repo I found that to access you'll need to serve or run using
php -S localhost:8000 public/index.php
Because of some PHP routing issue with php -S localhost:8000 public