2016-09-08 13 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を設定しても、そうではありません。 UserまたはProfileテーブルに非アクティブに設定された行があるかどうかを表示しないようにする必要があります。ユーザーがいる可能性がありますが、アクティブなプロファイルが必要ない可能性があるためです。

答えて

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