如何修复模型之间的关系错误?

I have set up two models and made the relationship between them. I want to pass the attributes of the user as well as the user_detail.

I have used a similar code somewhere and it worked perfectly. But it is not working here.

//This is the function in "User.php" model.
public function user_detail(){
    return $this->hasOne('App\Profile');
}

//This is the function in "Profile.php" model.
public function user(){
    return $this->belongsTo('App\User');
}

//edit function in ProfileController
public function edit($id)
{
    $user=User::find($id);
    return view('profile.edit')->with('data',$user->user_detail);
}

When I click the edit button in the view, I expect the extract all the details from user table as well as from user_detail table.

try using where instead of find and then use with:

$user = User::where('id', $id)->with('user_detail')->first();
return view('profile.edit')->with('data', $user);

In your model:

public function user_detail(){
    return $this->hasOne('App\Profile', 'student_no');
}

I think you should edit your this code a little bit

public function edit($id)
{
    $user=User::findOrFail($id);
    return view('profile.edit')->with('data',$user);
}

And in your blade file (profile.edit), You can get all details from User and Profile Model.

{{ $data->id }}
{{ $data->user_detail->YOURPARAMETERS }}

The problem is with the relationship naming. Make it camelCase like,

//This is the function in "User.php" model.
public function userDetail(){
    return $this->hasOne('App\Profile');
}

//edit function in ProfileController
public function edit($id)
{
    $user=User::find($id);
    return view('profile.edit')->with('data',$user->userDetail);
}

Reference: https://github.com/laravel/framework/issues/4307#issuecomment-42037712