Eloquent: get changed columns - laravel

I need to log updated columns using Eloquent getChanges, It works just fine when use it in the same code context right after ex: updateOrCreate, but when call it from the "provider" it returns empty array, example of boot in service provider:
public function boot()
{
User::updating(function ($user) {
dd($user->getChanges()); //empty array
});
}

I get the problem I should use User::updated instead, ex:
public function boot()
{
User::updated(function ($user) {
dd($user->getChanges());
});
}

Related

Laravel How can I configure "reserved_at" in the jobs table

I want to use Carbon::today()->addMonth() as default value of reserved_at column in jobs table
then I will use ite in the job in the way of
if(reserved_at == Carbon->today())
{
//execution
} else {
//don't
}
You can set reserved_at in model's creating event within the model boot() method, like that add following method in Job model:
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->reserved_at = Carbon::today()->addMonth();
});
}
It will append reserved_at field value in model when you storing data using eloquent methods.
Note: It'll not work with insert() query builder method.
I figured it out it's by using the function of the class InteractsWithQueue
->delay(Carbon::now()->addMinutes(5)) with the dispatch function

laravel nova observer trying to get property on non object

I set a database connection by the auth()->user()-dbname
This works as desired using this in the model
public function __construct() {
$this->connection = auth()->user()->dbname;
}
Now I want to observe the model on creation, update, etc.
I tried to use
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$itemIds = $model->item_ids;
... update another model based on the $itemIds
});
But Nova is not recognizing the static::creating function
So I created an Observer (I think a better choice) however when the observer is called it does not recognize the
auth()->user()->dbname property
Why doesn't the observer recognize auth?
This may be caused because there is no authenticated user. Try dumping and see what it throws you.
public function __construct() {
// Should throw an User model OR null.
dd(auth()->user());
// Alternatively, you could use the Logger
\Log::info(json_encode(auth()->user()));
$this->connection = auth()->user()->dbname;
}
If auth()->user() is null, then no user is logged in and as you might have guessed, null is a non-object.
Thanks for the suggestions but none would work for me. I gave up on Observers in Nova. I used the boot() function. This is how I setup milti tenant.
In the _constructor I added this
public function __construct() {
parent::__construct(); // needed before boot would fire
$this->connection = auth()->user()->dbname;
}
Then my boot() function became the observer
protected static function boot()
{
parent::boot();
static::creating(function($item) {
$item->event_id = Event::currentEventID();
});
}

how to make a new column without using migration

I need to make new columns in a table using the controller file. for doing this task I added some migration code in the controller as follows
use Illuminate\Support\Facades\Schema;
and put this inside a controller function
public function getColumn(Request $request)
{
$columnName = $request->columnName;
Schema::table('tablename', function($table)
{
$table->double(columnName)->nullable();
});
}
here I want to put a name that was I am getting from the request, I unable to put the $columnName inside this code
You need to pass the columnName Variable to the function.
Probably something like this:
public function getColumn(Request $request)
{
$columnName = $request->columnName;
Schema::table('tablename', function($table) use($columnName)
{
$table->double($columnName)->nullable();
});
}

Laravel Model Binding

I have issue with model binding in routes and controller. Here is my routes:
Route::group(['prefix'=>'services/devops/domain-names'], function () {
Route::group(['middleware'=>'auth'], function () {
Route::get('/editAutoRenew/{domainname}', 'EnomController#editAutoRenew');
});
Route::post('/', 'EnomController#checkDomainName');
});
And here is function
public function editAutoRenew(DomainName $domainname)
{
dd($domainname);
}
But this gives me empty model. Why? How can I get my model?
I tried to route:list and there it shows {domainname}
You get in argument integer type, not object. Try to rewrite your function with:
public function editAutoRenew($domainname)
{
$domainnameObj = DomainName::findOrFail($domainname);
}
When you use:
DomainName::first();
You get all entries from your table domain_name, then command first() - get first of this records.

Prevent using same query twice, once in each view composer

I have two View composers in my AppServiceProvider class, below:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer('left', function ($view)
{
if (Auth::check())
{
$user = Auth::user();
// Gets a list of the people the user is following
$usersFollowing = Following::where('user_id', $user->id)
->get();
// More queries here
View::share('usersFollowing', $usersFollowing);
}
});
View::composer('right', function ($view)
{
if (Auth::check())
{
$user = Auth::user();
// Gets a list of the people the user is following
$usersFollowing = Following::where('user_id', $user->id)
->get();
// More queries here
View::share('usersFollowing', $usersFollowing);
}
});
}
}
As you can see, both composers request the same query data ($usersFollowing). Both of these layouts (left.blade.php and right.blade.php) are called on all of my pages (by including them in the base layout).
The problem with this is that the page is requesting $usersFollowing twice on a single page load. It's calling the query once for left.blade.php and once for right.blade.php.
I'm also calling Auth::user() twice, once in each composer.
How can I prevent these queries from being called twice for the same request, and only call it once?
I think it is simple to move your queries to top of your method and use them inside both View composers. This way your query would only run once.
Here is my proposed way of doing this;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$user = Auth::user();
// Gets a list of the people the user is following
$usersFollowing = Following::where('user_id', $user->id)
->get();
// You can use `use` keyword to access external variables inside callback function.
//Both of these variables will be accessible inside callback
View::composer('left', function ($view) use ($usersFollowing,$user)
{
if (Auth::check())
{
// More queries here
View::share('usersFollowing', $usersFollowing);
}
});
View::composer('right', function ($view) use ($usersFollowing,$user)
{
if (Auth::check())
{
// More queries here
View::share('usersFollowing', $usersFollowing);
}
});
}
}
I hope this can be helpful and you can generalize this method to any other situations where you need this kind of functionality.

Resources