Laravel Voyager overwrite BREAD controller

Pedro picture Pedro · Jun 15, 2017 · Viewed 8.2k times · Source

I've started to use Voyager and I've problems with a controller.

I've create a new table in database called Progress, voyager by default create a BREAD form to browse read delete and add items .

I like to put a default value into a field when the user go to add a new item. The default value that I like to put is authentication user_id

How can I do that?

Thanks.

Answer

Sam picture Sam · Aug 31, 2017

You can do that completely outside of Voyager.

First exclude the authentication_user_id from the Add form (in Voyager's database interface). If the field doesn't take a null value you can set some temporary default, or modify your migrations - whichever is most convenient.

Next create a model observer and then utilise the created() function. For example:

<?php
    namespace App\Observers;

    use App\Models\Project;

    class ProgressObserver
    {
        /**
         * Listen to the Progress created event.
         *
         * @param  Progress  $progress
         * @return void
         */
         public function created(Progress $progress)
         {
            $progress->authentication_user_id = WHATEVER_YOU_DO_HERE;
            $progress->save();
         }

    }