I'm trying to make cron tasks on my laravel project for the very first time, I have to admit I'm a bit confused. I made a simple command to publish articles that were planned but there is something wrong with my eloquent query. I can't figure it out, can you help me find what I'm missing ?
Do I even have the right approach for this process ?
Articles have a status and a publish_at column, I'm trying to compare the publish_at date with current date every minute and if there is a match, change the status input to 'published' to publish one or multiple articles.
Kernel.php :
protected function schedule(Schedule $schedule)
{
$schedule->command('articles:publish')->everyMinute();
}
Commands/PublishArticles.php :
use App\Models\Article;
use Illuminate\Console\Command;
class PublishArticle extends Command
{
protected $signature = 'articles:publish';
public function __construct()
{
parent::__construct();
}
public function handle()
{
// retrieve all planned articles that should be published now
$articles = Article::where('status', 'planned')->where('publish_at', now())->get();
foreach($articles as $article) {
$article->status = 'published';
$article->update();
}
return 0;
}
}
I tried a few alternative for the query, like :
$articles = Article::where('status', 'planned')->where('publish_at', '>', now())->get();
$articles = Article::where('status', 'planned')->where('publish_at', '<', now())->get();
$articles = Article::where('status', 'planned')->where('publish_at', '>=', now())->get();
$articles = Article::where('status', 'planned')->where('publish_at', '<=', now())->get();
I also tried to use now() with ->toDateTimeString in case this was a date conversion problem.
EDIT :
Here are some more informations about the enviromnent.
The timezones of the app and the server are the same, as specified in config/app.php
When using the eloquent query in php tinker, here is what I get for previous articles :
input :
$articles = Article::where('status', 'planned')->where('publish_at', '<', now())->get();
output :
all: [
App\Models\Article {#4275
id: 21,
user_id: 1,
title: "Article test 11",
slug: "article-test-11",
theme: "status",
mainContent: "content",
thumbnail: "IMG_20200926_234854.jpg",
status: "planned",
publish_at: "2021-04-14 20:10:00",
created_at: "2021-04-14 19:02:54",
updated_at: "2021-04-14 20:07:39",
},
]
My Answer:
In Laravel, Eloquent is the ORM (Object-Relational Mapping) that allows you to interact with your database using PHP syntax. Here is an example of an Eloquent query in Laravel:
php
// Retrieve all users
$users = User::all();
// Retrieve a specific user by ID
$user = User::find(1);
// Retrieve users with a specific condition
$users = User::where('age', '>', 18)->get();
// Retrieve users with a specific condition and order by a column
$users = User::where('age', '>', 18)->orderBy('name', 'asc')->get();
// Retrieve users with a specific condition and limit the results
$users = User::where('age', '>', 18)->limit(10)->get();
These are just a few examples of Eloquent queries in Laravel. You can also use Eloquent relationships to retrieve related data from multiple tables. Eloquent provides a powerful and expressive way to interact with your database in Laravel.
Rate this post
4 of 5 based on 3232 votesComments