Search code examples
phpmysqleloquentslim-3

Eloquent :: One to One where active in both tables, but specific roles


I am trying to only return specific profiles for users that have roles (role_id 5 & 6) that are active in both tables. It would also be nice if I can order by first_name ASC as well (user table).

user
+---------+---------+-------------+-----------+
| user_id | role_id | first_name  | is_active |
+---------+---------+-------------+-----------+
|       1 |       5 | Dan         |         1 |
|       2 |       6 | Bob         |         0 |
+---------+---------+-------------+-----------+

profile
+------------+---------+------+-------------+-----------+
| profile_id | user_id | bio  |   avatar    | is_active |
+------------+---------+------+-------------+-----------+
|          1 |       1 | text | example.jpg |         1 |
|          2 |       2 | text | noimage.gif |         1 |
+------------+---------+------+-------------+-----------+

My user model

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class User extends Model{

protected $table = 'user';
protected $primaryKey = 'user_id';

protected $fillable = [
    'role_id',
    'first_name',
    'is_active'
];

public function scopeActive(){
    return $this->where('is_active', '=', 1);
}

public function role(){
    return $this->belongsTo('App\Model\Role');
}

public function profile(){
    return $this->hasOne('App\Model\Profile');
}
}

My profile model

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model{

protected $table = 'profile';
protected $primaryKey = 'profile_id';

protected $fillable = [
    'user_id',
    'avatar',
    'is_active'
];

public function scopeActive(){
    return $this->where('is_active', '=', 1);
}

public function user(){
    return $this->belongsTo('App\Model\User');
}
}

My UserController

namespace App\Controller\User;

use App\Model\User;
use App\Model\Profile;
use App\Controller\Controller;

final class UserController extends Controller{

public function listExpert($request, $response){

    $user = User::active()->whereIn('role_id', array(5, 6))->orderBy('first_name', 'asc')->get();
    $profile = $user->profile ?: new Profile;
    $data['experts'] = $profile->active()->get();

    $this->view->render($response, '/Frontend/experts.twig', $data);
    return $response;
}
}

So I am getting all of my records just fine. I am getting all the profiles but not the ones that belong only to role_id's 5 & 6 in the user table. Also if I set is_active to 0 in the user table, they still show. But if I set is_active in the profile table they do not. I need them to not show whether the User or Profile table has those rows set to inactive. Because you can have a user but they may not want an active profile.


Solution

  • Okay I got it!

    $data['experts'] = User::whereIn('role_id', array(5, 6))
            ->where('is_active', '1')
            ->whereHas('profile', function($q){
                $q->where('is_active', '1');
            })
            ->with('profile')
            ->orderBy('first_name', 'ASC')
            ->get();
    

    In case you want to know how to return this in twig....

    {% if experts|length > 0 %}
        <ul>
        {% for item in experts %}
            <li>First Name: {{ item.first_name }}, Bio: {{ item.profile.bio }}, Avatar: {{ item.profile.avatar }}</li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No records found.</p>
    {% endif %}