Wednesday, March 15, 2017

laravel retrieving data from database

Welcome ! I dont have any idea how to retrieve data with belongs to user.

Note model :

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

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

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);
    }
}

Controller:

public function show($id)

{

    $notes = Note::with('pilot')->get();


    return view('dziennikpracy.show',compact('notes'));

}

Right now it displays me all notes and i want to display only notes which belongs to each user and i don't know how.

Regards



via tomczas

Advertisement