简体   繁体   中英

Laravel - Returning an Eloquent Instance Syntax

So I have a controller with a constructor that uses Reflection to inject a Eloquent Model dependency:

public function __construct(Analysis $analysis)
{
    $this->analysis = $analysis;
}

This dependency also has other Eloquent Model dependencies that are injected via Reflection:

use App\Load;
use App\Parameter;

class Analysis extends Model
{
    protected $loads;
    protected $parameters;

    public function __construct(Load $loads, Parameter $parameters)
    {
        $this->loads = $loads;
        $this->parameters = $parameters;
    }
}

When I dump or die like so:

dd($this->analysis['loads']);

It returns Load as an Eloquent instance (as I would like it to).

But then when I try dump or die like so:

dd($this->analysis->loads);

It just returns an empty collection. I can't figure out why one works and not the other? I've seen the latter syntax work in tutorial videos.

I wasn't sure what to put in the post title because I don't know the terms of the differing syntax (analysis['loads'] vs analysis->loads).

So changing the loads & parameters properties to public meant that

dd($this->analysis->loads);

Would then return an Eloquent instance, as desired. Probably not the best solution. I guess the correct way would be to use getters and setters...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM