2017-06-12 44 views
-1

模型ListDeals獲取透視表laravel的關係的值

class ListsDeals extends Model 
{ 
protected $table = "deals"; 

protected $fillable = ['title', 'slug', 'description', 'price', 'has_discount', 'price_to_discount', 'price_reduced', 'status', 'approved', 'suspended', 'start_date', 'end_date']; 

public function lists() 
{ 
    return $this->belongsToMany('App\Models\Lists', 'list_has_deals' , 'deal_id', 'list_id')->withPivot('list_id'); 
} 

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

模型列出

class Lists extends Model 
{ 

protected $table = "lists"; 

protected $fillable = ['title', 'slug', 'short_description', 'description', 'website', 'email', 'phone', 'lat_map', 'lng_map', 'address_reference', 'video', 'renewal_date', 'status', 'approved', 'suspended']; 

public function categories() 
{ 
    return $this->belongsToMany('App\Models\ListsCategories', 'list_has_categories' , 'list_id', 'category_id'); 
} 

public function deals() 
{ 
    return $this->belongsToMany('App\Models\ListsDeals', 'list_has_deals' , 'list_id', 'deal_id'); 
} 

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

透視表list_has_deals

id 
list_id 
deal_id 

控制器HomePageController

namespace App\Http\Controllers; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use Auth; 
use App\Models\User; 
use App\Models\Lists; 
use App\Models\ListsCategories; 
use App\Models\ListsDeals; 
use DB; 

class HomePageController extends Controller 
{ 

public function homepage(){ 

    $matchThese = [ 'suspended' => 0, 'status' => 1, 'approved' => 1 ]; 

    $deals = ListsDeals::where($matchThese)->limit(3)->offset(0)->orderBy('start_date')->get(); 

    return view("homepage") 
      ->with("deals", $deals); 

} 

} 

我想在視圖中得到相同的類別列表中HomePageController交易的,但我不kwno THW辦法,我嘗試用withPivot(「LIST_ID」),但我不獲取列表的ID,感謝您的幫助。

+0

請不要隨便傾倒所有的代碼。你應該包含一個[最小的,可行的,完整的例子](https://stackoverflow.com/help/mcve)。 – Styphon

+0

我已經包含解決方案的相關代碼。 –

回答

相關問題