I have Model Model1 where I want to update a particular column rank on saved.
protected static function boot()
{
parent::boot();
static::saved(function ($model) {
$service = new Service();
$service->updateRank($model->id);
});
}
I put a log inside the saved. but it is not called
You should use method booted.
protected static function booted()
{
static::saved(function ($model) {
$service = new Service();
$service->updateRank($model->id);
});
}
Related
I've tried using event handler for delete the relation but it can't work for delete relation of relation. Event hasMany Person and Person hasMany File
class Event
public static function boot() {
parent::boot();
static::deleting(function($model) { // before delete() method call this
$model->person()->delete();
});
}
}
class Person
public static function boot() {
parent::boot();
static::deleting(function($model) { // before delete() method call this
$model->files()->delete();
});
}
}
So when I delete event, it can delete person but it doesn't delete the files row. What should I do?
You should call it one by one to call boot method on each model instance. So, get the instances and then delete them one by one.
class Event
public static function boot() {
parent::boot();
static::deleting(function($model) { // before delete() method call this
foreach($model->person as $person){
$person->delete();
}
});
}
}
class Person
public static function boot() {
parent::boot();
static::deleting(function($model) { // before delete() method call this
foreach($model->files as $file){
$file->delete();
}
});
}
}
I am using in Pivot model. In previously i assigned user_id is routekey but now i want to match model binding flats (flat_id) also. I have attached the output screenshot too.
My url is http://127.0.0.1:8000/admin/apartments/1/flats/2/flat-members/3
namespace App\pivotes;
use Illuminate\Database\Eloquent\Relations\Pivot;
use App\models\RoleUser;
use App\models\Flat;
use App\models\Association;
use App\models\Role;
use App\User;
class FlatUser extends Pivot
{
protected $table = 'flat_user';
public $timestamps = false;
public function getRouteKeyName()
{
return 'user_id';
}
public function flat()
{
return $this->belongsTo(Flat::class, 'flat_id', 'id');
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function role()
{
return $this->belongsTo(Role::class, 'role_id', 'id');
}
}
Update I tired this
In RouteServiceProvider
public function boot()
{
parent::boot();
Route::bind('flat_member', function ($value) {
dd(\App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first());
return \App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}
for dd() I am getting correct data, but if I am returning it, it seems to be becoming empty.
I am using RouteServiceProvider.php. I check flat_id and route parameter flats whether it matches, giving particular pivot model data. In below added my codes.
public function boot()
{
parent::boot();
Route::bind('flat_member', function ($value) {
return \App\pivotes\FlatUser::where('flat_id', request()->route()->parameter('flat'))->where('user_id', request()->route()->parameter('flat_member'))->first() ?? abort(404);
});
}
I am trying to change a value on save
public static function boot()
{
static::saving(function ($formRow) {
$formRow->sales = 1;
});
}
But the weird part is that its not changing the sales to 1, any idea why ?
You're missing the call to the parent's boot method:
public static function boot()
{
// add this
parent::boot();
static::saving(function ($formRow) {
$formRow->sales = 1;
});
}
You can just override the save method on the model itself:
public function save(array $options = [])
I am reading up on laravel softdelete and restore cascade here: Laravel 5: cascade soft delete
where a user said:
You should use Eloquent events for this.
Offers::deleted(function($offer) {
$offer->services()->delete();
});
Offers::restored(function($offer) {
$offer->services()->withTrashed()->restore();
});
He did not mention where to place this code, I am interested in listening for eloquent deleted and restored events. Where can I put this code? Can I listen for it in the model class? If not where to place it?
I think...
<?php
class Attribute extends Model implements Transformable
{
//....
protected static function boot() {
parent::boot();
static::deleting(function($model) {
foreach ($model->attributeValue()->get() as $attributeValue) {
$attributeValue->delete();
}
});
}
Or example:
class BaseModel extends Model
{
public static function boot()
{
static::creating(function ($model) {
// blah blah
});
static::updating(function ($model) {
// bleh bleh
});
static::deleting(function ($model) {
// bluh bluh
});
parent::boot();
}
}
I would like $protected $foo to be assigned 'foo' in the static::creating event, but when I output the final object, the property is null. The event does not seem to fire:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model {
protected $foo;
public static function boot() {
parent::boot();
static::creating(function (Item $item) {
echo "successfully fired"; //this does not get echoed
$item->foo = 'foo';
});
}
}
What am I doing wrong?
Your code is working fine, I tested at my end in this way
In Model
protected $foo;
public static function boot() {
parent::boot();
//while creating/inserting item into db
static::creating(function (AccountCreation $item) {
echo "successfully fired";
$item->foo = 'fooasdfasdfad'; //assigning value
});
//once created/inserted successfully this method fired, so I tested foo
static::created(function (AccountCreation $item) {
echo "<br /> {$item->foo} ===== <br /> successfully fired created";
});
}
in Controller
AccountCreation::create(['by'=>'as','to'=>'fff','hash'=>'aasss']);
in browser, got this response
successfully fired
fooasdfasdfad =====
successfully fired created
I used this code in laravel 9:
protected static function booted()
{
static::creating(function ($model) {
$model->code = self::generateUniqueCode();
});
}