Inserting if record not exist, updating if exist - laravel-5

Hiii
I have 2 database tables with the columns table :1 "id, invoice_id, subject, total" table:2 "id, invoice_id, item_name, price".whenever i try to update record with the help of invoice_id if record doesn't exist in item table it will not insert new item in item table.
here i attached my JSON data
{
"date": "2019-06-08",
"client_id": "1",
"currency_id": 4,
"total_amount": null,
"subject": "RD Management",
"items": [
{
"item_name": "Saving",
"price": "500"
},
{
"item_name": "Fix",
"price": "500"
},
{
item_name": "Current",
"price": "200"
}
]
}
here one problem is also
my JSON can not send item_id also
so without item id how can i update my record...???
here 3rd item is not present in my table
here is my controller
foreach ($request->items as $key => $items)
{
$item_update = [
'item_name' => $items['item_name'],
'price' => $items['price']
];
DB::table('items')
->where('invoice_id', $id)
->update($item_update);
}
I Except output like this
"items": [
{
"id": 1,
"invoice_id": "1",
"item_name": "Saving",
"price": "500",
},
{
"id": 2,
"invoice_id": "1",
"item_name": "Fix",
"price": "500",
},
{
"id": 3,
"invoice_id": "1",
"item_name": "current",
"price": "200",
},
]
but my actual output is
"items":[
{
"id":"1"
"item_name": "Fix",
"price": "500",
},
{
"id":"2"
"invoice_id": "1",
"item_name": "Fix",
"price": "500",
}
]
this output override item_name at update time.
there are any way to solve this both problem.

If you can't identify which items already exist and which ones are new, your remaining option is to identify items by item_name+invoice_id. The downside is that you cannot update item_name this way.
If you have Eloquent models properly set up, you can use updateOrCreate().
<?php
foreach ($request->items as $key => $items)
{
$itemAfterUpdate = App\Item::updateOrCreate(
[
'invoice_id' => $id,
'item_name' => $items['item_name']
],
[ 'price' => $items['price'] ]
);
}
If not, you will basically have to do what Eloquent does behind the scenes, which is check if the item already exists based on item_name and invoice_id, and then insert or update accordingly.
<?php
foreach ($request->items as $key => $items)
{
$alreadyExists = DB::table('items')
->where('invoice_id', $id)
->where('item_name', $items['item_name'])
->exists();
}
if($alreadyExists){
DB::table('items')
->where('invoice_id', $id)
->where('item_name' => $items['item_name'])
->update(['price' => $items['price']);
}
else{
DB::table('items')->insert([
'invoice_id' => $id,
'item_name' => $items['item_name'],
'price' => $items['price']
]);
}
}

Related

i want to change my API response to become array of array object in laravel

