statamic-rad-pack / runway

Eloquently manage your database models in Statamic.
https://statamic.com/runway
MIT License
107 stars 42 forks source link

Relationship (wishlistProducts) on runway User not properly working #503

Closed sennewancour closed 1 month ago

sennewancour commented 1 month ago

Description

I'm loading a relationship called wishlistProducts (return $this->belongsToMany(Product::class, 'user_wishlistproducts', 'user_id', 'product_rentman_id', 'id', 'rentman_id');) on my User model, but it returns following error.

Object of class Illuminate\Database\Eloquent\Relations\BelongsToMany could not be converted to string

I call the relation with the following code. The {{ name }} tag is rendered the correct way, so the right user is called. ` {{ _currentUser = {user}; }}

{{ _currentUser:id | dump}}

{{ runway:user where='id:{_currentUser:id}' }}
    {{ name }}
    {{ wishlist_products }}
        {{ id }}
    {{ /wishlist_products }} 
{{ /runway:user }}

`

Steps to reproduce

  1. Create User repository in runway.php
  2. Add 'Has many' field in the blueprint of the runway User.
  3. Create relation on the User model (see description)
  4. The relation table is made via a migration with the columns 'user_id' and 'rentman_product_id'

Environment

Environment Application Name: Statamic Laravel Version: 11.8.0 PHP Version: 8.2.16 Composer Version: 2.7.6 Environment: local Debug Mode: ENABLED URL: ambiance.test Maintenance Mode: OFF

Cache Config: NOT CACHED Events: NOT CACHED Routes: NOT CACHED Views: CACHED

Drivers Broadcasting: log Cache: statamic Database: mysql Logs: stack / single Mail: smtp Queue: sync Session: file

Statamic Addons: 3 Sites: 1 Stache Watcher: Enabled Static Caching: Disabled Version: 5.5.0 PRO

Statamic Addons joetannenbaum/antlers-stack-tags: 0.1.0 statamic-rad-pack/runway: 7.1.0 statamic/eloquent-driver: 4.0.0

Statamic Eloquent Driver Asset Containers: file Assets: file Blueprints: eloquent Collection Trees: eloquent Collections: eloquent Entries: eloquent Forms: eloquent Global Sets: eloquent Global Variables: eloquent Navigation Trees: eloquent Navigations: eloquent Revisions: eloquent Taxonomies: eloquent Terms: eloquent

sennewancour commented 1 month ago

Okay just discovered the problem. When the 'Relationship Name' field (hasmany or belongsto) in the blueprint is filled in with the wrong name for example 'Product' instead of 'product' it throws the error.

duncanmcclean commented 1 month ago

When you say "relationship name", are you talking about the "Resource" setting?

sennewancour commented 1 month ago

Screenshot 2024-05-30 at 10 24 54

It's the setting in the blueprint

duncanmcclean commented 1 month ago

Ah okay! Yes, that's case sensitive and should match exactly with the relationship's method name in your model.

sennewancour commented 1 month ago

Allright took me a while to realise that ;) Thanks for the help!