CRUD icon indicating copy to clipboard operation
CRUD copied to clipboard

[Feature Request] something like "orAddClause" in List Operation

Open mamprogr opened this issue 3 years ago • 1 comments

Feature Request

What's the feature you think Backpack should have?

I am using $this->crud->addClause() in List Operation but sometimes I need to add or between two clauses like: $this->crud->addClause('myBooks') $this->crud->orAddClause('myFavoriteBooks')

Have you already implemented a prototype solution, for your own project?

I created another scope myBooksOrMyFavoriteBooks

Do you see this as a core feature or an add-on?

Yes, I think it's something that is constantly needed

mamprogr avatar May 14 '22 08:05 mamprogr

I love it! Thanks @mamprogr - I've tagged it for 5.2 so we'll take another look at how we can do this in about a month.

Cheers!

tabacitu avatar May 24 '22 05:05 tabacitu

Hello @mamprogr Sorry it took so much to get back here.

I don't share the same opinion as @tabacitu here. I think what you want to do is "undoable" from our side (Backpack). The addClause('scope') would simply run the query you have written inside that scope.

If you have:

public function scopeMyBooks($query) {
    return $query->where('book_author_id', backpack_user()->id);
}
public function scopeFavoriteBooks($query) {
    return $query->where('is_favorite', true);
}

There is no way for Backpack to change your $query->where to $query->orWhere.

What you can do is to take that into account and write your scope accordingly, so you can pass a parameter to it and return the desired results.

Something like this should work:

// in your Model.php
public function scopeFavoriteBooks($query, $orWhere = false) {
    return $orWhere ? $query->orWhere('is_favorite', true) : $query->where('is_favorite', true);
}

// in your EntityCrudController.php
CRUD::addBaseClause('FavoriteBooks', true); 

Notice the addBaseClause instead of addClause. The base clause is used so that the entries not matched in the query are not counted in the total count.

I will be closing this, I don't think there is any bug here or nothing that we, Backpack, should be doing.

Cheers guys!

pxpm avatar Nov 02 '22 15:11 pxpm