Relationship of tables (Laravel API)

Asked

Viewed 180 times

1

I am creating a PHP API using Laravel, but I could not make the relationship between two tables.

User Table(Loginusers)>User Behavior(Behaviors)

. I have already created the GET of Loginuser and Behavior separately, but I would like that when I make a user request (Loginuser) bring his behaviors (Behavior), and not only the user as it is being done today. The Behaviors table, has the fk login_user_id. Can anyone help me? I imagine it’s in Controller, but I can’t 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);
    }

}

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 specifically use Eloquent’s native functions for the queries instead of making these selects on the models. Example: Instead of adding the method Getresults, Voce can do this:

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

Here explains more: https://laravel.com/docs/5.6/eloquent#retrieving-models

Browser other questions tagged

You are not signed in. Login or sign up in order to post.