TC
Twill CMSundersound

Fetch all models with field value of X

Sorry to ask such a basic question but I want to do the following.... I have a model called Work (and a WorkController and WorkRepository). On my Work module there is a field called "available" of type checkbox. Now i want to fetch all models with "available" selected. 1. What is best practice to implement a e.g. getAllAvailable() method? Is that on the WorkRepository, WorkController of Work model class? 2. How do I select all the models with that checkbox selected. In the laravel docs I saw this $flights = Flight::where('destination', 'Paris')->get(); but I can't seem to use the where function on my model. E.g. Work::where is not working. Thanks for any guidance
A
agnonym372d ago
Hi @undersound, a good practice is to create a scope on the model (Twill already use this feature to get published models) : https://laravel.com/docs/10.x/eloquent#local-scopes For example, on your Work class, if you available attribute is a bool:
public function scopeAvailable(Builder $query, bool $available = true): void
{
$query->where('available', $available);
}
public function scopeAvailable(Builder $query, bool $available = true): void
{
$query->where('available', $available);
}
Then, you can get the available models like that:
$workList = Work::available()->get();
$workList = Work::available()->get();
For this 'available' need, you could also use the native 'published' feature of Twill
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
U
undersound372d ago
Thanks @agnonym , is this also applicable to laravel 9.x?
A
agnonym372d ago
Since at least Laravel 4 🙂
U
undersound372d ago
Is the published feature of Twill you mention to indicate if a model instance is published? Because the available field on my model does not represent this state of the model instance. It is more a value to indicate if a piece of work is availble for sale Sorry real beginner here with Laravel 🙂
A
agnonym372d ago
Yep, just wanted to tell you it could be the same feature as you want to achieve with available.
U
undersound372d ago
y eah I thought that, appreciated @agnonym that works like a charm, thank you very much! Out of curiousity... how would I implement the same functionality but than with the get function https://github.com/area17/twill/blob/3.x/src/Repositories/ModuleRepository.php#L55. Would I want to use the filter parameter there?
A
agnonym372d ago
Not sure to understand what you want, is that for admin filtering like that:
No description
A
agnonym372d ago
or is that for our frontend to be used in a controller ?
U
undersound372d ago
As far as I understand the WorkRepository class gets created when creating a module through php artisan twill:make:module . So in my case WorkRepository got created and extends ModuleRepository. I am using this class WorkRepository in my WorkController to fetch Work models for my frontend Route::get('/work', [WorkController::class , 'index']); I think this WorkRepository class is also used by the admin views but I am not sure. Anyways, in this case I want to use it for filtering in the frontend.
A
agnonym372d ago
ok. Twill is headless CMS, all the files created when you create a module if for admin functionalities (Admin Controller, Repository, Request, ...), but it doesn't mean you can't use it for your frontend Personally, I don't use the Repository for my frontend as if there are breaking changes in Twill package, I don't want to fix all my frontend So, on my frontend controller I usually fetch models like that $workList = Work::with(potential relations like medias, blocks, slugs, ...)->publishedInListings()->get() or ->paginate(x) the publishedInListings is a scope that filter published models and checks if you have publication dates (start / end) that the model is visible But I think you can use the get method of the ModuleRepository, something like that: app(WorkRepository::class)->get([relations], [scopes like 'publishedInListings', 'available'], [orders], 100) If you want all your available and published models order by name attribute, and you need slugs to redirect to a Work page and your model has translations, medias and blocks, it should be (not tested) app(WorkRepository::class)->get(['slugs', 'translations', 'medias', 'blocks'], ['publishedInListings', 'available'], [['name' => 'asc']], -1);
U
undersound371d ago
Thanks @agnonym , that's helpful and nice to see other approaches for getting content.
Want results from more Discord servers?
Add your server
More Posts
Displaying Block elements on the Front Pagehow do you display block elements on the frontpage? And i don't mean the preview file. I want to accdashboard routeHello. I have a problem with dashboard config I have a Category and CategoryArticle Models I have aTwill Role based accessHow would i go about adding a Role based access to CMS Components and its entries. For e.g in an imaTwill assets errorHello to everyone i have an website in production builded in twill but some twill assets in there aWhere to find block repositories?I've seen in the tutorial(https://www.youtube.com/watch?v=kAcJ5G2GhiA) some blocks like "paragraph &[3.0.0] Block class - getBlockTitle()Hi, I was trying to move Blade blocks to Class. When I wanted to move `@twillBlockTitleField` in cla[3.0.0] componentBlock overriding getBlockIcon doesn't seem to work?(Successfully changed the title by overriding it's static function) Checked twill:list:icon for the[3.0.0] Maintaining automated Permalink Generation when using getCreateForm to add fields.As soon as I add any fields, using the Formbuilder on this function, the automated permalink disappemulti select selected optionsI'm using the multi select as documented in https://twill.io/docs/form-fields/multi-select.html#mult[3.0.0-rc4] - How to show current value in a select form fieldCurrently I have my select field defined as: `@formField('select', [ 'name' => 'work_year', [3.0.0-rc4] Images not found on remote server - League\\Glide\\Filesystem\\FileNotFoundExceptionI just went from my local to my remote server and some images are showing up others are getting a 5[Twill2] Do settings section support media and how?As title says, is it possible to inject media here or should I make a custom settings singleton to h[3.x.dev] Call to undefined method ReflectionUnionType::getName()Newbie to Twill here. Fresh install of Laravel 10.4.1 / PHP 8.2.4 / MYSQL 8.0.32 on Ubuntu Budgie 2Add custom icons into twillI add the icons into the path `resource/views/admin/icons` and I run the command, result is ok, but Select an another block in a block?Hi everyone, I have a function to create a new block with name "Columns block". With this block, usMedia Library / Glide only loading JPEGsHey there, So I had a Laravel/Twill app running successfully on a subdomain with no issue. Today IAdding a vue componentGood afternoon Tell me what could be the problem? On the main page of the admin panel there is an aSave title in translationsI'm trying to save my title as a translatable field, normal fied works fine but when i make it transSet translations to activeWhile trying to set translations to active it isn't saved to database? Only english is selectablecan't use Published, Draft, Trash filters on module indexI see the buttons, they show the correct counts in the parenthesis, but when I click on them, I alwa