Whenever I create a "user", I have to create a line in different tables (like account).
I know that in the controller I can create the user and account like this:
$user = User::create($user_inputs);
$account = $user->account()->create($account_inputs);
$OtherTables...
Is there a way to do this in the model? Always when someone creates a user from another controller, will the lines be automatically inserted in the other tables. Or is it always necessary to indicate it in the controller every time?
You can use Laravel observer
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver
{
/**
* Handle the user "created" event.
*
* #param \App\User $user
* #return void
*/
public function creating(User $user)
{
$user->account()->create([
// your data
]);
}
}
You can use model events for this. https://laravel.com/docs/9.x/eloquent#events-using-closures
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*
* #return void
*/
protected static function booted()
{
// This code will be called every time a new user is inserted into the system
static::created(function ($user) {
$user->account()->create([ 'name' => $user->name ])
});
}
}
There are few more events you can use within booted method, the name tells clearly what they do.
creating
created
updating
updated
saving
saved
deleting
deleted
Related
i've registered an observer on my Client model.
Why is $client->id null? Shouldn't the freshly created model ID be available to read?
What can i do to retrieve the last created Client ID?
ID is set as fillable in model Client, so i don't understand what the problem might be. Thanks.
<?php
namespace App\Observers;
use App\Models\Client;
use App\Models\SampleModel;
class ClientObserver
{
/**
* Handle the Client "created" event.
*
* #param \App\Models\Client $client
* #return void
*/
public function created(Client $client)
{
$model_to_create = new SampleModel();
$model_to_create->id_client = $client->id;
//other stuff to save in model
$model->save();
}
You should have created the Observer without reference to your model, if the laravel does not know which model is, the $client always gonna return null.
I am using Laravel 6 with Voyager admin panel.
I have two different Laravel models and I am deleting their items from Voyager admin panel, deletion works well for items of both models.
I want to add some actions on model deleting so I add this code to both models.
It works for model A but not working for model B, I don't know why and how can I debug and fix it.
public static function boot() {
parent::boot();
static::deleted(function($model) {
someaction();
});
}
The only two differences between models I found
Model B has cascading deletion in its DB migration
Model B extends \TCG\Voyager\Models\User
you have to create a event bind class:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Item;
use Log;
class ItemEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct()
{
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function itemDeleted(Item $item)
{
Log::info("Item Deleted Event Fire: ".$item);
}
}
register on event provider:
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\Event' => [
'App\Listeners\EventListener',
],
'item.deleted' => [
'App\Events\ItemEvent#itemDeleted',
]
];
/**
* Register any events for your application.
*
* #return void
*/
public function boot()
{
parent::boot();
}
}
and you could test it firing on booting model like so:
class Item extends Model
{
protected $fillable = ['name', 'price'];
public static function boot() {
parent::boot();
static::deleted(function($item) {
\Log::info('Item Deleted Event:'.$item);
});
static::deleting(function($item) {
\Log::info('Item Deleting Event:'.$item);
});
}
}
You mentioned Model B has cascade delete on migration. This is most likely the cause. The delete event won't be fired because the delete happens on database level and not through Laravel. If you still need the events for this, you could handle the delete through the app instead of cascade delete. Another approach would be to watch the delete event on foreign key model, and then handle your function. Here's a related question that might help.
To further confirm if cascade delete is the cause, try manually deleting something from your Model B through the app (ModelB::find(1)->delete()).
Apart from that, if you're deleting records through query (ModelB::where('something', 1)->delete()), it won't be fired either.
I would suggest using laravel's observers in order to trigger certain code on events including creation, update, deletion...
you can see the reference here: https://laravel.com/docs/8.x/eloquent#observers
after creating the observer you add this code to your model:
public static function boot()
{
parent::boot();
MyModel::observe(MyModelObserver::class);
}
I found similar title and similar asked question on this website when I was researching to solve the problem. But none of posted answers helped me. This question might be duplicated but I could not solve the problem using existing questions on StackOverflow.
I'm trying to prevent access to users who are not logged in OR who are not member of "School" model!
In "web.php" file I used "middleware("auth")" to prevent access to users who are not logged in.
Now I created a "Policy" named "SchoolPolicy" to prevent access to users who are not member of "Schools" database/model.
When I call "view" method from SchoolPolicy, it prevents access for all authorized and unauthorized users!
I also checked and I realized "School" model returns "null" when I try to catch "user_id" foreign key from "schools" table.
The below piece of code is the way I created "Schools" table using Migration:
Schema::create('schools', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('school_name');
$table->string('school_address')->nullable();
$table->string('school_email');
$table->string('school_phone')->nullable();
$table->string('url');
$table->longText('descriptions')->nullable();
$table->timestamps();
});
This is the route to view any school which is created by any user (URL can be dynamic)
Route::group(['middleware' => 'auth'], function () {
Route::get('/schools/{url}', [ViewSchool::class, 'index'])->name('yourschool.show');
});
And this is "School" model. I used php artisan make:model School command to create this model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Auth;
class School extends Model{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'user_id',
'school_name',
'school_address',
'school_email',
'school_phone',
'url',
'descriptions'
];
}
In this section I created School Policy. However I used Laravel 8 but I also registered created Policy manually
SchoolPolicy
<?php
namespace App\Policies;
use App\Models\School;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class SchoolPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any models.
*
* #param \App\Models\User $user
* #return mixed
*/
public function viewAny(User $user)
{
//
}
/**
* Determine whether the user can view the model.
*
* #param \App\Models\User $user
* #param \App\Models\School $school
* #return mixed
*/
public function view(User $user, School $school)
{
return $user->id == $school->user_id;
}
}
In AuthServiceProvider.php I registered SchoolPolicy like this:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use App\Models\User;
use App\Models\School;
use App\Policies\SchoolPolicy;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
School::class => SchoolPolicy::class
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
}
}
"ViewSchool.php" file where I want to use authorize method:
<?php
namespace App\Http\Controllers\Schools;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\School;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
class ViewSchool extends Controller
{
public function index (School $school) {
$this->authorize('view', $school);
return view('layouts.viewschool');
}
}
I tried many ways to solve the problem, but none of them properly worked:
First Try:
public function index (School $school) {
$this->authorize('view', $school);
}
Second Try:
public function index () {
$this->authorize('view', School::class);
}
I even tried to print any output from School model but I receive "null":
public function index (School $school) {
dd($school->user_id);
}
I followed all tutorials on YouTube and official Laravel website, but in my examples I gave you, authorization doesn't work properly.
Please help me to solve this problem.
Thank you
I have a products table that has an serial id, a sku, an is_current flag, and then various attributes about the item. It also has a unique index on sku where is_current = true to prevent having more than one current record.
What I want to happen is, when you change the model and call save(), a new record is created instead and the existing record is only changed to flip the is_current flag. So what I think I want is a way to copy the model with replicate(), discard the changes in the old model and just update to flip the current flag false, and then insert the new model. Something like that, although I might not have thought that though 100%. Can this be done in as a part of an "updating" event? Is a trigger a better choice to prevent accidents if some direct table updates occur?
You can do this 2 ways, One is hooking into the save event on the model. The other is creating a new method on the model, I would probably go the new method route. like $product->updateCurrent(); which would then do all of the logic.
public function updateCurrent($details)
{
$newProduct = $this->replicate()->fill($details);
$this->update([
'is_current' => false,
]);
return $newProduct;
}
Otherwise the saving event
product model
protected $dispatchesEvents = [
'saving' => \App\Events\ProductSaving::class,
];
ProductSaving event
<?php
namespace App\Events;
use App\Product;
use Illuminate\Queue\SerializesModels;
class ProductSaving
{
use SerializesModels;
public $product;
/**
* Create a new event instance.
*
* #param \App\Product $product
*/
public function __construct(Product $product)
{
$this->product = $product;
}
}
Now you need a listener
ProductSaving
<?php
namespace App\Listeners;
use App\Events\ProductSaving as ProductSavingEvent;
class ProductSaving
{
/**
* Handle the event.
*
* #param \App\Events\ProductSavingEvent $event
* #return mixed
*/
public function handle(ProductSavingEvent $event)
{
$product = $event->product->replicate();
$old_product = Product::find($event->product->id);
$old_product->update([
'is_current' => false,
]);
return false; // Prevent model from saving.
}
}
Most of my db table contain create_user_id and update_user_id
How can l update this two field automatic when l use save(), update(), insert(), createOrUpdate() and etc method.
For example, l execute this script:
$model = Model::find(1);
$model->model_f = 'update';
$model->save();
then this record's model_f updated, and update_user_id updated, too.
l know eloquent can manage update_time automatic and l have use it already. But l want to do something else when update or insert or delete
PS: l have a constant named USERID to remember current user's id
You could make use of Observers.
You can hook to the following events on your Model:
retrieved
creating
created
updating
updated
saving
saved
deleting
deleted
restoring
restored
Let me give you an example where we are trying to hook into the events emitted by the App/User model. You can change this to match your particular Model later on.
To create an observer, run the following command:
php artisan make:observer UserObserver --model=User
Then you can hook to specific events in your observer.
<?php
namespace App\Observers;
use App\User;
class UserObserver
{
/**
* Handle the User "saved" event.
*
* #param \App\User $user
* #return void
*/
public function saved(User $user)
{
//
}
/**
* Handle the User "created" event.
*
* #param \App\User $user
* #return void
*/
public function created(User $user)
{
//
}
/**
* Handle the User "updated" event.
*
* #param \App\User $user
* #return void
*/
public function updated(User $user)
{
//
}
}
Since, in your particular case, you want to hook into these 3 events, you can define the events above and perform additional operations to your model when those events are called.
Don't forget to register this observer in your AppServiceProvider.
<?php
namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
User::observe(UserObserver::class);
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
//
}
}
There is pretty simple way to automatically update the create_user_id and update_user_id
Step1:
Open you app folder and create the new file named as UserStampsTrait.php
Step:2
and paste the following code
<?php
namespace App;
use Illuminate\Support\Facades\Auth;
trait UserStampsTrait
{
public static function boot()
{
parent::boot();
// first we tell the model what to do on a creating event
static::creating(function($modelName='')
{
$createdByColumnName = 'create_user_id ';
$modelName->$createdByColumnName = Auth::id();
});
// // then we tell the model what to do on an updating event
static::updating(function($modelName='')
{
$updatedByColumnName = 'update_user_id';
$modelName->$updatedByColumnName = Auth::id();
});
}
}
Thats it
Step:3
Open you model which needs to updated the corresponding models automatically
for Example it may be Post
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\UserStampsTrait;
class Post extends Model
{
use UserStampsTrait;
}
Thats it