3

I'm working on updating a laravel blade template to insert some database info into an html table. IN order to do this, I'm having to add new data to the controller for this blade and that's where I'm having some troubles.

I'm still trying to understand more with laravel, so I'm thinking my syntax or methods of creating this data are incorrect but I just can't put my finger on it right now.

In my function below, the $calls_allowed portion was already existing and it works on the page currently. I created the $contact_events portion of the function and that's where my problem is.

IN my view, I created a foreach loop and if statement around the html table in question. The table loads, but it's empty even though there are records in the database for the dealer.

I'm trying to say if $dealer-> id matches contact_events.dealer_num, load all records for that dealer

contact_events is the table and dealer_num is the column I'm matching, then I'm trying to load the columns from that table (updated_at,method,notes) into the html table.

The affected code is below. The view/route/controller work, it's just this function I'm creating that isn't loading data. Any help is much appreciated.

Controller code:

public function show($id)
{
    $d = Dealer::find($id);
    if(!$d){
        \Session::flash('warning_message', 'Sorry that resource can not be found.');
        return redirect()->route('account.dealer.index');
    }

    $calls_allowed = DB::table('dealers.dealers')->
    where('dealer_num', $id)->
    pluck('calls_allowed');

    $contact_events = DB::table('dealers.contact_events')->
    where('dealer_num', $id)->
    pluck('updated_at', 'method', 'notes');

    if(!empty($calls_allowed)){
        $d->calls_allowed = $calls_allowed[0];
    } else {
        $d->calls_allowed = null;
    }
    return view('Account.Dealer.show')->with('dealer', $d);
}

View code:

<thead>
    <tr>
        <th>Contacted Date</th>
        <th>Type of Contact</th>
        <th>Call Notes</th>
    </tr>
</thead>
@foreach($dealer->contact_events as $events)
    @if($events->dealer_num = $dealer->id)
        <tbody>
            <tr>
                <td>{{$events->updated_at}}</td>
                <td>{{$events->method}}</td>
                <td>{{$events->notes}}</td>
            </tr>
        </tbody>
    @endif
@endForeach
8
  • there is a type here: @if($events->dealer_num = $dealer->id) should be == Commented Dec 27, 2017 at 16:45
  • I actually did catch that, unfortunately it still hadn't solved the problem though Commented Dec 27, 2017 at 17:00
  • Use dd() to see if $calls_allowed and $contact_events are getting values Commented Dec 27, 2017 at 17:37
  • are dealers.dealers and dealers.contact_events columns on the dealer's table? Commented Dec 27, 2017 at 17:44
  • @retrograde dealers is the name of the schema. Contact_events and Dealers are each a table in that schema Commented Dec 27, 2017 at 17:45

2 Answers 2

2

It looks like you are not assigning the data to the object after retrieving from database.

$contact_events = DB::table('dealers.contact_events')->
where('dealer_num', $id)->
pluck('updated_at', 'method', 'notes');

// add this
$d->contact_events = $contact_events;
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, I added that line without modifying anything else and it still isn't loading that data for some reason.
I added dd($dealer->contact_events) and it does dump one record, but it still doesn't display for some reason in the html table/view
@TomN. It might be helpful if you add the dd contents to your answer.
pluck only takes 2 arguments
0

This seems like a perfect time to use the power of Laravel's Eloquent ORM...

Check out the with and has in the Laravel docs

This will require some finessing based on your needs, but it will be something like this:

$d = Dealer::where('id', '=', $id)
     ->with('contact_events')->first();

This uses Eloquent to get all of the contact_events that belong to the dealer with the $id.

Then you can do something like this note: this assumes that calls_allowed is a record on the dealer table. if I misunderstood that, you can still run than you can include that just as you have it.

@if(!is_null($dealer->calls_allowed)
@foreach($dealer->contact_events as $events)
    <tbody>
        <tr>
            <td>{{$events->updated_at}}</td>
            <td>{{$events->method}}</td>
            <td>{{$events->notes}}</td>
        </tr>
    </tbody>
@endForeach
@endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.