简体   繁体   中英

Retrieve data through user_Id

I'm building an API of a profile. That is my profile controller and I want to retrieve data from the User with the Profile. I'm using OneToOne relationships. I want to retrieve data for the single user but as you can see it will give me the data of total users so how can I retrieve of the related user based on id or profile id.

profileController extends Controller
{
    public function setting(){
       $profile=User::with('profile')->get();
       return $profile; `enter code here`
    }
}`
profileController extends Controller
{
  public function setting(){
    $profile=User::with('profile')->first(); // or find($id);
    return $profile; `enter code here`
  }
}

you can use find() or first() method to get only one user. cek here

profileController extends Controller
{
    public function setting(){
       $profile=User::with('profile')->where('user_id',$user_id)->first();
       return $profile; `enter code here`
    }

user where() and first()

you can try this:

profileController extends Controller
{
   public function setting(){
     $profile=User::table('profile')->where('user_id',$user_id)->get();
   return view('profile_page',['profile' => $profile]);

   // or
   return view('profile_page')->with('profile' => $profile);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM