2017-11-15 8 views
0

私はこのテーブルの構造を持っているが、projectrewards and shippingは、ピボットテーブルreward_shipmany to many関係を持って、rewardsone to many関係を持っています。Laravelモデル

projects  rewards   shipping  reward_ship 
---------  --------  --------  ------------ 
id    id    id    id 
title   amount   location  reward_id 
amount   project_id  name   ship_id 

私は1つのクエリ内の他のすべての仲間のテーブルデータ1つの特定のproject詳細(reward_shipテーブルを使用してrewards and shippingデータ)を抽出しようとしています。

これらは、私はそれが動作しません

プロジェクトモデル

class Rewards extends Model { 
     public function projs(){ 
       return $this->hasMany('App\Rewards'); 
      } 
     public function rewds(){ 
       return $this->belongsToMany('App\Shipping') 
       ->withPivot('reward_ship', 'ship_id', 'reward_id'); 
     } 
     public function shiplc(){ 
       return $this->belongsToMany('App\Rewards') 
       ->withPivot('reward_ship', 'ship_id', 'reward_id'); 
     } 
     } 
class Rewards extends Model { 
    public function proj() { 
     return $this->belongsTo('App\Projects'); 
    } 
} 

コントローラのAPIクラス

Route::get('projects/{id}', function($id) { 
$p = Projects::find($id); 
$getd = Rewards::with('proj') 
    ->where('rewards.project_id', '=', $p->id) 
    ->get(); 
}); 

をしようとしている方法です。 larvelで多くの関連モデルベースクエリを検索して試しました。 私の実装が間違っていることを知っています。私は解決するように提案してください。

+0

シングルクエリではどういう意味ですか? –

+0

私は報酬でプロジェクトを抽出し、データを発送することを意味します –

+0

これを確認してくださいhttps://stackoverflow.com/questions/23788844/hasmanythrough-with-one-to-many-relationship/23789210#23789210 –

答えて

1

あなたが持っている関係を修正する必要があります。

プロジェクトモデル:

public function rewards(){ 
    return $this->hasMany('App\Rewards'); 
} 

報酬モデル:

public function projects() { 
    return $this->belongsTo('App\Projects'); 
} 

public function shippings(){ 
    return $this->belongsToMany('App\Shipping','reward_ship', 'reward_id', 'ship_id'); 
} 

送料モデル:

$project = Projects::with('rewards.shippings') 
        ->where('id', $project_id) 
        ->get(); 

とビューあなたの中に:

public function rewards(){ 
    return $this->belongsToMany('App\Rewards','reward_ship', 'ship_id', 'reward_id'); 
} 

あなたはこのような希望の要素熱心な負荷にコントローラ内の関係を呼び出すことができますその後報酬の上をループして、このようなshippingsを得ることができます:

@foreach ($project->rewards as $reward) 
    <p>This is a reword {{ $reward->amount }}</p> 
    @foreach ($reward->shippings as $shipping) 
     <p>This is a shipping {{ $shipping->name }}</p> 
    @endforeach 
@endforeach 
+0

@Raikumarはあなたのために働いていますか? – Maraboc

+0

ありがとうございます! @Marabocそれは働いた!私の問題を解決するだけでなく、私はlaravelモデルで新しいことを学びました。 –

+1

すべての答えが良いです!非常に役立ちます。私はあなたを受け入れます。 –

1
class Project extends Model 
{ 
    public function rewds() 
    { 
     return $this->hasMany('App\Rewards'); 
    } 

    public function shiplc() 
    { 
     return $this->hasManyThrough('App\Shipping', 'App\Rewards'); 
    } 
} 

class Rewards extends Model 
{ 
    public function shiplc() 
    { 
     return $this->belongsToMany('App\Shipping'); 

    } 

    public function projs() 
    { 
     return $this->belongsTo('App\Project'); 
    } 

} 

class Shipping extends Model 
{ 
    public function shiplc() 
    { 
     return $this->belongsToMany('App\Shipping'); 

    } 
} 

Route::get('projects/{id}', function($id) { 
    $p = Projects::with(['rewds', 'shiplc'])->find($id); 
}); 
1

あなたはLaravel 5.5新機能API Resourcesを使用することができます。

モデルやコレクションなどのオブジェクトの出力を書式設定し、属性や関係を表示するのに役立ちます。

だから、あなたはあなたのItemResourceにこのような何かを行うことができます:

<?php 

    namespace App\Http\Resources; 

    use Illuminate\Http\Resources\Json\Resource; 

    class Project extends Resource 
    { 
     /** 
     * Transform the resource into an array. 
     * 
     * @param \Illuminate\Http\Request 
     * @return array 
     */ 
     public function toArray($request) 
     { 
      return [ 
       'project_id' => $this->project_id, 
       'title' => $this->title, 
       'amount' => $this->amount, 
       // To access relationship attributes: 
       'rewards' => $this->rewards->load('shippings'), 
      ]; 
     } 
    } 

を次に、あなたのコントローラで、あなただけの新しいリソースのインスタンスを作成し、返したいアイテムオブジェクトを渡す必要があります:

use App\Http\Resources\Project as ProjectResource; 

// some code 

    /** 
    * Show a single formatted resource. 
    * 
    * @param Project $project 
    * @return ProjectResource 
    */ 
    public function show($project) 
    { 
     return new ProjectResource($project); 
    } 

// the rest of your code 

出力は期待どおりです。

1

Project.php

class Project extends Model { 
    public function rewards() { 
     return this->hasMany(Reward::class, 'project_id', 'id'); 
    } 
} 

報酬。PHP

class Reward extends Shipping { 
    public function shipping(){ 
     return $this->belongsToMany(Shipping::class, 'reward_ship', 'reward_id', 'ship_id'); 
    } 

    public function project(){ 
     return $this->belongsTo(Project::class); 
    } 
} 

あなたはこのようにそれを取得することができます。

$projectDetails = Project::where('id', $projectId) 
          ->with(['rewards', 'rewards.shipping'])->get(); 
+0

ありがとう、それは非常に便利です。 –