我正在尝试根据名为" stn"的属性显示我的学生的对象。在学生表中。我在使用主键时能够显示它,但是当使用属性时,该函数在用于返回对象时不返回任何内容。
@foreach($users as $user)
@foreach($user->contactlogs as $logs)
{{ $logs->students }}
@endforeach
@endforeach
这用于显示学生对象,现在它什么都没有,这些是我的模型
class ContactLog extends Model
{
protected $table = 'contactlogs';
public function users()
{
return $this->belongsTo(User::class, 'id');
}
public function students()
{
return $this->belongsTo(Student::Class);
}
}
class Student extends Model
{
protected $table = 'students';
public function user()
{
return $this->belongsTo(User::class);
}
public function contactlogs()
{
return $this->hasMany(ContactLog::class);
}
}
答案 0 :(得分:0)
在控制器中你必须有这样的东西:
@foreach($users as $user)
{{ $user->users->name }}
{{ $user->students->name }}
@endforeach
然后在你看来你必须这样做:
public function yourAction(){
$students = Student::with('users', 'contactlogs')->get();
return view('your.view' , compact('students'));
}
你也可以为学生做同样的事情:
@foreach($students as $student)
{{ $student->users->attr }} //attr is the attribute from users
{{ $student->contactlogs->attr }}// attr is the attribute from contaclogs
@endforeach
与您的观点相同
In [1]: np.may_share_memory(data, data[[1, 3, 4]])
Out[1]: False
答案 1 :(得分:0)
我能够以下列方式显示信息
$student = Student::where('stn', '=', $request->stn)->first();
$log->student_id = $student->id;
它运行搜索stn并返回第一个匹配,这些是唯一的,所以它应该工作,然后它存储ID,在视图中,我只能显示STN而不是ID
答案 2 :(得分:0)
尝试使用contactlogs()而不是contactlogs
@foreach($ users as $ user)
@foreach($user->contactlogs() as $logs)
{{ $logs->students }}
@endforeach
@endforeach