Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get a hasMany of hasMany using jenssegers-mongodb library.

Website model:

use CviebrockEloquentSluggableSluggable;
use IlluminateDatabaseEloquentModel;
use JenssegersMongodbEloquentSoftDeletes;
use JenssegersMongodbEloquentHybridRelations;

class Website extends Model
{
    use HybridRelations, SoftDeletes, Sluggable;

    protected $connection = 'mysql';

    protected $dates = ['created_at', 'updated_at'];

    protected $fillable = ['name', 'link', 'slug'];

    public function visitors()
    {
        return $this->hasMany(Visitor::class, 'website_id', 'id');
    }

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }
}

Visitor model:

use JenssegersMongodbEloquentModel as Eloquent;
use JenssegersMongodbEloquentSoftDeletes;

class Visitor extends Eloquent
{
    use SoftDeletes;

    protected $connection = 'mongodb';

    protected $dates = ['created_at', 'updated_at'];

    protected $fillable = ['uid', 'ip', 'deviceInfo', 'location', 'currentPage', 'currentSection', 'created_at', 'updated_at'];

    public function campaigns()
    {
        return $this->hasMany(Campaign::class);
    }

    public function clicks()
    {
        return $this->hasMany(Click::class, 'tracking_id', '_id');
    }

    public function visits()
    {
        return $this->hasMany(Visit::class);
    }
}

Click model:

use JenssegersMongodbEloquentModel as Eloquent;
use JenssegersMongodbEloquentSoftDeletes;

class Click extends Eloquent
{
    use SoftDeletes;

    protected $connection = 'mongodb';

    protected $dates = ['created_at', 'updated_at'];

    protected $fillable = ['page', 'section', 'click', 'count', 'created_at', 'updated_at'];

    public function tracking()
    {
        return $this->belongsTo(Tracking::class);
    }
}

I am able to get $website->visitors but not $website->visitors()->clicks(). Also I need to perform some aggregation on these clicks.

Error Received:

BadMethodCallException: Method IlluminateDatabaseEloquentCollection::clicks does not exist. in file /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php on line 103

#0 /Applications/MAMP/htdocs/tracking-admin/backend/app/Repositories/Website/WebsiteRepository.php(114): IlluminateSupportCollection->__call('clicks', Array)
#1 /Applications/MAMP/htdocs/tracking-admin/backend/app/Http/Controllers/WebsiteController.php(116): AppRepositoriesWebsiteWebsiteRepository->collectiveClickData('localhost')
#2 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): AppHttpControllersWebsiteController->collectiveClickData(Object(AppHttpRequestsWebsiteValidateDelete), 'localhost')
#3 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): IlluminateRoutingController->callAction('collectiveClick...', Array)
#4 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Route.php(254): IlluminateRoutingControllerDispatcher->dispatch(Object(IlluminateRoutingRoute), Object(AppHttpControllersWebsiteController), 'collectiveClick...')
#5 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Route.php(197): IlluminateRoutingRoute->runController()
#6 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): IlluminateRoutingRoute->run()
#7 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): IlluminateRoutingRouter->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#8 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#9 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateRoutingMiddlewareSubstituteBindings->handle(Object(IlluminateHttpRequest), Object(Closure))
#10 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(127): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#11 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(63): IlluminateRoutingMiddlewareThrottleRequests->handleRequest(Object(IlluminateHttpRequest), Object(Closure), Array)
#12 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateRoutingMiddlewareThrottleRequests->handle(Object(IlluminateHttpRequest), Object(Closure), '60', '1')
#13 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(44): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#14 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateAuthMiddlewareAuthenticate->handle(Object(IlluminateHttpRequest), Object(Closure), 'api')
#15 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#16 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): IlluminatePipelinePipeline->then(Object(Closure))
#17 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php(670): IlluminateRoutingRouter->runRouteWithinStack(Object(IlluminateRoutingRoute), Object(IlluminateHttpRequest))
#18 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): IlluminateRoutingRouter->runRoute(Object(IlluminateHttpRequest), Object(IlluminateRoutingRoute))
#19 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): IlluminateRoutingRouter->dispatchToRoute(Object(IlluminateHttpRequest))
#20 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): IlluminateRoutingRouter->dispatch(Object(IlluminateHttpRequest))
#21 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): IlluminateFoundationHttpKernel->IlluminateFoundationHttp{closure}(Object(IlluminateHttpRequest))
#22 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#23 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure))
#24 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#25 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure))
#26 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#27 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewareValidatePostSize->handle(Object(IlluminateHttpRequest), Object(Closure))
#28 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#29 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): IlluminateFoundationHttpMiddlewarePreventRequestsDuringMaintenance->handle(Object(IlluminateHttpRequest), Object(Closure))
#30 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/fruitcake/laravel-cors/src/HandleCors.php(37): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#31 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): FruitcakeCorsHandleCors->handle(Object(IlluminateHttpRequest), Object(Closure))
#32 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/fideloper/proxy/src/TrustProxies.php(57): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#33 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): FideloperProxyTrustProxies->handle(Object(IlluminateHttpRequest), Object(Closure))
#34 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#35 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): IlluminatePipelinePipeline->then(Object(Closure))
#36 /Applications/MAMP/htdocs/tracking-admin/backend/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): IlluminateFoundationHttpKernel->sendRequestThroughRouter(Object(IlluminateHttpRequest))
#37 /Applications/MAMP/htdocs/tracking-admin/backend/public/index.php(55): IlluminateFoundationHttpKernel->handle(Object(IlluminateHttpRequest))
question from:https://stackoverflow.com/questions/66059236/jenssegers-mongodb-hasmany-to-hasmany-relation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
249 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...