staudenmeir / belongs-to-through

Laravel Eloquent BelongsToThrough relationships
MIT License
1.15k stars 88 forks source link

BelongsToThrough::associate() #49

Closed vmihaly2020 closed 3 years ago

vmihaly2020 commented 4 years ago

"staudenmeir/belongs-to-through": "2.9", PHP 7.2.26 Laravel Framework 6.18.19 I get this error on update: Call to undefined method Znck\Eloquent\Relations\BelongsToThrough::associate() and this is my relation methode:

 public function language() {
        return $this->belongsToThrough(\ModulMap::model('Language'), [\ModulMap::model('FaqCategory')], null,'', [\ModulMap::model('FaqCategory') => 'category_id'] );
 }
staudenmeir commented 4 years ago

I get this error on update:

What update? This method doesn't exist.

vmihaly2020 commented 4 years ago

On update form of FaqTopicModel

use Illuminate\Database\Eloquent\Model;
class FaqTopic extends Model{
 public function language() {
        return $this->belongsToThrough(\ModulMap::model('Language'), [\ModulMap::model('FaqCategory')], null,'', [\ModulMap::model('FaqCategory') => 'category_id'] );
 }
}
staudenmeir commented 4 years ago

How are you using the relationship?

vmihaly2020 commented 4 years ago

I use this with Laravel Nova. The updateting process is controlled by \Laravel\Nova\Resource. I don't know why is called that method. After I have added the ``

public function associate(){ }

into the Znck\Eloquent\Relations\BelongsToThrough it started to work, but I don't think this is the right solution for this issue.

this is the stacktrace


C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:36
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\Relation.php:385
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Fields\BelongsTo.php:253
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Fields\Field.php:341
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Fields\Field.php:320
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Fields\BelongsTo.php:221
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Support\HigherOrderCollectionProxy.php:60
:
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Support\Collection.php:638
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Support\HigherOrderCollectionProxy.php:61
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\FillsFields.php:67
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\FillsFields.php:35
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Http\Controllers\ResourceUpdateController.php:32
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Database\Concerns\ManagesTransactions.php:29
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:349
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:261
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Http\Controllers\ResourceUpdateController.php:41
:
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:45
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Route.php:219
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Route.php:176
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Router.php:681
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:130
C:\projs\vrsrc\lcrm.loc\app\Http\Middleware\PasswordExpired.php:29
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Http\Middleware\Authorize.php:18
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Http\Middleware\BootTools.php:20
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Http\Middleware\DispatchServingNovaEvent.php:20
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php:43
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Http\Middleware\Authenticate.php:31
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php:41
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:76
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php:49
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php:56
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php:37
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php:66
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:105
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Router.php:683
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Router.php:658
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Router.php:624
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Routing\Router.php:613
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:170
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:130
C:\projs\vrsrc\lcrm.loc\vendor\laravel\nova\src\Http\Middleware\ServeNova.php:26
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:21
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:21
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php:27
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php:63
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\fideloper\proxy\src\TrustProxies.php:57
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:171
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:105
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:145
C:\projs\vrsrc\lcrm.loc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:110
C:\projs\vrsrc\lcrm.loc\public\index.php:55
staudenmeir commented 4 years ago

@dennislindsey Can you maybe take a look at this? Did you ever run into the same issue?

staudenmeir commented 4 years ago

I'm afraid I won't be able to help you with this.

It sounds like something that the package can't support. It wouldn't be possible to set the foreign key on the parent model (FaqTopic) from the related model (Language) because it can only be retrieved from the intermediate model (FaqCategory).

dennislindsey commented 4 years ago

I've also not run into this issue before - but I think the core problem is that you'd need to create an instance of the intermediate model for the BelongsToThrough relationship to work. The whole point of using this tool is that the intermediate resource isn't just a pivot table and is its own entity. I'd recommend, within Nova, defining the relationship of the intermediate model to be edited from either the parent or child model; and setting the relationship from parent<->child to be read-only in the interface.

mrkaaf commented 2 years ago

I have same issue when use this with Nova admin panel.