# Associations when accessing another controller

**URL:** <https://discourse.cakephp.org/t/associations-when-accessing-another-controller/6888>\
**Category:** Need Help\
**Created:** [November 15, 2019, 9:27am UTC](https://discourse.cakephp.org/t/associations-when-accessing-another-controller/6888 "2019-11-15T09:27:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Aman](https://discourse.cakephp.org/u/Aman)\
**Post date:** [November 15, 2019, 9:27am UTC](https://discourse.cakephp.org/t/associations-when-accessing-another-controller/6888/1 "2019-11-15T09:27:12Z")

</div>

Good afternoon!  
 lpu = this-\> findByHospital\_id ( id, [ 'hospitals.lpu' =\> intval ( uid\_lpu),  
]  
);  
The first table “Hospitals” contains: ID, lpu and description.  
The second Blood table contains Hospital\_id  
Please tell me how in the second controller I want to determine the id of the first (intval(uid\_lpu)) - this is the value obtained by the post method)  
The bottom line is that I need to find the id of the second table (via the condition)

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [November 16, 2019, 10:09pm UTC](https://discourse.cakephp.org/t/associations-when-accessing-another-controller/6888/2 "2019-11-16T22:09:40Z")

</div>

Well first get some authentication and authorization setup. Next setup relations: [https://book.cakephp.org/3/en/orm/associations.html](https://book.cakephp.org/3/en/orm/associations.html)

Or use a query similar to:

### Not cake example but you should get the idea:

```auto
    public function ownerWithPets()
    {
        $ownerid = $request->input('ownerid'); // passed from search
        $owner = Powner::find($ownerid); // find owner (hospital)
        $pets = DB::table('pets') // child table
                        ->where('ownerid', '=', $ownerid)
                        ->orderBy('petname', 'asc')->get();
        //print_r($pets);
        return view('owner/ownerpet')->with(compact('owner', 'pets'));
    }

```

#### view

```auto
<!DOCTYPE html>

<html>
    <head>
        <title>whatever</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div><?php echo $owner->oname; ?> // the petowner (hospital)
            <?php echo "<br>"; ?>
            <?php echo "======================"; ?>
        </div>
        <div>
            <?php foreach ($pets as $pet) { ?>
            <?php echo $pet->petname; ?> // Child table data
                <?php echo "<br>"; ?>
            <?php } ?>
        </div>
    </body>
</html>

```

Just a very simplified example here. Use whatever layout, template, etc as needed.  
Don’t forget to validate, sanitize the data.
