I have three table: user, products and orders. A user can subscribe/unsubscribe to multiple products, each subscription is stored in the orders table which has user_id and product_id fields.
Following are my relationships in user Model:
public function orders()
{
return $this->hasMany('App\Order');
}
public function userOrders()
{
return $this->belongsToMany(Product::class, 'orders');
}
Products Model:
public function orders()
{
return $this->belongsTo('App\Order');
}
Orders Model:
public function user()
{
return $this->belongsTo('App\User');
}
public function products()
{
return $this->hasMany('App\Product');
}
In my HomeController I am passing the values to a view:
public function index()
{
$user = Auth::user()->id;
$products = Product::all();
$orders = Order::where('user_id', Auth::user()->id)->get();
// dd($orders);
return view('home')->withOrders($orders)->withProducts($products)->withUser($user);
}
In my view I want to show the list of all the products to which the user has "subscribed" all of these products are in my orders pivot table, I can get the user_id and product_id to show in my view but I am utterly confused on how to show the product name and other details as well. The home view is supposed to show All the products to which the logged in user has subscribed. Here is my view:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<h2>Your subscribed Courses:</h2>
@foreach ($orders as $order)
sdfdfd
<p>{{$order}}</p>
@endforeach
</div>
</div>
</div>
</div>
</div>
@endsection
Any help much appreciated guys.