ronon
237
8
Laravel

Relationship null in console

Posted 4 months ago by ronon

I need to load relationships on a command.

The problem is that it's always null.

I tried it with with('myrelation') and load('myrelation') but it's always empty. Running it from browser the relationship is available.

Why the heck is it null? I don't get it.

class CheckImage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check:image {limit?}';

    /**
     * The console command description.
     *
     * @var string
     */

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $limit = 200;
        if(!is_null($this->argument('limit')))
            $limit = $this->argument('limit');

        $post = Post::with('image')->where('slug', $slug)->first();
         dd($post->image); // always null
    }
}

also tried

$post = Post::first();
$post->load('image');
dd($post->image); // <- also null

Please sign in or create an account to participate in this conversation.