Is it possible to add additional query variable(s) for relationship?

Solved
jeff

Feb 5th, 2018 10:25 PM

I have a relationship setup between the 'Users' table and a 'Students' table. The relationship is belongsToMany using a pivot table called 'users_students'.

This was setup in the Voyager admin and works well.

I'm using a drop-down on the Student edit screen but I only want to show Users with a certain role (parent) - thus the need for an additional variable in my query. Is there any approach using the GUI that will work for this?

devdojo

Feb 11th, 2018 03:27 AM

Best Answer

Hey Jeff,

Sorry for just getting back to you now, it doesn't look like there is a custom solution for that at the moment. There may be something in the works for more relationship options, but I would have to do a little digging through some of the Pull Requests.

One solution that you may want to look at doing for the mean time is to override your edit-add.blade.php. You would create a new view inside of resources/views/vendor/voyager/students/edit-add.blade.php from the edit-add.blade.php located here: https://github.com/the-control-group/voyager/blob/1.0/resources/views/bread/edit-add.blade.php You would then need to override that row in the file to something like this:

@foreach($dataTypeRows as $row)
    @if($row->field == 'name_of_relationship_field')
		   {{-- custom dropdown for the student parents relationship --}
		@else
		   {{-- Do the default stuff here --}}
		@endif
@endforeach

Hope that helps a little :)

Thanks!

jeff

Feb 12th, 2018 03:23 PM

@devdojo - thanks so much for the response. I thought I might need to go this route but didn't want to miss something obvious.

I'll give it a shot and report back.

Thanks!

xavi-baz

Mar 27th, 2018 08:55 AM

Hi!

I'm trying to do the same thing, but in a BelongsToOne relationship, filtering the list of values from the related table. I guess it would be the same procedure?

Looking at the code of edit-add.blade.php, I guess that the part that is retrieving the data is here:

@if($row->type == 'relationship') @include('voyager::formfields.relationship') @else .....

@jeff, did you manage to make it work?

I would appreciate your help... :)

Many thanks in advance! X.

xavi-baz

Mar 27th, 2018 09:55 AM

Hi again,

I figured it out... It works (Laravel 5.5, Voyager 1.0.16).

As @devdojo suggested, I added that extra "if" condition, and there I copied the code from formfields/relationship.blade.php, changing $query with the appropriate method of the model instead of all().

Cheers! X.