Twill: Use admin layout for non twill module models

Created on 7 Dec 2018  路  4Comments  路  Source: area17/twill

Hi,
I have a model which is not a twill module for some reasons, is it possible to use admin layout so that I can list some items, or in general add any content there?

Thanks.

question

Most helpful comment

Hi @manuelsofia!

Would you like to list and edit your existing User model like any other Twill module? In that case you should be able to create a UserController and UserRepository that connect to your existing model, as well as a form.blade.php view for it. Then, adding a route for it in the admin.php and a navigation entry in twill-navigation.php should do the trick! You will also want to grab some of the code from Twill's parent model to get everything to work correctly on your existing model that doesn't extend it. I'm thinking that extending Twill's parent model should not actually be a requirement and that we could provide those features through a trait, like HasTwillModule, something like that.

Hope that helps!

All 4 comments

Hi @gogabidzia,

Definitely! The base model that is extended by generated Twill's models is really just there to share the same traits/scopes across generated models, which means you can perfectly add those traits and scopes to your own models to be able to list/edit those through Twill's UI, or even decide to extend your existing models using the Twill's base model. As long as you have a controller and repository for that model, defined like a generated module would look, and of course the routes registered, everything should work. A quick and dirty way to figure this out for you could be to generate a Twill module using the name of your existing model, and then revert the changes it applied to your existing model file (that's if your model is under app\Models of course, otherwise you will need to update the injected reference to it in the repository as well).

Exemple custom model:

<?php

namespace App\Models;

use Cartalyst\Tags\TaggableInterface;
use Cartalyst\Tags\TaggableTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CustomModel extends Model implements TaggableInterface
{
    use SoftDeletes, TaggableTrait;

    protected $fillable = [
        'published',
        'title',
        'description',
    ];

    public $checkboxes = [
        'published',
    ];

    public function scopePublished($query)
    {
        return $query->wherePublished(true);
    }

    public function scopeDraft($query)
    {
        return $query->wherePublished(false);
    }
}

Then, to support Twill's features on that model (like slugs or medias attachments), you can add the corresponding traits and tables.

Feel free to share a repo, I'd be happy to help.

Hi @ifox, can you please assist me so I can add the default Laravel User model to the Twill admin layout? What would be the best way to achieve this? Thanks!

Hi @manuelsofia!

Would you like to list and edit your existing User model like any other Twill module? In that case you should be able to create a UserController and UserRepository that connect to your existing model, as well as a form.blade.php view for it. Then, adding a route for it in the admin.php and a navigation entry in twill-navigation.php should do the trick! You will also want to grab some of the code from Twill's parent model to get everything to work correctly on your existing model that doesn't extend it. I'm thinking that extending Twill's parent model should not actually be a requirement and that we could provide those features through a trait, like HasTwillModule, something like that.

Hope that helps!

Thank you so much @ifox! I'll try that out :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lostdesign picture lostdesign  路  5Comments

newvladimirov picture newvladimirov  路  5Comments

manuelsofia picture manuelsofia  路  4Comments

stevanpavlovic picture stevanpavlovic  路  7Comments

jayhaluska picture jayhaluska  路  4Comments