Vinelab / NeoEloquent

The Neo4j OGM for Laravel
MIT License
633 stars 199 forks source link

Model::withCount('relation') throws "Array to string conversion" #193

Closed znittzel closed 4 years ago

znittzel commented 7 years ago

When using Laravels "withCount" on a models relation it returns the error:

Array to string conversion in HasOneOrMany.php line 412

. Im not sure why this error is thrown in

Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

when NeoEloquent has its own HasOneOrMany -class. Maybe i've set up the model incorrectly:

namespace App;

use Vinelab\NeoEloquent\Eloquent\Model as NeoEloquent;

class Event extends NeoEloquent
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'price',
        'picture',
        'currency',
        'date',
        'from_time',
        'to_time',
        'geolocation',
        'city',
        'description',
        'age_limits',
        'is_private'
    ];

    /**
     * Label in graph db
     */
    protected $label = "Event";

    /**
     * The host (user) of this Event
     */
    public function host() {
        return $this->hasOne('App\User', 'host');
    }

    /**
     * Guests of this Event
     */
    public function guests() {
        return $this->belongsToMany('App\User', 'going_to');
    }

    /**
     * Invited users through Invite
     */
    public function has_invites() {
        return $this->hasMany('App\Invite', 'has');
    }
}

And the query

// Get all public events with count of guests
$events = Event::whereIs_private(false)->withCount('guests')->get();
dgerike commented 7 years ago

Have the same problem, just for the other edge direction.

ErrorException in BelongsTo.php line 102:

Any ideas?

znittzel commented 7 years ago

I became a part of Neo's Slack-channel and asked generally about using "with". I got a reply that "with" is not ment to work more than including a related model, like so Model::with('subModel')->get(). Right now I'm going the long way of counting the objects in my code, which gives me an extra call to the db. Although, I saw while debugging in the neoEloquent that the framework itself is doing an extra call anyway when using "with" so maybe there's no better solution.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.