Php 获取透视表laravel的关系值
型号上市交易Php 获取透视表laravel的关系值,php,laravel,laravel-5,Php,Laravel,Laravel 5,型号上市交易 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'];
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');
}
id
list_id
deal_id
型号列表
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');
}
id
list_id
deal_id
数据透视表列表\u有\u交易
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');
}
id
list_id
deal_id
控制器主页控制器
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中查看与交易相同类别的列表,但我不想这样做,我尝试使用withPivot('list_id'),但我没有获取列表的id,谢谢您的帮助。查看检索中间表列的部分
请不要只转储所有代码。您应该包含一个。我已经包含了解决方案的相关代码。是的,我已经检查过了,但示例是$user=App\user::find(1);foreach($user->roles as$role){echo$role->pivot->created_at;}如果找到具有手动编号的用户,我希望在表中找到具有列表id的用户