laravel fill selectbox with foreign key - php

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>

Related

How to exclude certains columns while using eloquent | Laravel

i have an group table. My main table. So i have group_hotels table this table is a pivot table. (id, group_id, hotel_id)
Now in my blade group edit page, i want to list before added pivot table like this code:
My Group model:
public function hotels(){
return $this ->belongsToMany('App\Models\Hotel','group_hotels');
}
My controller:
$group = Group::find($id);
$hotels=Hotel::all();
My blade hotel select2 field:
#foreach($hotels as $hotel)
<option value="{{$hotel->id}}">{{$hotel->name}}</option>
#foreach ($group->hotels as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
#endforeach
#endforeach
The output like this: http://prntscr.com/10v13ed
Now what i want to do, in my group_hotels pivot table, if hotel id exist in group_hotels except the in the pivot table id nothing come hotel table.
In short: If the hotel_id part in the group_hotels table is present in my hotel table, I do not want to call it in the hotel variable.
One option you have is to exclude the Hotel's you don't want in your list at the query level:
Hotel::whereNotKey($group->hotels->modelKeys())->get();

fetch join table data using eloquent laravel

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?

Show Product Name in Laravel Model Relationship

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

Show Product Name in 3 Tables in Laravel Relationship Model

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?

Laravel Eloquent many to many relationship with different column as key

I have the following models
Category
id // we don't want to use this one for the relation
category_id // we want to use this
Product
id
CategoryProduct
product_id // points to "id" on products table
category_id // points to **category_id** on categories table
I have the Product model set up as follows (only the most recent variation).
class Product {
public function categories() {
return $this->belongsToMany('Category', 'categories_products', 'category_id');
}
}
When I try to get a product with categories as follows...
Product::with('categories')->get();
I either get the wrong categories because the query is using id on categories as the foreign key. I need it to be using category_id on the categories table.
Or I get none at all. I can't seem to hash out how to set up which columns to use on the belongsToMany method.
Try this follow,
class Product {
public function categories() {
return $this->belongsToMany('Category', 'categories_products', 'product_id','category_id');
}
}

Categories