laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.31k stars 10.95k forks source link

Eager loading manyToMany relations pivot error on php 7.4 #31099

Closed Krofek closed 4 years ago

Krofek commented 4 years ago

Type of Illuminate\Database\Eloquent\Relations\Pivot::$ must be array (as in class Illuminate\Database\Eloquent\Model)

the issue happens on:

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Pivot.php:8 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1078

Inserting, updating intermediate tables works. Whenever I try to eager load with ->with() or ->load() I get the error on the Pivot class.

I think it's a php 7.4 issue, since it worked fine on 7.3

edit:

App\Models\Grow

    public function waterings()
    {
        return $this->hasMany(Watering::class);
    }

App\Models\Watering

    public function nutrients()
    {
        return $this->belongsToMany(Nutrient::class)->withPivot('value');
    }

App\Models\Plant

    public function waterings()
    {
        return $this->belongsToMany(Watering::class);
    }

App\Http\Controllers\GrowController

    public function show(Grow $grow)
    {
        $grow->load([
            'user', 'plants.medium.type', 'plants.medium.company', 'plants.strain.company', 'waterings.nutrients'
        ]);
        return new GrowResource($grow);
    }

I dies when I try to load 'waterings.nutrients'

or the second case is:

App\Repositores\GrowRepository

    public function getByRequest(Request $request)
    {
        $query = $this->grow->with(['user', 'plants.medium.type', 'plants.medium.company', 'plants.strain.company', 'plants.waterings']);

        if ($request->get('done')) $query->whereNotNull('finished_at');

        $this->sortQuery($request, $query);

        return $this->paginateResults($request, $query);
    }

Here it dies when trying to load 'plants.waterings'

ankurk91 commented 4 years ago

Can you post minimal piece of code.

Krofek commented 4 years ago

I edited the issue post.

Krofek commented 4 years ago

After upgrading homestead to the lastest version and vagrant box to the latest version also, the error changed to

Type of Illuminate\Database\Eloquent\Relations\Pivot::$guarded must be array (as in class Illuminate\Database\Eloquent\Model)

It gets fixed by editing line 24 on:

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Pivot.php

Since the Model class has the Illuminate\Database\Eloquent\Concerns\GuardsAttributes on line 21 the guarded property is array typed.

Question, since the $guarded property is defined in Model, does it need to be defined again in the Pivot model?

driesvints commented 4 years ago

My guess is that you're using a PHP 7.4 property type for guarded. You can't do this for any properties inherited from framework classes because any properties from the framework aren't typed yet because we still need to support PHP 7.2 and 7.3

faridvatani commented 3 years ago

My guess is that you're using a PHP 7.4 property type for guarded. You can't do this for any properties inherited from framework classes because any properties from the framework aren't typed yet because we still need to support PHP 7.2 and 7.3

Is there any trick to bypass this problem? I have the same problems.

driesvints commented 3 years ago

No there isn't.

mostafaznv commented 3 years ago

I updated my laravel 6 project and I got this error on all my models. I fixed them but I got this error on laravel's own models.

Type of Illuminate\Database\Eloquent\Relations\Pivot::$guarded must be array (as in class Illuminate\Database\Eloquent\Model)
Type of Illuminate\Notifications\DatabaseNotification::$table must be string (as in class Illuminate\Database\Eloquent\Model)

Laravel version: 6.20.16 php version: 7.4 using with ubuntu/nginx