A
A
Alexey Nikolaev2018-09-22 16:21:48
Laravel
Alexey Nikolaev, 2018-09-22 16:21:48

Why does Laravel swear when I pass a model in a job's constructor?

Good day.
There is an instance of Jobs\UpdateTable. There is a constructor.

/**
 * Create a new job instance.
 *
 * @return void
 */
 public function __construct(Products $Model) // <-- путь корректен, я указал use
 {
    $this->Model = $Model; // если вот это убрать, все будет работать нормально
 }

This task is scheduled in the Laravel scheduler (app\Console\Kernel.php, shedule method).
$schedule->job(Jobs\UpdateTable::class)->everyMinute();

The Products model is empty. There is a table in the database, but it is also empty.
namespace App;

use Illuminate\Database\Eloquent\Model;

class Products extends Model
{
    //
}

At the same time, when I run the task, everything fails with the error "Illuminate\Database\Eloquent\ModelNotFoundException : No query results for model [App\Products]", although I did not select anything from the model and did nothing with the results. I'm running from the console.
$ php artisan schedule:run
// Вот это вот приведет к ошибке
// Illuminate\Database\Eloquent\ModelNotFoundException  : No query results for model [App\Products]

I expect the framework to just pass an instance of the model class to the instance task, and that's it! But no, it doesn't work for some reason.
The strangest thing is that when I remove the assignment of the model instance ($this->Model = $Model) in the Jobs\UpdateTable class, everything works fine.
Why is he trying to get something from the model? How do I get Laravel to let me store a model instance in a property?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-09-22
@Heian

Why did you decide that the framework itself will inject the model into the constructor? Didn't you read the documentation ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question