i have a problem for the response, i want to change the response API because i need for my mobile APP, the feature have filter object based on date. So i hope you all can help me to solve the problem
i wanna change the response for my API
before:
{
"tasks": [
{
"id": 7,
"user_id": 1,
"title": "gh",
"date": "2022-02-10 13:05:00",
"deskripsi": "gfh",
"created_at": "2022-02-09T06:05:56.000000Z",
"updated_at": "2022-02-09T06:05:56.000000Z"
},
{
"id": 5,
"user_id": 1,
"title": "ghf",
"date": "2022-02-17 16:05:00",
"deskripsi": "fghf",
"created_at": "2022-02-09T06:05:12.000000Z",
"updated_at": "2022-02-09T06:05:12.000000Z"
},
{
"id": 6,
"user_id": 1,
"title": "fgh",
"date": "2022-02-17 18:05:00",
"deskripsi": "gh",
"created_at": "2022-02-09T06:05:40.000000Z",
"updated_at": "2022-02-09T06:05:40.000000Z"
}
]
}
here is the code for the response API above
return response([
'tasks' => Task::where('user_id', auth()->user()->id)->where('date','>',NOW())->orderBy('date','asc')->get(),
],200);
and i want to change it my response API into this response
{
"tasks": [
{
"date": "2022-02-10",
"task": [
{
"id": 7,
"user_id": 1,
"title": "gh",
"date": "2022-02-10 13:05:00",
"deskripsi": "gfh",
"created_at": "2022-02-09T06:05:56.000000Z",
"updated_at": "2022-02-09T06:05:56.000000Z"
},
{
"id": 7,
"user_id": 1,
"title": "gh",
"date": "2022-02-10 15:05:00",
"deskripsi": "gfh",
"created_at": "2022-02-09T06:05:56.000000Z",
"updated_at": "2022-02-09T06:05:56.000000Z"
}
]
},
{
"date": "2022-02-12",
"task": [
{
"id": 7,
"user_id": 1,
"title": "gh",
"date": "2022-02-12 13:05:00",
"deskripsi": "gfh",
"created_at": "2022-02-09T06:05:56.000000Z",
"updated_at": "2022-02-09T06:05:56.000000Z"
}
]
},
]
}
Do groupBy on the resulting Collection from the query (see docs: https://laravel.com/docs/9.x/collections#method-groupby)
For example, you could do:
$tasksGroupedByDate = Task::where(.......)
->get()
->groupBy(fn (Task $task) => $task->date->format('Y-m-d'));
(Note: above uses PHP 7.4 arrow functions. Also, add a date cast on the date column in your Task model to be able to use ->format( directly on the date field)
The above code results to:
{
'2022-01-01' => [
{ Task object },
{ Task object },
{ Task object },
],
'2022-01-02' => [
{ Task object },
{ Task object },
{ Task object },
],
}
(used Task object for brevity, but that will be ['id' => 1, 'title' => 'Task name', .....])
To morph that to the structure you want, you can use map and then values to remove the keys and turn it back to an ordered array:
$tasksGroupedByDate->map(fn ($tasksInGroup, $date) => [
'date' => $date,
'task' => $tasksInGroup
])->values();
If you want to combine everything into one method chain:
return [
'tasks' => Task::where(......)
->get()
->groupBy(fn (Task $task) => $task->date->format('Y-m-d'))
->map(fn ($tasksInGroup, $date) => [
'date' => $date,
'task' => $tasksInGroup
])
->values(),
];
It sounds like you want to create a human friendly date field based on the date column, then group by it.
While solutions do exists to accomplish this at the database level, I believe you'd still need to loop around it again afterwards to get the hierarchy structure you're looking for. I don't think it's too complicated for PHP to loop through it.
My suggestion is as follows:
Before:
return response([
'tasks' => Task::where('user_id', auth()->user()->id)
->where('date','>',NOW())->orderBy('date','asc')->get(),
],200);
After:
$out = [];
$tasks = Task::where('user_id', auth()->user()->id)
->where('date','>',NOW())->orderBy('date','asc')->get();
foreach($tasks as $task) {
$date = strtok((string)$task->date, ' ');
if (empty($out[$date])) {
$out[$date] = (object)['date' => $date, 'task' => []];
}
$out[$date]->task[] = $task;
}
$out = array_values($out);
return response(['tasks' => $out], 200);
Note in the above I'm using the function strtok. This function might look new even to the most senior of php developers.... It's a lot like explode, except it can be used to grab only the first part before the token you're splitting on. While I could have used explode, since the latter part after the token isn't needed, strtok is better suited for the job here.
$task = Task::where('user_id', auth()->user()->id)->where('date','>',NOW())->orderBy('date','asc')->get();
foreach($task as $item){
$date[] = item->date;
$result = Task::where('user_id', auth()->user()->id)->where('date','=', $date)->get();
}
return response([
'tasks' =>
['date' => $date,
'task' => $task]
],200);
maybe something like this

Laravel Check for Value from Relation

I have a query that looks like this where I fetch data for various businesses in a particular location and I need to be able to tell that each business has (or does not have) a female employee.
$business = Business::where('location', $location)
->with(['staff'])
->get();
return MiniResource::collection($business);
My Mini Resource looks like this:
return [
'name' => $this->name,
'location' => $this->location,
'staff' => PersonResource::collection($this->whenLoaded('staff')),
];
This is what a sample response looks like:
{
"id": 1,
"name": "XYZ Business"
"location": "London",
"staff": [
{
"name": "Abigail",
"gender": "f",
"image": "https://s3.amazonaws.com/xxxx/people/xxxx.png",
"role": "Project Manager",
},
{
"name": "Ben",
"gender": "m",
"image": "https://s3.amazonaws.com/xxxx/people/xxxx.png",
"role": "Chef",
},
]
}
I really don't need the staff array, I just want to check that a female exists in the relation and then return something similar to this:
{
"id": 1,
"name": "XYZ Business"
"country": "USA",
"has_female_employee": true;
}
Is there an eloquent way to achieve this ?
NB: In my original code I have more relations that I query but I had to limit this post to be within the scope of my problem.
If you are only looking for male or female staff members, you can achieve it like so:
$someQuery->whereHas('staff', function ($query) {
$query->where('gender', 'f');
})
If you want both genders, I wouldn't go through the hassle of achieving this in the query, but recommend reducing your results collection in your MiniResource:
return [
'name' => $this->name,
'location' => $this->location,
'has_female_employee' => $this->whenLoaded('staff')->reduce(
function ($hasFemale, $employee) {
$hasFemale = $hasFemale || ($employee->gender === 'f');
return $hasFemale;
}, false),
];
Even better would be to create it as a method on your MiniResource for readability.
Change your code like below and see
$business = Business::where('location', $location)
->with(['staff'])
->where('gender', 'f')
->get();
return [
'name' => $this->name,
'location' => $this->location,
'has_female_employee' => empty($this->whenLoaded('staff')) ? false : true,
];

Laravel: assertDatabaseHas - unexpected fail

I don't understand why this database test fails. I'm aware that i don't assert on the created_at and updated_at columns, but the three columns (id, user_id, thing_id) should be enough and i'm sure that i have tested on just a selection of columns before, and it has worked!
What am i missing?
Failed asserting that a row in the table [thing_history] matches the attributes [
{
"id": 1,
"user_id": 1,
"thing_id": 1
},
{
"id": 2,
"user_id": 1,
"thing_id": 2
},
{
"id": 3,
"user_id": 1,
"thing_id": 3
}
].
Found: [
{
"id": "1",
"user_id": "1",
"thing_id": "1",
"created_at": "2019-02-01 21:18:17",
"updated_at": "2019-02-01 21:18:17"
},
{
"id": "2",
"user_id": "1",
"thing_id": "2",
"created_at": "2019-02-01 21:18:17",
"updated_at": "2019-02-01 21:18:17"
},
{
"id": "3",
"user_id": "1",
"thing_id": "3",
"created_at": "2019-02-01 21:18:17",
"updated_at": "2019-02-01 21:18:17"
}
]
This is the test code
/** #test */
public function retrieving_feed_creates_history()
{
$user = factory('App\User')->create();
$this->actingAs($user);
factory('App\Thing', 3)->create();
$response = $this->json('GET', '/api/thing/feed/all');
$this->assertDatabaseHas('feed_histories', [
[
'id' => 1,
'thing_id' => 1,
'user_id' => $user->id,
],
[
'id' => 2,
'thing_id' => 2,
'user_id' => $user->id,
],
[
'id' => 3,
'thing_id' => 3,
'user_id' => $user->id,
]
]);
}
This is the migration code:
public function up()
{
Schema::create('feed_histories', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('thing_id');
$table->timestamps();
});
}
Seems like i have misunderstood something. To check several rows, i have to split the test into into separate assertions for each row.
This works fine:
$this->assertDatabaseHas('feed_histories', [
'thing_id' => $thingA->id,
'user_id' => $user->id,
]);
$this->assertDatabaseHas('feed_histories', [
'thing_id' => $thingB->id,
'user_id' => $user->id,
]);
Yeah, the map of multiple records fails as assertDatabaseHas function currently only handles a single row by mapping a single row in the where clause...
To get a better insight, you can have a look at the base function of assertDatabaseHas
public function matches($table): bool
{
return $this->database->table($table)->where($this->data)->count() > 0;
}
here, the $this->data refers to the second argument of assertDatabaseHas funciton
So, it clears out our doubt of why we can't pass array of arrays.

Returning Specific Columns with SUM & COUNT from Collection of Relations

I have Models Product, ProductVariant, and Department
Department:
id | name | ....
Product:
id | name | sku | department_id | ....
ProductVariant:
id | product_id | quantity | ....
And all associated with each other like:
Relationship products: Department hasMany Products
Relationship department: Product belongsTo Department
Relationship variants: Product hasMany ProductVariants
Relationship product: Product belongsTo Product
Everything works as expected between relations over Eloquent calls
Now, using Eloquent I'm trying to retrieve a collection of following columns:
product.id | product.name | product.variant_count | product.stock | department.name
By product.stock I mean: $product->variants->sum('quantity'), but I'm having hard time getting SUM inside with() method
What I've tried so far:
$products = Product::select('id', 'name', 'sku', 'department_id') //gives product.name, sku, etc
->withCount('variants') //gives product.variants_count
->with(['variants' => function($query) {
$query->select('id', 'product_id', 'quantity'); //gives variants->each.quantity
}])
->with(['department' => function($query) {
$query->select('id', 'name'); //gives department.name
}]);
This code gives something like this:
[
{
"id": "2",
"name": "Letv LU50609 Earphone - Grey",
"sku": "PT-00002",
"department_id": "2",
"variants_count": "1",
"variants": [
{
"id": "2",
"product_id": "2",
"quantity": "35"
}
],
"department": {
"id": "2",
"name": "Phones & Tabs Accessories"
}
},
{
"id": "3",
"name": "MI In-Ear Headphones Basic 3.5mm HSEJ03JY",
"sku": "PT-00003",
"department_id": "2",
"variants_count": "5",
"variants": [
{
"id": "3",
"product_id": "3",
"quantity": "9"
},
{
"id": "4",
"product_id": "3",
"quantity": "9"
},
{
"id": "5",
"product_id": "3",
"quantity": "10"
},
{
"id": "6",
"product_id": "3",
"quantity": "7"
},
{
"id": "7",
"product_id": "3",
"quantity": "7"
}
],
"department": {
"id": "2",
"name": "Phones & Tabs Accessories"
}
}
]
But what I want to achieve is:
[
{
"id": "2",
"name": "Letv LU50609 Earphone - Grey",
"sku": "PT-00002",
"variants_count": "1",
"stock": "35",
"department": "name": "Phones & Tabs Accessories"
},
{
"id": "3",
"name": "MI In-Ear Headphones Basic 3.5mm HSEJ03JY",
"sku": "PT-00003",
"variants_count": "5",
"stock": "42",
"department": "name": "Phones & Tabs Accessories"
}
]
How can I achieve this???
Option 1
You could map() the collection before return it:
$products = Product::select('id', 'name', 'sku', 'department_id')
->withCount('variants')
->with(['variants', 'department'])
->get()
->map(function ($product){
return [
'id' => $product->id,
'name' => $product->name,
'sku' => $product->sku,
'variants_count' => $product->variants_count,
'stock' => $product->variants->sum('quantity'),
'department' => $product->department->name
];
});
Option 2
Using API Resources. Let me know if you need help in this aspect.
What about using the query builder something like this:
DB::table('products as product')
->select([
'product.id',
'product.name',
DB::raw('count(pv.id) as variant_count'),
DB::raw('sum(pv.quantity) as stock'),
'department.name'
])
->join('department', 'product.department_id', '=', 'department.id')
->join('product_variants as pv', 'product.id', '=', 'pv.id')
->get();
Not sure if this will work exactly like this, but it should give you a path.
you can access your required result with this query by checking your database tables
SELECT
products.*,
SUM(variants.available) AS stock,
COUNT(variants.id) as count,
department.name as department
FROM
products
LEFT JOIN variants ON products.id = variants.product_id
LEFT JOIN department ON products.department_id= department.id
GROUP BY
products.id
What you are looking for can be achieved in many ways. The ideal solution will build the sum within the database for best performance. To achieve so, you can use a custom query together with the Laravel query builder as already explained by #nakov or you use a little exploit of the Eloquent relationship system:
$products = Product::query()
->join('departments', 'departments.id', '=', 'products.department_id)
->withCount('variants as variant_count')
->withCount(['variants as stock' => function ($query) {
$query->selectRaw('SUM(quantity)'); // this may look weird but works
})
->select([
'products.id',
'products.name',
'departments.name as department',
])
->get();

How to join output of models in Laravel?

I have the following code:
$orders = OrderProduct::where(function ($query) use ($request) {
})->with('order_products')->orderBy('status', 'desc')->paginate(50)->toArray();
And order_products function is:
public function order_products()
{
return $this->hasMany("App\Order", "order_id", "order_id");
}
It gives me output result:
{
"user_id": "1",
"created_at": "2016-12-18 14:06:11",
"status": "2",
"note": "34535345",
"order_id": "2",
"address": "Kiev",
"courier_id": null,
"payment_type": "0",
"order_products": [
{
"id": 73,
"product_id": "1265",
"amount": "1"
},
{
"id": 74,
"product_id": "1266",
"amount": "1"
}
]
I need to join order_products with products_details, that to give title of product for each order like as:
"order_products": [
{
"id": 73,
"product_id": "1265",
"amount": "1",
"title": "Product name"
},
{
"id": 74,
"product_id": "1266",
"amount": "1",
"title": "Product name"
}
]
Instead this I get a new model output in response:
"products_details": [
{}, {}
]
How can I join two with models?
without joining, just using your first code:
in order_products, override the toArray() method.
function toArray() {
return [
id => $this->id,
product_id => $this->product_id,
amount => $this->amount,
title => $this->details->name
];
}
wich $this->details->name is the relation between order_products and products_details

Resources