thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

How to access voyager relationship in code #2822

Closed damms005 closed 6 years ago

damms005 commented 6 years ago

Everything works fine in Voyager.

However, in code, when I get the current Auth::user() and do $user->currency it returns null. I also manually declared the normal Laravel way of User's belongsTo currency() method, still not null.

Please help how to use Voyager relationships in normal Laravel code (since everything working perfectly in Voyager. I can do all Voyager BREAD stuff on both models)

Steps To Reproduce:

As described above

JanisE commented 6 years ago

In Laravel, you would add the relationships directly into the database, using foreign keys.

What I did was I used xethron/migrations-generator to create Laravel database migration files for the tables created by Voyager, and then edited the files to add the foreign keys.

Then I ran php artisan migrate:rollback --step=1 as many times as there are tables I created with Voyager, and then php artisan migrate to re-create the tables, now with the foreign keys.

And then the relationships do work.

fletch3555 commented 6 years ago

Voyager creates the relationships dynamically, which is why they only work within Voyager. You'll have to manually create the relationship methods on your model classes to get it to work outside of Voyager.

damms005 commented 6 years ago

That's a tall one!

I hope Voyager will get to this someday...whatever is achievable with Voyager GUI should be close to replicable in user code (like with some kind of inheritance or extends)

Well for me, for now, I simply use the foreign key to do a quick fetch and return :)

public function currency()
    {
        $c = Currency::where('id', $this->currency_id )->first();
        return $c;
    }
JanisE commented 6 years ago

And you say that this does not work for you, if you have the foreign keys set up?

    public function currency()
    {
        return $this->belongsTo('App\Currency');
    }
damms005 commented 6 years ago

Yes. It does not work by default. It does not work out of the box. I had to do the tweak I mentioned previously. What'd you think?

fletch3555 commented 6 years ago

Then you're doing something wrong. Laravel/eloquent model relationships are functionally equivalent, but completely separate, from what Voyager is doing under the hood.

damms005 commented 6 years ago

Then I will need to peek into my codes and see what I am doing wrong. I am glad to hear @fletch3555 confirm that Voyager does handle relationships that I make reference to in my code without needing to declare relationships in the responding model files, as I presently do.

If this is true, then this issue can be closed. I will just peek more into my codebase.

Thanks :+1:

fletch3555 commented 6 years ago

No, you misunderstand. You still have to create the relationships in your models, but there's no reason (related to voyager) that it won't work.

damms005 commented 6 years ago

Oh. Perfect then! (Sorry I wrongfully thought that Voyager automagically handles model relationships without necessarily doing the traditional Laravel relationship hard-coding:) )

But is there any plan to make Voyager peek into $model->relationship and automagically create necessary/needed BREAD relationships (especially for simply browse/read)?

Since Laravel provides a way to traditionally figure our possible relationship existence (i.e. by querying relationship existence e.g. App\Users::has('wallet'))

This will make it possible to just define relationships in just one place (i.e. either in Voyager OR in traditional Laravel way in model files) and it will be available in the other place. It seems better to define the relationship in Laravel and access same in Voyager, than the other way round.

I will like to take this up if you don't mind but I will appreciate some heads-up/personal Skype comm with you @fletch3555 if you don't mind

fletch3555 commented 6 years ago

We have plans for a future release to do some magic, but it relies on return type-hinting, which requires php7.1 (or was it 7.2...?), which just doesn't have the support yet for us to require as a minimum. We'll get it out eventually!

damms005 commented 6 years ago

Yeahhhhhhhhh!!! We rock @Voyager!!!!

Thanks pal. Issue closed :)

emergingdzns commented 6 years ago

I too am wondering if this is coming sooner rather than later. I love the ability to create the BREAD stuff in the admin panel. I am scratching my head as to why it wouldn't automatically add the necessary programming to the models it creates for you. I have worked with Streams laravel module (the base system for PyroCMS) which does all of this, but it's far more complicated and it's a full on CMS which I don't need in this case. At the very least why not make it so that the generated models extend some models that the BREAD could create and post to the storage or resources folder. So the "blank" model Xxxx.php in App folder extends Resources\App\Models\Xxxx (or something like this) which is the model that extends Eloquent models. This way, Voyager's system manages the code in these models and leaves us to only have to put in custom stuff outside of what Voyager is already doing. Just a suggestion. I hate having to write the relationship stuff. Always a struggle.

github-actions[bot] commented 4 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.