Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BelongsToDepend doesn't support DisplayUsing despite inheriting from BelongsTo #93

Open
azariah001 opened this issue Jul 6, 2021 · 3 comments

Comments

@azariah001
Copy link

azariah001 commented Jul 6, 2021

I've been implementing BelongsToDepend into an existing Nova site to ensure admin users can't mix up records associated with different customers. Eg select a company and have it filter client records to just that companies clients.

However, for whatever reason, I can't get any version of ->displayUsing() to work, it doesn't error it just does nothing. This is pretty critical as the code is currently hardcoded to use public static $title for the descriptions in the dropdown which only supports one field. I am dealing with client first name and last name columns that need to be combined for display which can't be done within $title because of the rules of static I guess, and I have another data class whose description is actually derived from one of its associated classes.

Now, I have seen other documentation that says I can use public function title() in my Nova/*.php in lieu of $title however... it doesn't work for BelongsToDepend for some reason. Now I've been digging through the PHP file for this field and I can't figure out where I would even begin to insert a patch for this to make it work. Weird thing is, the PHP file says, class NovaBelongsToDepend extends BelongsTo I was under the impression from my experience that when you extend a class you're not supposed to be able to break base class functions, override and modify the outcome sure but... not completely break. And this PHP file doesn't have an override from the original DisplayUsing function declaration so... what's gone wrong, and what can I do to help fix it?

@azariah001
Copy link
Author

azariah001 commented Jul 9, 2021

Found a workaround. Set $title to a key that isn't in your schema, I'm using name, and then create that column by customising the $builder query object in protected static function boot() static::addGlobalScope(). I created my custom column by joining some tables and then doing a $builder->select(DB::raw('CONCAT(client.first_name, " - ", client.last_name,) AS name'));.

Note: I do not believe this to be particularly maintainable and I do not recommend doing this, but until base class function inheritance is rectified here's your workaround.

BTW, I found another base class function that doesn't work ->withMeta() which is useful for selecting defaults, especially for having a variant of a form that has fields that ->dependsOn() another field, where you might want the dependency field selection hidden and hardcoded for some users but available for selection by admins. Again, works with a base BelongsTo form object.

@azariah001
Copy link
Author

Ha, yeah, very not maintainable. Having to do a $builder->select() in the app/*.php to make this work breaks selects for Trend card aggregate function calls such as countByDays().

@azariah001
Copy link
Author

azariah001 commented Jul 13, 2021

And found a fix. ->addSelect() also ->selectRaw(). It's amazing when google finally figures out what you're searching for. https://laravel.com/docs/8.x/queries#select-statements

If you're here in the future because you're searching for "laravel nova unkown column date_result" your issue is that you're using a select statement in your app/*.php protected static function boot() static::addGlobalScope() $builder. You should switch to using the addSelect and selectRaw function calls instead. This should allow things like Trends->aggregate()'s select function call to be processed allowing it to also add columns.

Actually... where's a Laravel Nova dev. Why is the Trends class using a select statement to add columns? If addSelect and selectRaw exist for this purpose why isn't trends (and probably other classes) using them for this purpose? Just curious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant