5

I am very new in Voyager.

I have got all the controllers inside TCG\\Voyager\\Http\\Controllers while installing Voyager but didn't find other controllers those I have created using BREAD.

Besides that I want to create custom controller in my Voyager admin panel inside App\\Http\\Controllers\\Voyager . I also followed the steps of Voyager tutorial in Youtube for making custom controller, but couldn't create.

Anybody help please ?

Avag Sargsyan
  • 2,149
  • 3
  • 24
  • 31
raff
  • 303
  • 2
  • 7
  • 25
  • Good day! Did you check this [link](https://voyager.readme.io/docs/using-custom-http-controllers)? About custom controller fo Voyager? – wau Feb 12 '18 at 10:08
  • @ D. Erashkin ... No – raff Feb 12 '18 at 10:17
  • @D.Erashkin... It doesn't work. Controller still doesn't create inside `App\Http\Voyager` – raff Feb 12 '18 at 10:31
  • It wont create BREAD Controller automatically. When you create/edit BREAD for your model you can set Controller Name. – wau Feb 12 '18 at 10:34
  • @D.Erashkin... Then can't I create custom controller in Voyager? – raff Feb 12 '18 at 10:35
  • I think you need to create your custom BREAD Controller and you can create it based on Voyager's BREAD controller. (when you create BREAD for some you model, Voyager doesn't generate/create new Controllers. It uses VoyagerBreadController.php). Hope it will help you – wau Feb 12 '18 at 10:42

3 Answers3

12

In your config\voyager.php file add your namespace:

'controllers' => [
    'namespace' => 'App\Http\Controllers\Back',
],

Then publish voyageres controllers to your namespace

php artisan voyager:controllers

Within that namespace create a new controller derived from VoyagerBreadController

namespace App\Http\Controllers\Back;

use Illuminate\Http\Request;

class SchoolController extends VoyagerBreadController
{

Then you can specify the controller in the bread editor.

NOTE: I did have to refer to mine as Back\SchoolController instead of just SchoolController as I would have expected.

Setting controller on backend

FloatingKiwi
  • 4,183
  • 1
  • 14
  • 37
  • @FloatingKiwi......I have created controller manually in the namespace as you suggested. But how to specify the created controller in the bread editor ? Is it from `vendor\tcg\voyager\resources\views\bread\` ? – raff Feb 13 '18 at 05:00
  • @raff - It's on the bread editor in the front end. https://localhost/admin/database/schools/bread/edit – FloatingKiwi Feb 13 '18 at 05:10
3

Update: From version 1.1 now you need to extend VoyagerBaseController instead of VoyagerBreadController.

Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
  • I have created that but when going to edit my new bread im getting Undefined variable: model_name (View: /var/www/html/myapp/vendor/tcg/voyager/resources/views/tools/bread/edit-add.blade.php) – lucasvm1980 Oct 25 '18 at 22:18
0

Add this to your model.

use Illuminate\Database\Eloquent\Builder;

protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('order', function (Builder $builder) {
            $builder->orderBy('name', 'asc');
        });
}
Stoney Eagle
  • 536
  • 4
  • 9