Creating function for laravel controller/view to load database values

php sql laravel

637 观看

2回复

1775 作者的声誉

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
作者: Tom N. 的来源 发布者: 2017 年 12 月 27 日

回应 2


2

17643 作者的声誉

决定

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;
作者: Jeff Puckett 发布者: 2017 年 12 月 27 日

0

1600 作者的声誉

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
作者: retrograde 发布者: 2017 年 12 月 27 日
32x32