Modell ListDealsErhalten Wert der Beziehung von Pivot-Tabelle 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');
}
Modell Listen
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');
}
Pivot-Tabelle list_has_deals
id
list_id
deal_id
Controller-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);
}
}
mag ich für das Geschäft in dem HomePageController der Kategorien der Liste in der Ansicht bekommen, aber ich habe nicht kwno thw Art und Weise, ich mit withPivot versuchen ('list_id'), aber i dont Holen Sie sich die ID der Liste, danke für die Hilfe.
Bitte werfen Sie den Code nicht nur alle. Sie sollten ein [minimales, praktikables, vollständiges Beispiel] (https://stackoverflow.com/help/mcve) einfügen. – Styphon
Ich habe bereits den relevanten Code für die Lösung enthalten. –