从多个表中检索数据

时间:2018-11-28 15:51:09

标签: php database laravel many-to-many

我的数据库中有一个Student表,它与Courses表连接很多,当我尝试从表中选择Student时,给我错误: “未定义的属性:stdClass :: $ courses(视图:C:\ xampp \ htdocs \ laravel \ resources \ views \ admin \ student \ index.blade.php)

              <td>
               @foreach($student->courses as $course)
                     <label>{{$course->name_courses}}</label>
               @endforeach                
              </td> 

编辑和更新有错误:

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

功能更新:

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

在学生模型中

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

}

有人可以告诉我该怎么做吗?

2 个答案:

答案 0 :(得分:0)

看起来您正在使用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]);
}

答案 1 :(得分:0)

您的代码有 2 个问题:

1-

以下内容表明您使用的是查询生成器,而不是雄辩的

$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();

您没有join courses表。

2-

查询生成器将返回Array,而不是对象。因此,要访问该课程,您应该改为$course['name_courses']

<td>
    @foreach($student->courses as $course)
        <label>{{$course['name_courses']}}</label>
    @endforeach                
</td>

要解决此问题,执行以下操作会更容易:

$students = Student::with('courses', 'contacts, 'contacts.users')
    ->where('id', '=', $id) //the 'id' here refers to 'student' ID.
    ->first();

然后您可以遍历该特定学生的课程。