[英]Retrieving data from multiple tables
I have Student table in my database which connect many to many with Courses table , when I try to select student from the table ,give me error : "Undefined property: stdClass::$courses (View: C:\\xampp\\htdocs\\laravel\\resources\\views\\admin\\student\\index.blade.php) 我的数据库中有Student表,它与Courses表之间有很多连接,当我尝试从表中选择Student时,给我错误:“未定义的属性:stdClass :: $$ courses(视图:C:\\ xampp \\ htdocs \\ laravel \\资源\\意见\\ ADMIN \\学生\\ index.blade.php)
<td>
@foreach($student->courses as $course)
<label>{{$course->name_courses}}</label>
@endforeach
</td>
edit and update have error: 编辑和更新有错误:
public function edit(Student $student)
{
$id = $student->id;
$students = DB::table('student')
->join('contacts', 'student.contact_id', '=', 'contacts.id')
->join('users', 'contacts.user_id', '=', 'users.id')
->select('contacts.*', 'users.*', 'student.*')
->where(['student.id' => $id])
->first();
$courses = Course::all();
$course2 = array();
foreach ($courses as $course) {
$course2[$course->id] = $course->name;
}
return view('admin.student.edit', ['student' => $students]);
}
function update: 功能更新:
public function update(Request $request, Student $student)
{
Student::updateStudent($request->all());
if (isset($request->courses)) {
$student->courses()->sync($request->courses);
} else {
$student->courses()->sync(array());
}
return redirect("/admin/student")->with('success','Has been Update');
}
in the model Student 在学生模型中
public static function createStudent($data) {
/*
$user = User::create([
'username' => $data['user_name'],
'role_id' => 1,
'password' => Hash::make($data['password']),
]);*/
$user = User::createUser($data) ;
$data['user_id'] = $user['id'];
$contactId = Contact::createContact($data);
$student = Student::create([
'contact_id' => $contactId
]);
return $student;
}
public static function updateStudent($data) {
/* DB::table('users')
->where('id', $data['user_id'])
->update([
'username' => $data['username']
]);*/
User::updateUser($data);
Contact::updateContact($data);
}
public function courses()
{
return $this->belongsToMany('App\Course');
}
Could someone tell me what to do to fix this ? 有人可以告诉我该怎么做才能解决此问题吗?
Looks like you are using Route model binding, and your student is injected, if thats the case you don't need to use DB::table(...
看起来您正在使用Route模型绑定,并且注入了学生,如果是这种情况,则无需使用
DB::table(...
public function edit(Student $student)
{
$courses = Course::all();
$course2 = array();
foreach ($courses as $course) {
$course2[$course->id] = $course->name;
}
return view('admin.student.edit', ['student' => $student]);
}
You have 2 problems in your code: 您的代码中有2个问题:
1- 1-
The below indicates that you're using Query Builder instead of Eloquent 以下内容表明您使用的是查询生成器,而不是雄辩的
$students = DB::table('student')
->join('contacts', 'student.contact_id', '=', 'contacts.id')
->join('users', 'contacts.user_id', '=', 'users.id')
->select('contacts.*', 'users.*', 'student.*')
->where(['student.id' => $id])
->first();
And you didn't join
the courses
table. 而且您没有
join
courses
表。
2- 2-
Query Builder will return Array
, not an object. 查询生成器将返回
Array
,而不是对象。 So to access the course you should do this instead, $course['name_courses']
: 因此,要访问该课程,您应该改为
$course['name_courses']
:
<td>
@foreach($student->courses as $course)
<label>{{$course['name_courses']}}</label>
@endforeach
</td>
To fix the issue, it's easier to do the following: 要解决此问题,执行以下操作会更容易:
$students = Student::with('courses', 'contacts, 'contacts.users')
->where('id', '=', $id) //the 'id' here refers to 'student' ID.
->first();
and then you can loop over the courses that belong to this particular student. 然后您可以遍历该特定学生的课程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.