2016-09-08 5 views
0

두 테이블 모두에서 활성 인 역할 (role_id 5 & 6)을 가진 사용자에 대해서만 특정 프로필을 반환하려고합니다. 또한 first_name ASC (사용자 테이블)에서 주문할 수 있으면 좋을 것입니다.Eloquent :: One to One where 두 테이블에서 활성화되어 있지만 특정 역할

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 | 
+------------+---------+------+-------------+-----------+ 

내 사용자 모델

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'); 
} 
} 

내 프로필 모델

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'); 
} 
} 

내 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; 
} 
} 

그래서 난 내 모든 레코드가 잘 받고 있습니다. 모든 프로필을 가져오고 있지만 사용자 테이블에 role_id의 5 & 6에 속하는 프로필은 아닙니다. 또한 사용자 테이블에서 is_active를 0으로 설정하면 여전히 표시됩니다. 그러나 프로필 테이블에 is_active를 설정하면 그렇지 않습니다. 사용자 또는 프로필 테이블에 비활성으로 설정된 행이 있는지 여부를 표시하지 않아야합니다. 사용자를 가질 수는 있지만 활성 프로필을 원하지 않을 수 있기 때문입니다.

답변

0

알았어! 당신이 나뭇 가지에이를 반환하는 방법을 알고 싶은 경우

$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(); 

....

{% 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 %}