How to create custom controller in Laravel Voyager
Asked Answered
K

4

9

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 ?

Keirakeiser answered 12/2, 2018 at 10:3 Comment(6)
Good day! Did you check this link? About custom controller fo Voyager?Rocketeer
@ D. Erashkin ... NoKeirakeiser
@D.Erashkin... It doesn't work. Controller still doesn't create inside App\Http\VoyagerKeirakeiser
It wont create BREAD Controller automatically. When you create/edit BREAD for your model you can set Controller Name.Rocketeer
@D.Erashkin... Then can't I create custom controller in Voyager?Keirakeiser
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 youRocketeer
G
14

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

Gastrotomy answered 13/2, 2018 at 4:18 Comment(2)
@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` ?Keirakeiser
@Keirakeiser - It's on the bread editor in the front end. localhost/admin/database/schools/bread/editGastrotomy
A
3

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

Agenda answered 16/8, 2018 at 1:53 Comment(1)
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)Fall
O
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');
        });
}
Oviform answered 31/1, 2019 at 4:12 Comment(0)
P
0

Try this :

  • composer dump-autoload
  • php artisan cache:clear
  • php artisan config:clear
  • php artisan view:clear
  • php artisan route:cache
Permute answered 22/8, 2021 at 9:1 Comment(1)
Please take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites.Sackett

© 2022 - 2024 — McMap. All rights reserved.