VarboxInternational / varbox

THE Laravel Admin Panel package built by developers, for developers
https://varbox.io
Other
63 stars 13 forks source link

Programatically get drafted items #12

Closed orosstefan closed 3 years ago

orosstefan commented 4 years ago

What are you trying to achieve (screenshots might help):

I've created a draftable custom entity. I noticed that from the admin I can save it as a draft or publish it.

However, I'm wondering how can I fetch all my drafted or published records and how can I determine if a single record instance is drafted or published

Thank you!

zbiller commented 4 years ago

Given what you've said, I'm assuming you're already using the Varbox\Traits\IsDraftable trait on your model.

You can make use of the query scopes that trait exposes in order to only fetch your drafted or published records like so:

$onlyPublishedRecords = YourModel::withoutDrafts()->get();

$onlyDraftedRecords = YourModel::onlyDrafts()->get();

Here's the documentation part for reference: https://varbox.io/docs/1.x/draft-records#query-scope


To answer your second part of the question, you can check if a record is drafted or published by using the isDrafted method:

$model = YourModel::find($id);

dump($model->isDrafted()); // true or false

Here's the documentation part for reference: https://varbox.io/docs/1.x/draft-records#check-if-draft