0

I am building a RESTful API with Laravel and I am trying to develop a feature, which allows to send complex queries based on URL paramters to my database models.

I found a promising repository, which seems to implement the wanted functionality. https://github.com/heroicpixels/filterable

I struggle with understanding the wrapper and traits. I extended my model like mentioned in the documentation class Event extends Filterable {}. But I don't know how to properly implement the FilterableTrait and FilterableWrapper.

When I place use FilterableTrait inside my class, I get an error saying

Heroicpixels\Filterable\Filterable and Heroicpixels\Filterable\FilterableTrait define the same property ($filterable) in the composition of App\Event. This might be incompatible, to improve maintainability consider using accessor methods in traits instead. Class was composed

Currently my Event model looks like this

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;
use Heroicpixels\Filterable\Filterable;

class Event extends Filterable {

    /*
    use FilterableTrait;
    */

    protected $dates = ['deleted_at','starts_at','ends_at'];

    protected $fillable = [
        'deleted_at',
        'locale',
        'sports_id',
        'starts_at',
        'ends_at',
        'locations_id',
        'creators_id',
        'capacity',
        'attendees',
        'information',
        'level',
        'price',
        'lat',
        'lng'
    ];

    protected $hidden = ['created_at','updated_at','deleted_at'];

    public function creator() {
        return $this->hasOne('App\User','id','creators_id');
    }


    public function scopeFuture($query)
    {
        return $query->where('starts_at', '>=', Carbon::now());
    }

    public function scopeFreespots($query)
    {
        return $query->where('capacity', '>', 'attendees');
    }
}

What am I missing? How can I implement the Trait and the Wrapper so my URL parameters are used as query params? Thanks a lot for ur help!

2 Answers 2

1

As far as I can understand you have to choose just one method (out of three).

You are trying to extend Filterable class AND use the trait. If you want to use the trait method you have to extend Eloquent class.

It's probably best to extend the Filterable class as this method is also used in the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right. I think I kinda went blind. I missed to define the $columns variable to declare which columns are taken into consideration. I thought if I don't define, all will be taken into consideration. This is not the case. So set $columns and it is working like a charme.
0

I have running to the same issue before. This is what I did end up doing. let's your api url looks like this

$url = /query?id=999&quantity=10&action=update&api=98546

in your routes create a route , i m using closure for example

route::get('/query/{name}',function($name){
   $modal = $name;
   $params = Request::all();
});

//output  products
{"id":"999","quantity":"10","action":"update","api":"98546"}

In your controller you can use switch case to adapt this any model and build your query from the url params.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.