Table Relationship (Laravel API)

1

I am creating an API in PHP using Laravel, but I could not do the relationship between two tables.

  

User Table (LoginUsers) & Behaviors

. I have already created the LoginUser and Behavior GET separately, but I would like when I make a user request (LoginUser) to bring its Behavior behavior, not just the user as it is being done today. The Behaviors table has fk login_user_id . Can someone help me? I imagine it's in the Controller, but I'm not sure how to do it.

/

    /Model: LoginUser
    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class LoginUser extends Model
    {
        protected $fillable = ["status_id","access_group_id","first_name","last_name","email","password","agreement","ip_user","timezone"];

    public function getResults($login_user_id)
    {
        return $this->select(
                        'status_id',
                        'access_group_id',
                        'login_user_id',
                        'first_name',
                        'last_name'
                    )
                    ->where('login_user_id','=',"{$login_user_id}")
                    ->get();
    }

    public function Behaviors(){
        return $this->belongsTo('App\Models\Behavior','login_user_id');
    }

//Controller: LoginUserController
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\LoginUser;

class LoginUserController extends Controller
{



    private $loginuser;

    public function __construct(LoginUser $loginuser)
    {
        $this->loginuser = $loginuser;
    }

    public function show(LoginUser $loginuser, Request $request)
    {
        $loginusers = $this->loginuser->getResults($request->login_user_id);
        return response()->json($loginusers);
    }



    }

}


//Model: Behavior

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Behavior extends Model
{
    //protected $fillable = [];

    public function getResults($login_user_id)
    {

        if(!$login_user_id)
            return $this->get();

        return $this->select(
                        'behavior_id',
                        'behaviors.key_behavior_id',
                        'lkp_key_behaviors.key_behavior',
                        'login_user_id'
                    )
                    ->join('lkp_key_behaviors','behaviors.key_behavior_id','=','lkp_key_behaviors.key_behavior_id')
                    ->where('login_user_id','=',"{$login_user_id}")
                    ->get();
    }

    public function LkpKeyBehaviors(){
        return $this->hasMany('App\Models\LkpKeyBehavior','key_behavior_id');
    }

    public function LoginUsers(){
        return $this->hasMany('App\Models\LoginUser','login_user_id');
    }
}
Controller: behaviorController

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Behavior;

class BehaviorController extends Controller
{

    private $behavior;

    public function __construct(Behavior $behavior)
    {
        $this->behavior = $behavior;
    }

    public function index(Behavior $behavior, Request $request)
    {
        $behaviors = $this->behavior->getResults($request->login_user_id);
        return response()->json($behaviors);
    }

}
    
asked by anonymous 31.08.2018 / 20:01

1 answer

1

Hello, When you return an object with the LoginUsers records, if your relationship is well done, you can use something like this:

$loginusers[0]->behavior

And I would particularly use the native Eloquent functions for the queries instead of making those selects in the models. Example: Instead of adding the GetResults method, you can do this:

loginusers = $this->loginuser::where('login_user_id',$request->login_user_id)->get();

Here it explains more: link

    
03.09.2018 / 06:31