Artisan Command Calling Controller

Asked

Viewed 693 times

0

I did a job on a Controller and created a Command in the Artisan to run this function.

Follows:

Controller

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\EmpresaRating;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CronController extends Controller
{
    public static function fireCron(){
        $ratings = EmpresaRating::where('quarentena', '=', 1)
        ->whereRaw("DATE_FORMAT(data_publicacao, '%Y-%m-%d') = '".date('Y-m-d')."'");

        if($ratings->exists()){
            $ratings->update(['quarentena' => 0]);
        }
    }
}

Croncommand

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Controllers\Controller;

class CronCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cron_services';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Cron para atualizar avaliações que estão em quarentena após 7 dias.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $controller = app()->make('App\Http\Controllers\CronController');
        app()->call([$controller, 'fireCron'], []);
    }
}

Kernel

protected $commands = [
    \App\Console\Commands\Inspire::class,
    \App\Console\Commands\CronCommand::class,
];

When to perform php artisan cron_services give me that mistake:

[Symfony Component Debug Exception Fatalerrorexception] Call to a Member Function getPath() on null

What am I missing ?

  • It’s to bring up a list. Laravel 5.1.

  • So hard that I already solved this with a Service Provider. I was just taking a break in the question. But for the purpose of answering, I put a dd(1) before calling Empresarating and he killed the code right there.

  • No bro. I don’t want to list the records. I just do a search, ask if there is a record. If yes, I already send an update.

1 answer

0


It seems that for some reason Command was not accessing my Controller. So I did using Service Provider.

I created a Prior called Cronprovider:

Cronprovider

public function boot(){

}

public function register(){
   $this->app->singleton('App\Cron\Cron', function ($app) {
       return new Cron;
   });
}

public function provides(){
   return ['App\Cron\Cron'];
}

I created a folder called Cron and created within it a file called Cron.php.

Cron

public static function isDeferred(){
   return true;
}

public function provides(){
   return [
      'App\Cron\Cron'
   ];
}

public function fireCron(){
   $ratings = EmpresaRating::where('quarentena', '=', 1)
   ->whereRaw("DATE_FORMAT(data_publicacao, '%Y-%m-%d') = '".date('Y-m-d')."'");

   if($ratings->exists()){
       $ratings->update(['quarentena' => 0]);
   }
}

Then I created a command called CronCommand, not forgetting the use App\Cron\Cron to call my class.

Croncommand

protected $signature = 'cron:services';

protected $description = '7 dias.';

use App\Cron\Cron;

public function __construct()    {
   parent::__construct();
}

public function handle(Cron $cron){
   $cron->fireCron();
}

I registered on Kernel.php of the briefcase Commands.

protected $commands = [
    \App\Console\Commands\Inspire::class, 
    \App\Console\Commands\CronCommand::class
];

I put it in the file app.php of the briefcase config.

App\Providers\CronProvider::class,

Then I ran the Commodore:

php artisan cron:services

Worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.