卡在集合数组上
我正在获取特定数组。但无法在刀片中循环
这是我的控制器:
public function appliedJob($id)
{
$apllied = AplliedJob::with('user','job')->find($id);
return view('dashboardviews.page.apllied-job',compact('apllied'));
}
这是我的模特
class AplliedJob extends Model
{
use Notifiable;
protected $fillable = [
'name', 'email', 'message','aplied_cv',
];
protected $table='appllied_jobs';
//protected $primaryKey = 'user_id';
protected $primaryKey = 'user_id';
public function user()
{
return $this->belongsTo('App\User','user_id');
}
public function job()
{
return $this->belongsTo('App\Job','job_id');
}
}
这是我要获取的数组,我想访问关联#[作业],但它 引发错误
AplliedJob {#257 ▼
#fillable: array:4 [▶]
#table: "appllied_jobs"
#primaryKey: "user_id"
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [▼
"user" => User {#263 ▼
#fillable: array:4 [▶]
#table: "users"
#hidden: array:2 [▶]
#casts: array:1 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▶]
#original: array:9 [▶]
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
"job" => Job {#261 ▼
#fillable: array:4 [▶]
#table: "jobs"
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 1
"user_id" => 2
"jb_name" => "web developer"
"jb_type" => "software"
"jb_salary" => "12000"
"jb_exp" => "12"
"jb_description" => "djghnbjkguyfgykgvkvbuykgbkub g uiygjghpiu p;"
"jb_loc" => "lahore"
"created_at" => "2019-05-15 01:18:46"
"updated_at" => "2019-05-15 01:18:46"
]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
我已经尝试过了,但是却给出了例外情况
@foreach($apllied as $aplil)
{{$aplil->job->jb_name}}
@endforeach
这是我遇到的错误,无法弄清楚我在做什么错
ErrorException (E_ERROR)
Trying to get property 'job' of non-object (View:
D:\xampp\htdocs\locojobs\resources\views\dashboardviews\page\apllied-
job.blade.php)
答案 0 :(得分:1)
find()
返回一个模型,因此您需要执行以下操作:
{{$apllied ->job->jb_name}} //Remove the loop
或使用get()
代替find()
答案 1 :(得分:0)
我从这段代码中得到的结果是,每个AplliedJob实例都有一个Job。因此,正如@Anas Bakro所说,您需要从代码中删除@foreach。如果要构建其他体系结构,请写下您的表格。
答案 2 :(得分:0)
尝试使用get()或paginate()代替find(),因为find()返回单个值。
尝试以下代码,
public function appliedJob($id)
{
$apllied = AplliedJob::with('user')->with('job')->get();
return view('dashboardviews.page.apllied-job',compact('apllied'));
}