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 :)
Related
I have a employees table and a attendances table
In employees Model
public function Attendance(){
return $this->hasMany(Attendance::class, 'employees_id', 'id');
}
Attendances table i have these column
id, employees_id, in_time, out_time, attendance_status_id, remarks
I want return those employee who has al least one attendance for a specific month which i will input in month field and it will filter based on in_time column
You can use the has() function when getting the Employee details. The has will only return those records if the Employee has any relational data in the Attendance table. You can also refer this link.
$employees = Employee::has('Attendance')->get();
Now you can simply use the count() function the display the total Attendance of the Employee. For the count please refer this link.
#foreach ($employees as $employee)
Total attendance: {{ $employee->Attendance->count() }}
#endforeach
I have 3 tables i.e category, sub category and products. I have passed category id to sub category and then sub category id to product so that i can get the whole data as per requirements. The problem in i an able to get the data from the secondary tables that has joins statement not from the primary table.Here is my code of query. Any help from you would be appreciated
public function showProduct()
{
$data = DB::table('category')
->join('sub_category','category.id',"=",'sub_category.category_id')
->join('product','sub_category.id', '=', 'product.sub_category_id')
->get();
return $this->success('date' ,$data);
}
use leftjoin
$data = DB::table('category')
->leftjoin('sub_category','category.id',"=",'sub_category.category_id')
->leftjoin('product','sub_category.id', '=', 'product.sub_category_id')
->get();
I have an orders table, an items table, and a pivot table called item_order which has two custom fields (price, quantity). The relationship between Order and Item is belongsToMany. I'm trying to return the count of all items with an id of 1, where the parent Order->status == 'received'. For the life of me, I can't figure out how to do this.
class Order extends Model
{
public function items()
{
return $this->belongsToMany(Item::class)->withPivot('price', 'quantity');
}
}
class Item extends Model
{
public function orders()
{
return $this->belongsToMany(Order::class)->withPivot('price', 'quantity');
}
}
Try this:
$total_quantity = Item::find(1) // getting the Item
->orders() // entering the relationship
->with('items') // eager loading related data
->where('status', '=', 'received') // constraining the relationship
->get() // getting the results: Collection of Order
->sum('pivot.quantity'); // sum the pivot field to return a single value.
The strategy here is to find the desired Item to then get the related Orders to this Item that has a 'received' status, to finally sum the pivot attribute in order to get a single value.
It should work.
Considering you know the id of the item, most performant way would be to query item_order table directly. I would create a pivot model for ItemOrder and define the belongsTo(Order::class) relationship and do this:
$sum = ItemOrder::where('item_id', $someItemId)
->whereHas('order', function ($q) {
return $q->where('status', 'received');
})
->sum('quantity');
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
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?