WallyJ
159
42
Laravel

Referencing a variable within a controller is not recognized

Posted 8 months ago by WallyJ

If I have relationships set up for the following: Contacts have ContactNotes Contacts have Deals Deals have Tasks

public function show($id)
    {
        $contacts = Contact::find($id);
        $contactnotes = Contactnote::orderBy('created_at', 'desc')->where('contact_id', '=', $contacts->id)->get();
        $deals = Deal::where('contact_id', '=', $contacts->id)->get();
        $tasks = Task::where('deal_id', '=', $deals->id);
        // Check for correct user
        if(auth()->user()->id !== $contacts->user_id){
            return redirect('/contacts')->with('error', 'That Is Not Your Contact');
        }
        return view('contacts.show')
        ->with('contact', $contacts)
        ->with('contactnotes', $contactnotes)
        ->with('deals', $deals)
        ->with('tasks', $tasks);
    }

I am getting an error "Property [id] does not exist on this collection instance." because I am obviously doing something wrong in the "$tasks=" line.

I am making things a little difficult here because the contact view (show) pulls in the contact info, the contact notes, and then includes deals.index, which brings in the task info.

I tried to simply reference $tasks in the DealsController, but I'm thinking that since the Contacts view is the primary view being called, all variables have to be defined within that controller function. Am I right about that?

Or can you include a file, and it's part of it's own controller will be called?

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.