Sometimes I do not need the standard CRUD

Posted In Voyager
  • jorge-aguirre 2 years ago
    I got this problems with two kind of "special crud":
    
    1. When I need just one create/edit view to store in two tables, like when I create a new contact I need to store its adress and phone in other tables.
    
    2. I am using a map api because I need to know the ubication and draw a perimeter of farms, how can I edit the create/edit view so it can have this map?
    
    So I try to use custom controllers like the documentation says so I change the controllers namespace:
    ['controllers' => [
        'namespace' => 'App\\Http\\Controllers\\Voyager',
    ],](http://)
    
    and run php artisan voyager:controllers
    
    then I just copy all the controllers that were in vendor/tcg/voyager/src/http/controllers into my app controllers folders.
    
    that is working but when I create a new table with my own controllers and models I can not give or take away permission for the roles, so every single role can do everything with that table.
    
    How can I do that? and if I need the basic Crud for a new table, would still working just the way it was working?

    I got this problems with two kind of "special crud":

    1. When I need just one create/edit view to store in two tables, like when I create a new contact I need to store its adress and phone in other tables.

    2. I am using a map api because I need to know the ubication and draw a perimeter of farms, how can I edit the create/edit view so it can have this map?

    So I try to use custom controllers like the documentation says so I change the controllers namespace: 'controllers' => [ 'namespace' => 'App\Http\Controllers\Voyager', ],

    and run php artisan voyager:controllers

    then I just copy all the controllers that were in vendor/tcg/voyager/src/http/controllers into my app controllers folders.

    that is working but when I create a new table with my own controllers and models I can not give or take away permission for the roles, so every single role can do everything with that table.

    How can I do that? and if I need the basic Crud for a new table, would still working just the way it was working?

  • devdojo 2 years ago
    Hey @Jorge-aguirre,
    
    You can create your own views as well. Perhaps this might be what you need to do to add this functionality into your application. If your data type is called `contacts` you can create a new view located at `resources/views/vendor/voyager/contacts/edit-add.blade.php`, this will override the current edit/add functionality for the contacts. Or if you added a new file called `browse.blade.php` that would override the current table browsing view.
    
    You can always copy over the logic from `/vendor/tcg/voyager/resources/views/bread/edit-add.blade.php` or the `browse.blade.php`
    
    You will then have to change that view to only show/hide certain parts for specific user roles.
    
    Hope that helps or at least sends you in the right direction. Perhaps someone else may have more ideas on how to accomplish this :)
    
    Thanks. talk to you soon.

    Hey @Jorge-aguirre,

    You can create your own views as well. Perhaps this might be what you need to do to add this functionality into your application. If your data type is called contacts you can create a new view located at resources/views/vendor/voyager/contacts/edit-add.blade.php, this will override the current edit/add functionality for the contacts. Or if you added a new file called browse.blade.php that would override the current table browsing view.

    You can always copy over the logic from /vendor/tcg/voyager/resources/views/bread/edit-add.blade.php or the browse.blade.php

    You will then have to change that view to only show/hide certain parts for specific user roles.

    Hope that helps or at least sends you in the right direction. Perhaps someone else may have more ideas on how to accomplish this :)

    Thanks. talk to you soon.

  • jorge-aguirre 2 years ago
    @Devdojo, thanks for the answer, but when I create a custom controller so I can create my own methods to store in two tables then I have the problems with the permissions.
    
    rigth now, I already know to create a new view and a new controller with my own methods and all I need, but all of that doesn't work for me if then when I want to edit a role it does not matter if it have permissions in contacts or not (checkboxes of contacts checke or not), that role always have access to contacts

    @Devdojo, thanks for the answer, but when I create a custom controller so I can create my own methods to store in two tables then I have the problems with the permissions.

    rigth now, I already know to create a new view and a new controller with my own methods and all I need, but all of that doesn't work for me if then when I want to edit a role it does not matter if it have permissions in contacts or not (checkboxes of contacts checke or not), that role always have access to contacts

  • devdojo 2 years ago
    @Jorge,
    
    Do you have some code you can share with me. Additionally a few screenshots might help as well :)
    
    Thanks. I'm sure theres a way to do it.

    @Jorge,

    Do you have some code you can share with me. Additionally a few screenshots might help as well :)

    Thanks. I'm sure theres a way to do it.

  • jorge-aguirre 2 years ago
    @devdojo ok the code rigth now it is just a text where I only redirect to a page that says hello, this is how I replace the route:
    
    [Route::group(['prefix' => 'admin'], function () {
        Voyager::routes();
    });
    
    Route::get('/admin/pruebas', '[email protected]');](http://)
    
    this is the controller:
    
    [<?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    class PruebaController extends Controller
    {
      public function prueba(){
        return view("prueba");
      }
    }](http://)
    
    I would love to share screenshots but I do not know how, however I will try to explain:
    
    1. when I added the bread I only change the area of controller and write my own onw.
    2. Then i edit admin role and left all the permissions in pruebas unchecked.
    3. By now I shouldn't be able to enter to /admin/pruebas, but still let me in.
    4. I already run php artisan voyager:controllers and modify the controllers namespace in config/voyager so basically now all the controllers are in app/http/controllers including the one of voyager.
    
    Really thank you for the support, thank to you and other users in git, i have been able to make some progress but I just can't apply all this in my production app until I know how can I take away permission in custom controller.
    
    (sorry for not knowing how to share images if they are in my local machine)
     
    
    

    @devdojo ok the code rigth now it is just a text where I only redirect to a page that says hello, this is how I replace the route:

    [Route::group(['prefix' => 'admin'], function () { Voyager::routes(); });

    Route::get('/admin/pruebas', '[email protected]');](http://)

    this is the controller:

    [<?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    class PruebaController extends Controller { public function prueba(){ return view("prueba"); } }](http://)

    I would love to share screenshots but I do not know how, however I will try to explain:

    1. when I added the bread I only change the area of controller and write my own onw.
    2. Then i edit admin role and left all the permissions in pruebas unchecked.
    3. By now I shouldn't be able to enter to /admin/pruebas, but still let me in.
    4. I already run php artisan voyager:controllers and modify the controllers namespace in config/voyager so basically now all the controllers are in app/http/controllers including the one of voyager.

    Really thank you for the support, thank to you and other users in git, i have been able to make some progress but I just can't apply all this in my production app until I know how can I take away permission in custom controller.

    (sorry for not knowing how to share images if they are in my local machine)

  • devdojo 2 years ago
    Hey Jorge,
    
    You can include screenshots by adding images using markdown: [https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images)
    
    Additionally, when you are loading the custom view, you can set conditionals around any section you want by doing the following:
    
    ```
    if( Auth::user->hasRole('admin') ){
        // allow user access
    }
    ```
    
    You will then need to include `use VoyagerUser` trait in your App\User model like so:
    
    ```
    <?php
    
    namespace App;
    
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use TCG\Voyager\Traits\VoyagerUser;
    
    class User extends Authenticatable
    {
        use Notifiable;
        use VoyagerUser;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password',
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
    }
    
    ```
    
    > Don't forget to include the namespace above the class: `use TCG\Voyager\Traits\VoyagerUser;` 
    
    You can choose to show or hide any section based on the users role :)
    
    Let me know if that helps.
    
    Thanks!

    Hey Jorge,

    You can include screenshots by adding images using markdown: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images

    Additionally, when you are loading the custom view, you can set conditionals around any section you want by doing the following:

    if( Auth::user->hasRole('admin') ){
        // allow user access
    }
    

    You will then need to include use VoyagerUser trait in your App\User model like so:

    <?php
    
    namespace App;
    
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use TCG\Voyager\Traits\VoyagerUser;
    
    class User extends Authenticatable
    {
        use Notifiable;
        use VoyagerUser;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password',
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
    }
    
    

    Don't forget to include the namespace above the class: use TCG\Voyager\Traits\VoyagerUser;

    You can choose to show or hide any section based on the users role :)

    Let me know if that helps.

    Thanks!

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
Want to create your own SAAS?

I can teach you to create your own Software as a Service. Click here to learn more!

Visit SAAS Adventure!