Can you help me again? I have 3 tables
Product Table
id
product_name
Productquantity Table
id
item_id
price
Orders
id
req_id
quantity_id
quantity
amount
How can I get the product name in my CartView? I only have this in my code
Order Model
public function product()
{
return $this->belongsTO('App\Productquantity', 'item_id', 'id');
}
My Controller
$dataReqorder = Reqorder::where('req_id','=', $shoppingId)->with('product')->get();
My View
#foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
<td> {{$order->product->prod_id}} </td>
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
#endforeach
I want this {{$order->product->prod_id}} should be the name of the product instead of the product ID.. I just do not know how to do it on model and controller.. Please help
I think you need to reorder some things
// Reorder tables and models
Table Schema and models
Product Model (Table name => 'products')
id
product_name
OrderProduct (Table name => 'order_product')
id
product_id
order_id
price
quantity_id
quantity
Order Model (Table name => 'orders')
id
req_id
amount
// Product Model
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('price', 'quantity_id','quantity');
}
// Order Model
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('price', 'quantity_id','quantity');
}
// Controller
$dataReqorder = Order::where('req_id','=', $shoppingId)->with('products')->get();
// View
#foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
#foreach($order->products as $product)
{{$product->id}}<br>
{{$product->pivot->price}}<br>
{{$product->pivot->quantity_id}}<br>
{{$product->pivot->quantity}}<br>
#endforeach
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
#endforeach
For more info I recommend you to see the documentation
https://laravel.com/docs/5.6/eloquent-relationships#many-to-many
Related
I am new to laravel & want to implement eloquent relationship.
Let me explain.
Consider I have 2 tables
products
product_id
product_name
brand_id
price
brands
id
brand_name
Each product will have one brand Id.But in Brands table, there is no product id. One brand_id can be in multiple product rows, and one product has one brand_id only. I want to select some col from products table plus brand_name with respect to brand_id of products table using Model.SO in Product model I wrote:
public function brands()
{
return $this->hasOne('App\Brand','product_id');
}
and in Brand model I write:
public function products()
{
return $this->belongsTo('App\Product','brand_id');
}
Now I want the result:
product_name
price
brand_name
How can I fetch those data in controller using eloquent relation? Also, the way I wrote Model relationship, Is it ok??
Your Product Model relation will be below
public function brand(){
return $this->belongsTo('App\Brand','brand_id');
}
public function product(){
return $this->belongsTo('App\Product','product_id');
}
Now in controller you can add query as below.
$products = Product::with('brand','product')->get();
echo '<pre>'
print_r($products->toArray());
exit;
It looks to me like you want a one-to-many relationship, so one brand can have many products and many products belong to one brand.
Product model:
public function brand()
{
return $this->belongsTo('App\Brand');
}
Brand model:
public function products()
{
return $this->hasMany('App\Product');
}
Then you would be able to get the brand information like this:
The full brand model:
Product::first()->brand
The brand name:
Product::first()->brand->brand_name
From the docs:
A one-to-many relationship is used to define relationships where a
single model owns any amount of other models. For example, a blog post
may have an infinite number of comments.
P.S.:
Your table column names do not make much sense to me, why do you have product_id on products but then on brands it is just called id? Why do you have product_name but then just price? why is it not just name or product_price, so your at least consistent?
Help me again,
I have 3 tables
Product Table
id
product_name
Productquantity Table
id
prod_id -> product table id
price
Purchaserecord
id
prodquantityid -> productquantity table id
quantity
amount
How can I make to display the name of the product in my view? I have this on my Purchaserecord model
public function productquantity()
{
return $this->belongsTO('App\Productquantity', 'prodquantityid', 'id');
}
and in my view
#foreach($dataPurchaseRecord as $Record)
<tr>
<td>{{$Record->productquantity->prod_id}}</td>
<td>{{$Record->quantity}}</td>
<td>{{$Record->price}}</td>
</tr>
#endforeach
I just want to know how to display the product name instead of the productid {{$Record->productquantity->prod_id}}
Thank you
In your controller, try this:
function viewPurchases(Request $request)
{
//Perform your query, and end on ->get() to retreive iterable collection.
$dataPurchaseRecord = Purchaserecord
::with('productquantity')
->where('id', '<', 10)
->get();
//load your view, and pass the collection to your view.
//compact('dataPurchaseRecord') basically results in a key value array like so:
//['dataPurchaseRecord' => $dataPurchaseRecord]
return view('your_viewname', compact('dataPurchaseRecord'));
}
Hope it helps :)
Can you help me again? I have 3 tables
Product Table
id
product_name
Productquantity Table
id
item_id
price
Orders
id
req_id
quantity_id
quantity
amount
How can I get the product name in my CartView? I only have this in my code
Reqorder Model
public function product()
{
return $this->belongsTO('App\Productquantity', 'item_id', 'id');
}
My Controller
$dataReqorder = Reqorder::where('req_id','=', $shoppingId)->with('product')->get();
My View
#foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
<td> {{$order->product->prod_id}} </td>
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
#endforeach
I want this {{$order->product->prod_id}} should be the name of the product instead of the product ID.. I just do not know how to do it on model and controller.. Please help
How can I create my controller, model and view in order for me to display the product name?
I am trying to display the product listing on listing page of the product. Each product has category.My table structure
categories
id name description
1 Cat1 Category 1
2 Cat2 Category 2
This is the category table having id name and description
products
id name description category_id
1 pro1 product 1 1
2 pro2 product 2 2
This is the product table having category_id.
Product Model
public function categories() {
return $this->belongsTo("App\Category");
}
This is the product model where the products are belongs to category
Category Model
public function products() {
return $this->hasMany("App\Product");
}
This is the Category model where the Category has many product
Now in the product controller on listing function I want the list of product with category name
public function index()
{
$product = Product::with('categories')->get();
print_r($product->categories);die;
return view('product.index')->with("list",$product);
}
I want my Output should be
products
id name description category name
1 pro1 product 1 cat1
2 pro2 product 2 cat2
I found this error "Property [categories] does not exist on this collection instance."
When you run:
$product = Product::with('categories')->get();
you are not getting single products but all products so it should be rather renamed to:
$products = Product::with('categories')->get();
Further, looking at your database structure, each product belongs to single category so in Product model you should rename
public function categories()
{
return $this->belongsTo("App\Category");
}
to
public function category()
{
return $this->belongsTo("App\Category");
}
If you do this change, you should then again change
$products = Product::with('categories')->get();
to
$products = Product::with('category')->get();
Going back to your controller assuming all your products have set category, you can display it like this:
foreach ($products as $product)
{
echo $product->id.' '.$product->name.' '.$product->description.' '.$product->category->name;
}
You can obviously do the same later in Blade view like so:
#foreach ($list as $product)
{{ $product->id }} {{$product->name}} {{ $product->description }} {{ $product->category->name }}
#endforeach
I have changed in ProductController which is my product controllers function
public function index()
{
$products = Product::with('category')->get();
return view('product.index')->with("list",$products);
}
Also changed in Product Model
public function category() {
return $this->belongsTo("App\Category");
}
After these changes I got my expected result.
I have two models:
class Order extends Eloquent
{
public function User()
{
return $this->belongsTo('User');
}
public function Product()
{
return $this->belongsToMany('Product');
}
}
and the second one is:
class Product extends Eloquent
{
public function Order()
{
return $this->belongsToMany('Order');
}
}
My question is how can i access a value of second table column using pivot table:
product table:
id
title
image
order table:
id
status
pivot table(order_product):
id
product_id
order_id
I need to access title column of products from orders. for example if a user orders many products in one order I can fetch all product title and show theme.
I don't like use join , instead I like to use Laravel functionality itself.
I found the answer:
$orders = Order::orderBy('created_at', 'DESC')->paginate($page_number);
foreach($orders as $item){
foreach($item->product as $item){
{{$item->title}}<br>
}
}
I can access products from order.