'Laravel call dependency injected class in for loop
I have a Laravel controller where I am using dependency injection. So my controller is like
use App\Services\LocationService
class LeaveController extends Controller
{
protected $location_service;
public function __construct(LocationServcice $location_service){
$this->location_service= $location_service;
}
public function carryForward()
{
foreach ($users as $user) {
$this->location_service->carryForward($user);
}
}
.........
}
In the service, I am inserting data into a database table. In that also I am using dependency injection. The problem is that the insertion happens only for the first user. I think it is because I am using the dependency injection, the model class gets instantiated only once. How to solve this? Or do I need to use the DB facade?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
