I have written as below in my code in LineSheet controller and the items function is in LineSheet model, and I have an array call $seasons. Now I need to send this $seasons array with items
LineSheet Controller:
$linesheetItems = LineSheetItem::select('linesheet_id', 'items.season', 'items.amt_item')
->join('items', function ($join) {
$join->on('items.amt_item', '=', 'linesheet_items.item_code');
$join->on('items.company', '=', 'linesheet_items.company');
$join->on('items.division', '=', 'linesheet_items.division');
})
->where('linesheet_items.linesheet_id', '=', $id)
->groupBy('items.amt_item', 'items.season')
->get();
foreach ($linesheetItems as $linesheetItem) {
$seasons[] = Season::where('code', $linesheetItem['season'])->first();
}
$linesheet = LineSheet::where('linesheet.id', '=', $id)
->select('linesheet.*')->with(['items', 'creator:id,username', 'updater:id,username'])
->first();
LineSheet Model:
public function items()
{
return $this->hasMany('\App\Models\LineSheetItem', 'linesheet_id', 'id')
->join('items', function ($join) {
$join->on('items.amt_item', '=', 'linesheet_items.item_code');
$join->on('items.company', '=', 'linesheet_items.company');
$join->on('items.division', '=', 'linesheet_items.division');
})
->join('inventory_items', function ($join) {
$join->on('inventory_items.item', '=', 'linesheet_items.item_code');
$join->on('inventory_items.company', '=', 'linesheet_items.company');
$join->on('inventory_items.division', '=', 'linesheet_items.division');
})
->select('linesheet_items.linesheet_id', 'items.id', 'items.amt_item', 'items.image_name',
'items.company', 'items.division', 'items.color_description', 'items.item_description',
'items.season', 'items.wholesale_price', 'items.retail_price', 'items.color_code',
'items.vendor_desc', 'items.vendor_code', 'inventory_items.on_hand', 'inventory_items.cost',
'items.brand', 'items.category_code', 'items.category', 'items.fabric_code', 'items.fabric_desc')
->groupBy('linesheet_items.item_code', 'linesheet_items.company', 'linesheet_items.division');
}
with items I need to attach my $seasons array also and get a response as below
I expect a final output as below:
"items": [
{
"linesheet_id": 44,
"id": 61,
"amt_item": "PS839730WT",
"image_name": "image_name",
"company": "01",
"division": "PAP",
"color_description": "colordes1",
"item_description": "itemdes1",
"season": "S1",
"wholesale_price": "100",
"retail_price": "100",
"color_code": "colorcode1",
"vendor_desc": "vendor_desc",
"vendor_code": "vendor_code",
"on_hand": "40",
"cost": "3.70",
"brand": "brand",
"category_code": "category",
"category": "category1",
"fabric_code": "code1",
"fabric_desc": "fabric_desc",
seasons: [
//array elements
]
}
]
How to achieve this?
You can use belongsTo relationship and then eager load them. A LineSheetItem seems to belong to a Session.
So you can do LineSheetItem::with('season')->get().
https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse
https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
Related
I have created a function to get data from db but the function returns an error when i test with postman.
public function payments(){
$data = Investigations::whereHas(function($query){
$query->where('name', 'LIKE', '%consultation%' );
})->get();
return $data;
}
The table has these columns
protected $fillable = [
"visit", "admission_id", "type", "procedure", "quantity", "price", "discount",
"amount", "user", "instructions", "ordered", "invoiced", "reasons", "assigned_to",
"performing_doctor", "on_credit", "comments", "credit_status", "is_walkin",
'service_sale_id', "cancelled", 'cancel_text', "date_changed_by", "date_adjusted_from",
"assigned_by", "assigned_on",
'moved_from_chargesheet', 'package_id',
"created_at",
];
I can't see there is a "name" column.
'whereHas' used to query in a relation
Investigations::whereHas('relation_name', function($query){
$query->where('name', 'LIKE', '%consultation%' );
})->get();
If you want to search in the current table, you can use 'where'.
it can be a condition or a function
Investigations::where('name', 'LIKE', '%consultation%' )->get()
Investigations::where(function($query){
$query->where('name', 'LIKE', '%consultation%' );
})->get()
I have two models TeamleaderCompany which : has many TeamleaderCompanyTag
TeamleaderCompany
public function teamleaderCompanyTags()
{
return $this->hasMany('App\TeamleaderCompanyTag');
}
TeamleaderCompanyTag
public function teamleaderCompany()
{
return $this->belongsTo(TeamleaderCompany::class);
}
when I TeamleaderCompany::all() I have this results :
(...)
"teamleader_company_tags": [
{
"id": 7,
"tag": "hot lead",
"teamleader_company_id": 3,
"created_at": "2019-09-03 09:23:51",
"updated_at": "2019-09-03 09:23:51"
},
{
"id": 8,
"tag": "reseller",
"teamleader_company_id": 3,
"created_at": "2019-09-03 09:23:51",
"updated_at": "2019-09-03 09:23:51"
}
]
(...)
What I'm trying to do is to show TeamleaderCompany results where teamleaderCompanyTags has only one tag which is 'reseller' (if there is another tag except 'reseller' don't show)
$companies->whereHas(
'teamleaderCompanyTags',
function ($query) use ($condition) {
$query->where('tag',
(...)
);
}
);
thanks
Try this method
TeamleaderCompany::with('teamleaderCompanyTags:id,tag')
->whereHas('teamleaderCompanyTags',function(\Illuminate\Database\Eloquent\Builder $query){
$query->where('tag', "reseller");
})->get();
Try this query:
TeamleaderCompany::has('teamleaderCompanyTags', '=', 1) // companies that have only one tag
->whereHas('teamleaderCompanyTags', function ($query) { // companies that have `reseller` tag
$query->where('tag', 'reseller');
})
->get()
You have two conditions.
it need to have the "reseller" tag
it's the only tag that it has
TeamleaderCompany::whereHas('teamleaderCompanyTags', function ($query) {
$query->where('tag', 'reseller');
})
->whereDoesntHave('teamleaderCompanyTags', function ($query) {
$query->where('tag', '!=', 'reseller');
})
->get()
Hello I have query that is already working and it combines two queries, but what I need is to combine the same product _id and not output it into another array.
Here is my code:
$first = DB::table('delivery_receipt_detail');
$first->join('delivery_receipts', 'delivery_receipt_detail.delivery_receipt_id', '=', 'delivery_receipts.id');
$first->where(function ($first) use ($start_date, $end_date) {
$first->whereBetween('delivery_receipts.posting_date', [new Carbon($start_date), new Carbon($end_date)]);
$first->orWhereDate('delivery_receipts.posting_date', '=', new Carbon($start_date));
$first->orWhereDate('delivery_receipts.posting_date', '=', new Carbon($end_date));
});
$first->groupBy('delivery_receipt_detail.product_id');
$first->select('delivery_receipt_detail.product_id as delivery_product_id', DB::raw('SUM(product_qty) as delivery_product_qty'));
$result_one = $first->get();
//Sales
$query = DB::table('sale_detail');
$query->join('sales', 'sale_detail.sale_id', '=', 'sales.id');
$query->where(function ($query) use ($start_date, $end_date) {
$query->whereBetween('sales.posting_date', [new Carbon($start_date), new Carbon($end_date)]);
$query->orWhereDate('sales.posting_date', '=', new Carbon($start_date));
$query->orWhereDate('sales.posting_date', '=', new Carbon($end_date));
});
$query->groupBy('sale_detail.product_id');
$query->select('sale_detail.product_id as sales_product_id', DB::raw('SUM(product_qty) as sales_product_qty'));
$test = $query->get();
$merged = $result_one->merge($test);
$result = $merged->all();
return $result;
and my result is this:
[
{
"delivery_product_id": "1",
"delivery_product_qty": 4
},
{
"delivery_product_id": "2",
"delivery_product_qty": 1
},
{
"sales_product_id": "1",
"sales_product_qty": "3"
},
{
"sales_product_id": "2",
"sales_product_qty": "3"
}
]
What I need is this output
[
{
"product_id": "1",
"delivery_product_qty": 4
"sales_product_qty": "3"
},
{
"product_id": "2",
"delivery_product_qty": 1
"sales_product_qty": "3"
}
]
I need to combine this and merging the same product_id and show their different quantities.
For this case, I think you only need to specify the same name for the product_id when selecting and ->merge will do the rest
$first = DB::table('delivery_receipt_detail')
->select('delivery_product_id as product_id', 'delivery_product_qty');
// ...
$query = DB::table('sale_detail')
->select('sales_product_id as product_id', 'sales_product_qty');
// ...
$merged = $result_one->merge($test);
Define a simple query for getting three tables data
table users,
table user_details,
table user_device_datas
On my LoginController define a function to get data from UserDetails model .
$userInformation = UserDetails::userDetail($user->id);
on User model define a relationship
public function UserDeviceData() {
return $this->hasMany(UserDeviceData::class);
}
public function UserDetails()
{
return $this->hasOne(UserDetails::class);
}
On UserDetails model my query for getting data look like
public static function userDetail($id){
$result = User::whereHas('userDetails',function($query) use ($id) {
$query->where('user_id',$id);
})->whereHas('UserDeviceData',function($query) use ($id) {
$query->where('user_id',$id);
})->where('user.id', $id)
->with('userDetails','UserDeviceData')
->first();
}
Response i am getting :
"userInfo": {
"id": 2,
"name": null,
"email": "test#test.com",
"phone": null,
"address": null,
"company": null,
"description": null,
"device_id": null,
"system_role": "User",
"status": "Active",
"otp_verify_id": "253526851",
"parent_id": 0,
"last_login": "2018-04-23 12:03:43",
"profile_picture": null,
"business_card_pic": null
}
I am not getting my userDetails Data on the query.what is the mistake in my code?
public static function userDetail($id){
$result = User::whereHas('userDetails',function($query) use ($id) {
$query->where('user_id',$id);
})->whereHas('UserDeviceData',function($query) use ($id) {
$query->where('user_id',$id);
})->where('users.id',$id)
->with(['UserDetails' => function($query) use ($id){
$query->where('user_id',$id);
},
'UserDeviceData' => function($query) use ($id){
$query->where('user_id',$id);
}
])
->first();
return $result ;
}
You have to use with() to get the relations too.
I need to alias when I do a Laravel eager loading:
$posts = Post::with(array('images as main_image' => function($query) // do not run
{
$query->where('number', '=', '0');
}))
->where('id', '=', $id)
->get();
return Response::json($posts);
I need to to this because I want the JSON response like this:
[
{
"id": 126,
"slug": "abc",
"name": "abc",
"created_at": "2014-08-08 08:11:25",
"updated_at": "2014-08-28 11:45:07",
"**main_image**": [
{
"id": 223,
"post_id": 126
...
}
]
}
]
It is possible?
Perfect! you give me the idea. Finally I've done this:
Post.php
public function main_image()
{
return $this->hasMany('FoodImage')->where('number','=','0');
}
public function gallery_images()
{
// for code reuse
return $this->main_image();
}
PostController.php
$posts = Post::with('main_image', 'gallery_images')
->where('id', '=', $id)
->get();
I don't think you can do that with Eloquent, but there would be a few work-arounds that might work.
If you are using PHP 5.6, it's possible to alias the function.
use function images as main_image;
If you are using a version of PHP less than that, you can create a main_image() function and have it call images().
public function main_image()
{
return $this->images();
}