0

I have to pass the secret author_id of the user when he edit for example an Article and memorize that into the Database in Backpack-Laravel. How can do that?

I be able to do just this, the value appears in the $request array (I use dd($request) for know that) but isn't stored on the database.

AuthorCrudController.php

public function update(UpdateArticleRequest $request)
{
    //dd($request); <-- author_id = Auth::id()
    return parent::updateCrud();
}

UpdateArticleRequest.php

public function rules()
{

    $this->request->add(['author_id'=> Auth::id()]);
    return [
        'title' => 'required|min:5|max:255',
        'author_id' => 'numeric'
    ];
}
Cinzia Nicoletti
  • 169
  • 1
  • 14

2 Answers2

3

99 times out of 100, when the value isn't stored it's because that column hasn't been mentioned in your model's $fillable property. Is this it?


Sidenote: Adding the author_id like this works, but if you're using this approach for multiple models, I recommend coding it once for all your models. I use a trait for this. That way, any time an entry is created, the author is saved and you have the have all the methods for getting it in one place, the trait ($this->creator(), this->updator).

My approach to this is this:

1) I have two new columns in my database created_by and updated_by

2) I use a trait like this:

<?php namespace App\Models\Traits;

use Illuminate\Database\Eloquent\Model;

trait CreatedByTrait {

    /**
     * Stores the user id at each create & update.
     */
    public function save(array $options = [])
    {

        if (\Auth::check())
        {
            if (!isset($this->created_by) || $this->created_by=='') {
                $this->created_by = \Auth::user()->id;
            }

            $this->updated_by = \Auth::user()->id;
        }

        parent::save();
    }


    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    public function creator()
    {
        return $this->belongsTo('App\User', 'created_by');
    }

    public function updator()
    {
        return $this->belongsTo('App\User', 'updated_by');
    }
}

3) Whenever I want a model to have this feature, i just need to:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Car extends Model
{
    use CrudTrait;
    use CreatedByTrait; // <---- add this line

Hope it helps.

tabacitu
  • 6,047
  • 1
  • 23
  • 37
  • No unfortunately, the '$fillable' property is present as well as the 'use CrudTrait;' that I had previously forgotten :/ I do not know why I can not update or store that parameter. – Cinzia Nicoletti Oct 21 '16 at 10:21
  • Hmm... Then maybe try adding it in the AuthorCrudController::update() method, not in the FormRequest? – tabacitu Oct 21 '16 at 11:22
  • Or you could use Backpack for this, and add a hidden field with that value as default. – tabacitu Oct 21 '16 at 11:23
  • I tried to do this: (but don't work) ` public function update(UpdateRequest $request) { $this->setAuthorId($request); return parent::updateCrud(); } protected function setAuthorId(Request $request) { $request->add(['author_id'=> "1"]); }` – Cinzia Nicoletti Oct 21 '16 at 14:08
  • A hidden field should work fine: https://laravel-backpack.readme.io/docs/crud-fields#hidden – tabacitu Oct 21 '16 at 14:10
  • Yes it 's true, but I don't want that anyone can changing this parameter. – Cinzia Nicoletti Oct 21 '16 at 14:13
  • Then I guess the best way to proceed would be to create a mutator in you model, like I suggested above. – tabacitu Oct 21 '16 at 14:32
0

The update function in my backpack setup has $request passed in updateCrud function. The one that you have mentioned does not have the request passed into the parent function.

public function update(UpdateRequest $request)
{
    // your additional operations before save here
    $redirect_location = parent::updateCrud($request);
    return $redirect_location;
}