Laravel: assertDatabaseHas - unexpected fail - laravel

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.

Related

Laravel mapWithKeys() returns single value

In laravel I'm getting products array that looks like this
dd($activity->load('products'));
"products": [
{
"id": 1,
"pivot": {
"activity_id": 1,
"product_id": 10,
"quantity": 16
}
},
{
"id": 2,
"pivot": {
"activity_id": 1,
"product_id": 11,
"quantity": 20
}
}
]
I want to get an array of objects and pluck only the pivot.product_id and pivot.quantity from the products array
Expected result:
"selected_products": [
{
"id": 10,
"quantity": 16
},
{
"id": 11,
"quantity": 20
}
]
What I tried
return $activity
->load('products')
->products()
->get()
->mapWithKeys(function ($product, $key) {
return ['id' => $product->pivot->product_id, 'quantity' => $product->pivot->quantity];
});
Result
{
"id": 10,
"quantity": 16
}
There are 2 problems on your code.
First, load('products') is redundant. You cam simple call $activity->products. Or if you need filter on the products , use $activity->products()->where(...)->get()
Second, you should use map for indexed array. mapWithKeys is using for associative array.
return $activity->products
->map(function ($product, $key) {
return ['id' => $product->pivot->product_id, 'quantity' => $product->pivot->quantity];
});

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 pass array see object

I'm stuck and probably missing something really obvious.. but..
I'm trying to pass an array of roles via jwt to my SPA (not a jwt question - that bit works fine)
I get my list of role names via
$roles = $this->roles->pluck('title')->toArray();
the roles function is
public function roles()
{
return $this->belongsToMany(Role::class);
}
in laravel this works fine and logging $roles out I see ["Admin"]
on the SPA response, however, I see the entire roles object joined to the user object (the user object is supposed to be passed over here) eg:
{
"user": {
"id": 1,
"name": "Admin",
"email": "example#example.com",
"email_verified_at": null,
"user_loggedin_state": null,
"user_login_time": null,
"user_login_hash": "",
"user_log_out_time": null,
"user_phone": "123456",
"user_job": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"team_id": 1,
"roles": [
{
"id": 1,
"title": "Admin",
"created_at": null,
"updated_at": null,
"deleted_at": null,
"pivot": {
"user_id": 1,
"role_id": 1
}
}
]
}
}
What I want is
{
"user": {
"id": 1,
"name": "Admin",
"email": "example#example.com",
"email_verified_at": null,
"user_loggedin_state": null,
"user_login_time": null,
"user_login_hash": "",
"user_log_out_time": null,
"user_phone": "123456",
"user_job": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"team_id": 1,
"roles": ["Admin"]
}
}
I definitely pass the array to jwt, not the object - to validate this I wrapped the $roles in a function
public function rolesArray()
{
$roles = $this->roles->pluck('title')->toArray();
Log::info($roles);
return $roles;
}
and the jwt fn
public function getJWTCustomClaims() {
return [
'roles' => $this->rolesArray(),
];
}
Use API Resources for getting your own outputs.
Run this command to make a new resource for getting users:
php artisan make:resource UserListResource
In App/Http/Resourcers open UserListResource.php file
Change toArray method to:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'email_verified_at' => $this->email_verified_at,
'user_loggedin_state' => $this->user_loggedin_state,
'user_login_time' => $this->user_login_time,
'user_login_hash' => $this->user_login_hash,
'user_log_out_time' => $this->user_log_out_time,
'user_phone' => $this->user_phone,
'user_job' => $this->user_job,
'create_dates' => [
'created_at_human' => $this->created_at->diffForHumans(),
'created_at' => $this->created_at
],
'update_dates' => [
'updated_at_human' => $this->updated_at->diffForHumans(),
'updated_at' => $this->updated_at
],
'deleted_dates' => [
'deleted_at_human' => $this->deleted_at->diffForHumans(),
'deleted_at' => $this->deleted_at
],
'team_id' => $this->team_id
'roles' => new RolesShowResource($this->roles),
];
}
Then make Roles resources:
php artisan make:resource RolesShowResource
Open the RolesShowResource.php and do these changes:
public function toArray($request)
{
return [
'title' => $this->title,
];
}
So when you want to return objects try using the code below in fetching users:
return UserListResource::collection($users);
ok the bleeding obvious was that all this data is of course in the JWT payload..
I was confused since the moment I added stuff in the custom fields - Laravel sent the User object across in the response as well (which I want to find a way of stopping since its in the clear)

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,
];

Inserting if record not exist, updating if exist

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']
]);
}
}

Resources