lundi 17 octobre 2016

How to get the column values according to id inside blade and controller in Laravel?

I am retrieving all Tickets assigned to a user. A single ticket has a foreign key to users table. These are the code below:

App\Models\User.php
protected $appends = ['full_name', 'avatar'];

public function getFullNameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}


App\Http\Controllers\TicketsController.php
 /**
 * Display a listing of the resource.
 */
public function index()
{
    $tickets = Ticket::paginate(25);

    $users = User::all();

    //return view('user.tickets.index', compact('tickets'));
    return View::make('user.tickets.index', compact('tickets','users'));
}

Resources/Views/User/Tickets/index.blade.php

            <table class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>Status</th>
                    <th>Assigned To</th>
                </tr>
            </thead>
          <tbody>

             @foreach($tickets as $item)
                 <tr>
                    <td></td>
                    <td></td>
                 </tr>
             @endforeach


            </tbody>

        </table>

I want to convert the field into The field should be converted into the First Name according to the $item->user_id

user_id is a foreign key to tickets and the equivalent of it is the is of Users table.

Please help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire