したいこと:
各ユーザーがログインで入ったら、Userが持つjob_idに紐付く社員のみのプロファイルを表示させたいです。
誰か教えてください!
表示できなかったView ファイル下記です。
@if($users) @foreach($users as $user) <p>{{ $user->employee_job_offers->job_id }}</p> <p>{{ $employee->employee_id}}</p> <p>{{ $employee->gender}}</p> <p>{{ $employee->birth_on }} </p>
error:
Trying to get property of non-object (View: /vagrant/test/resources/views/home.blade.php)
説明:
データベース一つ:
テーブルは下記の四つ:
1,User
2,employee
3,employee_profiles
4,employee_job_offers
1,Userテーブル(ログインで入る)にJob_idがあります
select *from user; id | name | job_id
user Modleは下記です。
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password','job_id', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; //Userに所属社員 public function employee(){ return $this->hasMany('App\employee')->orderBy('created_at', 'desc'); } //Userに所属社員のプロファイル public function employee_profiles(){ return $this->hasMany('App\employee_profiles')->orderBy('created_at', 'desc'); } //Userに所属社員の仕事内容をもつテーブル public function employee_job_offers(){ return $this->hasMany('App\employee_job_offers')->orderBy('created_at', 'desc'); } }
2,下記はemployeeテーブル
select
1id | name | employee_id
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Employee extends Model { public function employee_profiles(){ return $this->hasMany('App\employee_profiles')->orderBy('created_at', 'desc'); } }
3,下記はemployee_profilesテーブル
select
1id | employee_id | gender | birth_on
<?php namespace App; use Illuminate\Database\Eloquent\Model; class employee_profiles extends Model { public function employee_job_offers(){ return $this->hasMany('App\employee_job_offers')->orderBy('created_at', 'desc'); } }
4,下記はemployee_job_offersテーブル:
select
1id | employee_id | job_id
<?php namespace App; use Illuminate\Database\Eloquent\Model; class employee_job_offers extends Model { public function user(){ return $this->belongsTo('App\User'); } public function employee_profiles(){ return $this->belongsTo('App\Employee_profiles'); } }
UserController:
public function index() { $employee_profiles = Employee_profiles::all(); $employee = Employee::all(); $job_offers = Employee_job_offers::all(); return view('home',compact('employee_profiles','employee','job_offers')); }
ControllerからViewへはどのように追加をしていますか? また、@foreach($users-> as $user)の->は記述ミスですかね・・・?
ありがとうございます!!ここに書くときのミスです!!
controllerを追加しました。宜しくお願いします
回答1件
あなたの回答
tips
プレビュー