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?
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
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
There are 3 tables
Sizes
id
name
Products
id
name
Product_sizes
id
product_id
size_id
I want to fill an selectbox with the id of product_sizes and the name of the foreign key connected table size.
for exampe:
product_sizes.id 1
sizes.name medium
where medium would be found by size_id in product sizes.
How can I the name value in the list?
What i currently have
I can get the right row with
$sizes = $product->sizeAvailable()->lists('pr', 'size_id');
where sizeAvailable is a relation that returns all product_sizes for a selected product.
First of all you don't need an id field in the pivot table because you can uniquely identify each row with the composite key product_id, size_id. A Product can belong to many Sizes and a Size can belong to many Products, thus you shouldn't have duplicate rows.
The relationships of each model are like the following:
class Product extends Model {
public function sizeAvailable() {
return $this->belongToMany('Size', 'product_sizes');
}
}
class Size extends Model {
public function products() {
return $this->belongToMany('Product', 'product_sizes');
}
}
Finally in your View you can use Blade functions to generate your selectbox:
<select>
#foreach($product->sizeAvailable() as $size)
<option value="{{ $size->id }}">{{ $size->name }}</option>
#endforeach
</select>
Please any help me that how i pull all the fields from both tables in laravel many to many relationship.
I have two table
categories
id
cname
ctitle
description
products
id
pname
ptitle
pdescription
price
and bridge table
products_to_categories
id
category_id
product_id
So i have pull all the fields from both tables based on product id.
UPD: As commented patricus, the "bridge" table should be called "category_product"
First, you should define the "belongsToMany" relationship in the Product model:
// ...
class Product extends Eloquent {
// ...
function categories()
{
return $this->belongsToMany('Category');
}
}
After it you should load the product by id:
$product = Product::find($product_id);
And now you can access its categories through the "categories" property:
foreach($product->categories as $category)
{
// do what you want with the $category
}
Official docs for M2M relationships:
http://laravel.com/docs/4.2/eloquent#many-to-many