Do you have a Subcategory model with relationships on Category and Product, and do you have a Brand model setup too?
Do the Products only belong to one Category and one Subcategory?
Can you show the store method on Products?
Hello .. I have the following
My products object
class Product extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function brands()
{
return $this->belongsTo('App\Brand');
}
public function categories()
{
return $this->belongsTo('App\Category');
}
public function subcategories()
{
return $this->belongsTo('App\Subcategory');
}
protected $table = 'products';
protected $fillable = ['id', 'title','quantity','price','brand','category','subcategory','created_by','created_at','updated_at'];
}
My Category Model
class Category extends Model
{
public function products()
{
return $this->hasMany('App\Product');
}
public function subcategories()
{
return $this->hasMany('App\Subcategory');
}
protected $table = 'categories';
protected $fillable = ['id', 'title'];
public $timestamps=false;
public $incrementing=true;
}
My View
@if(sizeof($products)>0)
<div class="card-content table-responsive">
<table class="table">
<thead class="text-success">
<th>ID</th>
<th>TITLE</th>
<th>QUANTITY</th>
<th>PRICE</th>
<th>CATEGORY</th>
<th>SUBCATEGORY</th>
<th>BRAND</th>
<th></th>
<th></th>
<th></th>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<td>{{ $product['id'] }}</td>
<td>{{ $product['title'] }}</td>
<td>{{ $product['quantity']." Units" }}</td>
<td>{{ "K". $product['price'] }}</td>
<td>{{ $product->category }}</td> // <<<< CATEGORY
<td>{{ $product->subcategory }}</td>
<td>{{ $product->brand }}</td>
<td><a href="{{url('/account/stock/edit/')}}/{{ $product['id'] }}"><i class="material-icons text-gray">edit</i></a></td>
<td><a href="{{url('/account/stock/update/')}}/{{ $product['id'] }}"><i class="material-icons text-gray">autorenew</i></a></td>
<td><a href="{{url('/account/stock/delete/')}}/{{ $product['id'] }}"><i class="material-icons text-gray">delete_forever</i></a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div align="center"> {!! $products->render() !!} </div>
@else
<div class="card-content">
<h2>No products were found in the system</h2>
</div>
@endif
My Controller
//DATA BEING SENT TO THE CONTROLLER
public function index()
{
$products=Product::orderBy('title')->paginate(10);
$products->setPath('view');
return view('stock/view')->with('products',$products);
}
What am i doing wrong .. Dont get me wrong, i know about database concepts and how relationships work in general, maybe im not doing it correctly according to laravel, please help .. "Thank you's" later .. :)
Please or to participate in this conversation.