I'm new in laravel Backpack. In my note entity i want store current user notebook id and uniqid.
I don't know how to include in laravel backpack crud.
Before i used this store function in my controller:
public function store(TodoCreateRequest $request)
{
$uid = uniqid(true);
$todolist = $request->user()->todolist()->first();
$todo= $todolist->todos()->create([
'uid' => $uid,
'todo' => $request->todo,
'status'=> $request->status,
]);
if ($request->ajax()) {
return response()->json(null, 200);
}
return redirect()->back();
}
Hi @squierll ,
You can do things before or after an entry is created/updated using:
A) The controller's store() and update() methods (quick, but will only works in the admin panel);
B) Eloquent Observers to listen on the events (creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored) - will also work when you create/update/etc entries from the front-end;
C) A mutator - will also work in the front-end, but it's more hacky;
D) By overwriting the model's save method - will also work in the front-end;
Hope it helps. Cheers!
@tabacitu I'm starting to think maybe more and more to add an abstract function into the store/update methods which users can extend like callbacks for simplicity.
Especially as the methods extend and evolve over time it makes it harder to maintain. So suggesting people to copy the function out and overwrite them it means they might miss out or have clashes with future features??
Or am i missing something haha
For any kind of "log current user ID to table row" operations, you might consider automating it on your model. Here's an implementation I did:
class Product extends Model
{
public function __construct(array $attributes = [])
{
$this->creating([$this, 'onCreating']);
parent::__construct($attributes);
}
public function onCreating(\App\Models\Product $row)
{
// Placeholder for catching any exceptions
if (!\Auth::user()->id) {
return false;
}
$row->setAttribute('created_by_user_id', \Auth::user()->id);
}
Above, the creating method allows you to hook into your own custom event (onCreating in this case) when a row is being created for the first time.
This way you aren't extending the function from the real model.
+1 for adding an abstract method to make it easier!
@OwenMelbz I'm open to a more abstract approach. What do you have in mind?
I'm literally thinking of something as simple as
public function store(Request $request){
$this->beforeStore($request);
//all other code here
$this->afterStore($request, $entity);
}
basically this PR https://github.com/Laravel-Backpack/CRUD/pull/283
As I'm consolidating things, I'm going to close this up as there is another list and a PR for it.
You can see the list https://github.com/Laravel-Backpack/CRUD/issues/285
Most helpful comment
For any kind of "log current user ID to table row" operations, you might consider automating it on your model. Here's an implementation I did:
Above, the
creatingmethod allows you to hook into your own custom event (onCreatingin this case) when a row is being created for the first time.This way you aren't extending the function from the real model.
+1 for adding an abstract method to make it easier!