2016-02-08 12 views
11

私はtimesheetテーブルとuserテーブルをデータベースに持っています。 timesheetモデルで次の関係が設定されています。結果にユーザーオブジェクトをロードしますLaravel 5.1選択した行に関係データを読み込む

$this->timesheet->whereStatus('Approved')->with('user'); 

これと配列に変換場合は以下となります。私は私のようなものを使用して、データベースからタイムシートを選択すると、ユーザデータを取得することができ

/** 
* The user that owns the timesheet. 
* 
* @return Object 
*/ 
public function user() 
{ 
    return $this->belongsTo('App\Models\User\User'); 
} 

上記の手段そうそうだ。

0 => array:13 [▼ 
    "id" => 1 
    "user_id" => 2 
    "week" => 1 
    "year" => 2016 
    "week_ending" => "Sunday 10th January 2016" 
    "total_hours" => "45.00" 
    "token" => "0e6796a2dc68066c8d36ff828c519af00657db02b733309b8a4ac0f7b5d6a385" 
    "status" => "Approved" 
    "supervisor_id" => 1 
    "approved_by" => 1 
    "created_at" => "2016-01-13 15:42:49" 
    "updated_at" => "2016-01-14 14:52:07" 
    "user" => array:7 [▼ 
     "id" => 2 
     "first_name" => "Bill" 
     "last_name" => "Andrews" 
     "email" => "[email protected]" 
     "status" => 1 
     "created_at" => "2016-01-13 15:38:18" 
     "updated_at" => "2016-01-14 14:50:03" 
    ] 
    ] 

しかし、私は、ユーザーテーブルからfirst_namelast_nameを必要としています。配列usertimesheetとマージ/フラット化する方法はありますか?

0 => array:14 [▼ 
    "id" => 1 
    "user_id" => 2 
    "week" => 1 
    "year" => 2016 
    "week_ending" => "Sunday 10th January 2016" 
    "total_hours" => "45.00" 
    "token" => "0e6796a2dc68066c8d36ff828c519af00657db02b733309b8a4ac0f7b5d6a385" 
    "status" => "Approved" 
    "supervisor_id" => 1 
    "approved_by" => 1 
    "created_at" => "2016-01-13 15:42:49" 
    "updated_at" => "2016-01-14 14:52:07" 
    "first_name" => "Bill" 
    "last_name" => "Andrews" 
    ] 

私はこのように熱心な負荷をかけようとしました。

$this->timesheet->with(['user' => function ($query) { 
    $query->select('first_name', 'last_name'); 
}])->get()->toArray(); 

ただし、次のように出力されます。

array:126 [▼ 
    0 => array:13 [▼ 
    "id" => 1 
    "user_id" => 2 
    "week" => 1 
    "year" => 2016 
    "week_ending" => "Sunday 10th January 2016" 
    "total_hours" => "45.00" 
    "token" => "0e6796a2dc68066c8d36ff828c519af00657db02b733309b8a4ac0f7b5d6a385" 
    "status" => "Approved" 
    "supervisor_id" => 1 
    "approved_by" => 1 
    "created_at" => "2016-01-13 15:42:49" 
    "updated_at" => "2016-01-14 14:52:07" 
    "user" => null 
    ] 

答えて

7

2番目の例でユーザー関係がnullである理由は、Eloquentリレーションシップが機能するために、リレーションシップを結ぶキーが必要なためです。言い換えれば...

  1. timesheetを取得します。
  2. userfirst_namelast_nameです。
  3. 関係を構築します。
  4. ユーザーIDを取得していないため、user's idtimesheet's user_idが一致しないため、関係を構築できません。あなたのクエリが動作するためには

、あなたはこのようにそれを調整する必要があります:あなたが平坦化された結果をしたい場合は邪魔にそれと

$this->timesheet->with(['user' => function ($query) { 
    $query->select('id', 'first_name', 'last_name'); 
}])->get()->toArray(); 

、私はそれがjoinsを使用するのが最善だと思います熱心なローディングの性質のために熱心なローディングではなく、

$this->timesheet 
    ->join('users', 'timesheets.user_id', '=', 'users.id') 
    ->select('timesheets.*', 'users.first_name', 'users.last_name') 
    ->get()->toArray(); 

これは、あなたのテーブル名がuserstimesheetsであることを前提としています。

+0

助けてくれてありがとう、これは私が後にした情報です。私は9時間で奨励金を授与する(それまで私が賞を授けることはできないと言っている)。 – V4n1ll4

2

あなたが更新あなたはまた、関連熱心な負荷に積極的なロードを実行することができ リスト

->lists('first_name', 'last_name'); 

またはあなたが選択

->select('first_name', 'last_name')->get() 

を実行したい場合を取得しようとしたことがありオブジェクト。ここに例があります

$users = App\User::with(['posts' => function ($query) { 
    $query->select('first_name', 'last_name'); 

}])->get(); 

私はそれが役立つかどうかお知らせください。

+0

私があなたの質問に正しく答えたと思うなら、これを次の人の正しい答えとしてください。ありがとう! – Gagan

+0

私は$行リスト( 'week_ending'、 'user.first_name'、 'user.last_name'、 'created_at'、 'total_hours'、 'status')を試しました;これは必要な列のリストです'' Bill "=>" 2016年1月10日日曜日 "' – V4n1ll4

+0

上記の出力を参照してください(元の投稿を更新しました)。 – V4n1ll4

-1

この場合、Raw SQLまたはQuery Builderを使用する方がよいでしょう。 データベース・データをリレーショナル・オブジェクトとして使用するために作成された関係です。

1

これはまさにjoinの機能です。

From the documentation

クエリビルダはまた、結合文書き込むために使用することができます。 に基本SQL「内部結合」を実行するには、 クエリビルダーインスタンスで結合メソッドを使用できます。結合メソッド に渡される最初の引数は、結合する必要があるテーブルの名前です。残りの 引数は、結合の列制約を指定します。

$this->timesheet 
     ->leftjoin('user', 'user.id', '=','timesheet.user_id') 
     ->get() 
     ->toArray(); 

あなたがフィールド上より選択的になりたい場合は、あなたは何select選択することができます。

$this->timesheet 
     ->leftjoin('user', 'user.id', '=','timesheet.user_id') 
     ->select('timesheet.*', 'user.first_name', 'user.last_name') 
     ->get() 
     ->toArray(); 

他が提案してきたように、それはのような

this->timesheet = DB::table('timesheet') 
       ->where('timesheet.status', 'Approved') 
       ->leftjoin('user', 'user.id', '=','timesheet.user_id') 
       ->select('timesheet.*', 'user.first_name', 'user.last_name') 
       ->get() 
       ->toArray(); 
2

Laravelモデルは属性を設定/取得する前にデータを変更する方法を持っているDBクエリビルダを使用する方がよいかもしれません。実際にゲッター関数を定義することによって、モデルに属性を追加することができます。これにより、user_idまたはstatusと同じ方法でユーザー名を参照できます。これらの関数は、ビューまたはサニタイズフォーム入力の日付形式を変更する場合にも便利です。

/** 
* Get the first name of the user. 
* 
* @return string 
*/ 
public function getFirstNameAttribute() 
{ 
    return $this->user->first_name; 
} 

/** 
* Get the last name of the user. 
* 
* @return string 
*/ 
public function getLastNameAttribute() 
{ 
    return $this->user->last_name; 
} 
関連する問題