Referencing a variable within a controller is not recognized

Published 4 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?

Best Answer (As Selected By WallyJ)
Sergiu17

@WallyJ now it's different. Do you have in

#attributes: array:26 [▶]

an attribute called contactnotes ? which is " " ( empty ) ?

Sergiu17
$contacts = Contact::with('contactnotes')->with('deals.tasks')->get();

dd($contacts);

I think this should work in your case.

WallyJ

But wouldn't that get rid of the use of the ID it's pulling from the URL?

Sergiu17

@WallyJ ohh, yes.

$contacts = Contact::with('contactnotes')->with('deals.tasks')->find($id);

Instead of get() use find($id). Try this and tell me if works

WallyJ

Same error. Same line - $tasks

public function show($id)
    {
        $contacts = Contact::with('contactnotes')->with('deals.tasks')->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)-get();
        // 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);
    }
Sergiu17

@WallyJ delete this entirely

// DETELE THIS.
$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)-get();

Try this. your delas, your tasks your contactnotes will be in be in collection if you have setup relationships properly

// TRY THIS
public function show($id)
{
    $contacts = Contact::with('contactnotes')->with('deals.tasks')->find($id);

    dd($contacts);

        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);
}
WallyJ

All of the information shows, so the relationships are working, but if I remove the dd($contacts);, I receive the error "Undefined variable: contactnotes" on this line:

->with('contactnotes', $contactnotes)
Sergiu17

@WallyJ ContactsController.php =>

public function show($id)
{
    $contacts = Contact::with('contactnotes')->with('deals.tasks')->find($id);

    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', $contacts->contactnotes)
    ->with('deals', $contacts->deals);
    // ->with('tasks', $tasks); this work will be done in front end.
}

show.blade.php =>

// this is in view file. I think it makes sense, you separate all your tasks by deal
@foreach ($deals as $deal)
    {{ $deal->name }}
    @foreach($deal->tasks as $task)
        {{ $task->title }}
    @endforeach 
@endforeach 
WallyJ

That makes sense.

However, when I try to use this code in my view, I receive a "Invalid argument supplied for foreach()"

@foreach($contactnotes as $contactnote)
                                <li class="list-group-item">
                                    <p>{{$contactnote->created_at->diffForHumans()}} - {{$contactnote->contactnotetext}}</p>
                                </li>
                                @endforeach

I did a {{dd($contactnotes)}} in the view to test and it only shows quotation marks - ""

It doesn't like something about the variable, or the way I used it.

Also, I tried changing $contactnotes as $contactnote to:

$contacts->contactnotes as $contactnote, and it gave me a "Undefined variable contacts" error on that line.

Sergiu17

@WallyJ do a quick check in your controller

public function show($id)
{
    $contacts = Contact::with('contactnotes')->with('deals.tasks')->find($id);

    if(auth()->user()->id !== $contacts->user_id){
        return redirect('/contacts')->with('error', 'That Is Not Your Contact');
    }

    dd($contacts->contactnotes, $contacts->deals); // ADD THIS LINE TO SEE IF THERE ARE ANY CONTACTNOTES AND DEALS ASSOCIATED WITH TASKS
}
WallyJ

Yes. It returns an array of 2 items (deals), and under item 0, the #relations has an array of 1 task, and under its attributes it shows the fields. So the data is connected.

There's something in the naming between the controller and the view that is off. I just can't figure out what.

Sergiu17

@WallyJ

 return view('contacts.show')
    ->with('contact', $contacts)
    ->with('contactnotes', $contacts->contactnotes)
    ->with('deals', $contacts->deals);

Now open contacts/show.blade.php

{{ dd($contactnotes, $deals) }} 

Do you get something ?

WallyJ

Yes. Same array.

So it's getting the deals, but not the contactnotes.

Actually, it technically returns:

""

Collection {#744)

So I am getting the quotation marks instead of the contact notes collection

Sergiu17

@WallyJ now start looping through it, and display all concatnotes, all deals and tasks' deals

Make sure that there are some contactnotes associated with current category

JoaoPedroAS51

@WallyJ Can you show the Contact Model?

WallyJ

There are definitely notes associated with that contact. That part was working before I added the tasks.

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