mehdi-fathi/eloquent-filter

Possible to make queries for relationships?

orenlande opened this issue · 9 comments

Hello, great library. I wondered if it's possible to support relationship models?
Lets say I have Orders model, and there Order Shipment model.
Relationship looks like $this->hasOne('OrderShipment', [primary_key]).

Now, when we query the information, I need to able search in these related models. for example, I'd like to have something like &order_shipment.status=New

Final SQL should be something like SELECT .. WHERE ... AND order_shipment.status = 'New'.

No eloquent filter doesn't support relationship models now.I wanted to add the feature to this but prefer to implement a custom query filter. I'm working on new version 2 this library with many wonderful features maybe I will add this feature to that.but you can implement this feature by override method filter.

Do you have an example of how to implement it? I'd like to avoid as much as possible from creating custom traits since I have many relationships across my code.

Make a method at the same query string in the model. For example :

public function status(Builder $builder, $value){ return $builder->with(['clients', 'tasks', 'status']) ->whereHas('status', function($q) use($value) { // Query the name field in status table $q->where('name', '=', $value); // '=' is optional }) }

test it, please.maybe this work.

Hey, So I made some small tryouts with a possible relationship implementation. Please check the forked repository lastest commit: https://github.com/orenlande/eloquent-filter/commits/master

I know it's not really good implementation, Just wanted to test out the concept.
I'm using the ":" delimiter to identify the table and the field inside it. aka [table]:[field].

You welcome to take cues from this solution if you feel it's makes sense to work this way.

  1. Seems it working well, but limited to 1st level relationship every time. We might need to build another class to handle relationship building.

  2. I can't find a practical method to query deeper relationships like 2nd relationship, 3rd relationship, etc...
    Here some clear example to demonstrate:

$order->hasOne('order_details'); // <-- we can query this

$order_details->hasOne('product_information') // <-- this 2nd relationship already if called from $order level, cannot query this.

we need do something like [table1]:[table2] ... [field].
Feels like we need to run recursions to solve it.

Let me know what you think.

@orenlande your code is a little complex and doesn't have a unit test for this feature.i'v implement this feature in branch mock-test-relation but I haven't released that now and I don't want to handle all query complex in this package. I've made an override method feature for custom query complex in the eloquent filter.
Thanks

@orenlande wait a few days. I will release a beta version for this feature and just for your job.just you can test it in real work and you should respond to me.ok?

@mehdi-fathi sure, thank you!

@orenlande I finished this feature.just install "mehdi-fathi/eloquent-filter": "dev-mock-test-relation"
please edit composer.json and run composer update to switch to this version.set version dev-mock-test-relation in commposer file.you should delete composer.lock .for use it you just set query string as this example:

http://127.0.0.1:8000/panel/dashboard/list_usdt_investment?user[ProfitUsdtMonthlyRefId][user_id]=20

user is a relation model and ProfitUsdtMonthlyRefId is a relation model to users and user_id is a field in the ProfitUsdtMonthlyRefId table.

just you must set
private static $whiteListFilter = ['user.ProfitUsdtMonthlyRefId.user_id'];
in your model.enjoy and if you have any idea i glade to hear that.Thanks

@orenlande Install version mehdi-fathi/eloquent-filter": "dev-v-2" for test hottest new features and read this doc in the link https://github.com/mehdi-fathi/eloquent-filter/tree/v-2