Filipe Ferminiano
Filipe Ferminiano

Reputation: 8801

Undefined property

I'm trying to create a CRUD on my model, but I'm getting this error:

Undefined property: App\Http\Controllers\MarketingChannelController::$MarketingChannelRepository

On this funciton:

 public function edit($id)
    {
        //

        return view('MarketingChannel.create', [
            'MarketingChannel' => $this->MarketingChannelRepository->getMarketingChannel($id),
        ]);
    }

This is my MarketingChannelRepository:

<?php

namespace App\Repositories;

use App\MarketingChannel;

class MarketingChannelRepository
{
    /**
     * Get all of the tasks for a given user.
     *
     * @param  User  $user
     * @return Collection
     */
    public function allMarketingChannels()
    {
        return MarketingChannel::orderBy('id', 'asc')
                    ->get();
    }

    public function getMarketingChannel($id)
    {
        return MarketingChannel::where('id', $id)
                    ->orderBy('id', 'asc')
                    ->get();
    }

}

?>

Upvotes: 1

Views: 1231

Answers (1)

Huzaib Shafi
Huzaib Shafi

Reputation: 1138

The Controller needs the repository variable.

Example:

class MarketingChannelController extends Controller{
    protected $MarketingChannelRepository;

    public function __construct(MarketingChannelRepository $MarketingChannelRepository){
     $this->MarketingChannelRepository = $MarketingChannelRepository;
    }
    public function edit($id)
    {


        return view('MarketingChannel.create', [
        'MarketingChannel' => $this->MarketingChannelRepository->getMarketingChannel($id),
    ]);
    }
}

Upvotes: 1

Related Questions