dimanche 22 mars 2020

Laravel: How to Filter In Custom GetAttribute Before Collection

I am able to filter the Custom Get Attribute field after retrieving it to the collection. I now need to filter before using get()

Example:

For is_paid custom attribute

//is_paid
public function getIsPurchasedAttribute()
{
    //Since purchased has one relation with user
    return !empty($this->purchasedRelation) ? true : false;
}

I now need to retrieve User based on is_purchased like:

public function index(Request $request){
        $isPaid = $request->is_paid;

        $users = User::query();

        if($isPaid){
            $users = $users->where('is_paid', true);

        }

        $users = $users->paginate(10);

        return view('user.index', [
            'users' => $users
        ]);

    }

If I do like this, it will return:

Column not found: 1054 Unknown column 'is_paid' in 'where clause

Yes, I can do like but I am now unable to use paginate function:

$users = User::get();

$users = $users->where('is_paid', 1);

return view('user.index', [
    'users' => $users
]);


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3dn8E6r
via IFTTT

Aucun commentaire:

Enregistrer un commentaire