I am working on a Laravel App, and in a Migration, I want to update certain objects. For this my Migration has the following code:
public function up()
{
//Update the filter whose current name is City
$filter= App\Filter::where('name', 'City')->firstOrFail();
if($filter){
$filter->name='City Region Type';
$filter->description='City Region Type';
$filter->save();
}
}
I also have an Observer on this class which puts in the user_id of the user who is doing the update, using:
public function updating(Filter $filter)
{
$filter->updated_by = auth()->user()->id;
}
Running this migration fails, because it cannot get the user_id.
What's the Recommended way to write a Migration when you have an Obser set up like this?
Or Do I give up and use Raw SQL?
There is one way in Laravel in which you can check whether application on console or web. So you can use below code in observer. And if App is on console then no need to run code inside observer.
public function updating(Filter $filter)
{
if (\App::runningInConsole()) {
return false;
}
$filter->updated_by = auth()->user()->id;
}
I hope this will help you.