Thursday, March 16, 2017

laravel display variable from second table

Welcome ! I made app where user has it's own notes ( based on foreign key). Now in table I want to display his name but i can't pass it :

Pilot model:

class Pilot extends Model
{
  protected $table = 'pilots';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'phone', 'email',
    ];

    public function note() {
      return $this->hasMany(Note::class);
    }

Note model:

class Note extends Model

{
  protected $table = 'notes';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'data', 'zadanie', 'uwagi', 'pilot_id',
    ];

    public function pilot() {
      return $this->belongsTo(Pilot::class);
    }

Notes Controller ( i want to display pilot name in index page)

public function index(Request $request)

{
    $notes = Note::all();

    $pilots = Pilot::find($id);
    return view('uwagi.index',['notes'=>$notes,'pilots'=>$pilots]);
}

When i pass it to view via

 

I have an error undefined $id. Each note belongs to user based on foreign key pilot_id which is in note table. This pilot_id key refers to table pilots -> id and i want to display in index page name of this id but i don't know how to pass it.

Regards and thank you for help



via tomczas

Advertisement