0

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

{{$pilots->name}} 

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

2
  • What exactly do you want to show? just one pilot with its associated notes? Commented Mar 16, 2017 at 9:03
  • Check my answer stackoverflow.com/a/42829707/4049692 Commented Mar 16, 2017 at 9:18

2 Answers 2

0

There is no defined variable $id in your Controller's index method. Also if you are defined model relations, you can grab needed data using Pilot::with('note')->all(); Take a look at this: https://laravel.com/docs/5.4/eloquent-relationships#eager-loading

But the best way to pass pilot_id to route like

Route::get('pilot/{id}', 'NotesController@index')

and index method will be like public function index(Request $request, $pilot_id)

or

Route::resource('pilot', 'NotesController')

and inside controller define show($pilot_id) method than use

$pilot_data = Pilot::with('note')->find($pilot_id);

$pilot_data->note will contain all notes connected to pilot

And according to the code, there is no doubt for binding Request $request inside controller's action, as you do not use it inside the code

Sign up to request clarification or add additional context in comments.

2 Comments

Missing argument 2 for App\Http\Controllers\NotesController::index()
remove Request parameter from index method, leave only $pilot_id
-1

Ok answer is easiest as i thought :

Controller:

public function index(Request $request)

{
    $notes = Note::all();

    return view('uwagi.index',['notes'=>$notes,]);
}

Blade:

@foreach ($notes as $note)
{{ $note->pilot->name }}
@endforeach

1 Comment

Except it doesn't match the question at all (not to mention it's your own question), because the code you've pasted suggests you wanted only notes from a given Pilot, not all of them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.