laravel pass array see object - laravel

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)

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

How to send specific fields in Laravel Resource

I need to send specific fields from the API resource
here is my User resource code
namespace App\Http\Resources;
use App\Models\MediaFile;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;
class User extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
$attributes = $this->getAttributes();
unset($attributes['password']);
return [
'type' => 'users',
'attributes' => $attributes,
];
}
}
Below are my attributes
"attributes": {
"id": "1",
"email": "email",
"full_name": "Name",
"permission": "admin",
"security_key": "alpha",
"token": "encrypted token",
"two_factor_enabled": "true",
"created_at": "2020-05-15 08:56:50",
"updated_at": "2020-05-15 08:56:57",
}
I would like hide specific fields in specific routes. How can I implement this?
use makeHidden
$user = \App\Models\User::firstOrFail();
$user->makeHidden(['email', 'phone']);
return $user;
You can use two approaches:
$attributes = [
"id"=> "1",
"email" => "pera",
"full_name" => "Name",
"permission" => "admin",
"security_key" => "alpha",
"token" => "encrypted token",
"two_factor_enabled" => "true",
"created_at" => "2020-05-15 08:56:50",
"updated_at" => "2020-05-15 08:56:57",
];
PHP approach: unset($attributes['id'], $attributes['security_key']);
Laravel approach: Arr::except($attributes, ['id','security_key']);

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

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.

Laravel 5 + DingoApi paginate

i'm developing an api with Laravel and DingoAPI which returns the message threads of the user with pagination.
$threads = Thread::forUser($currentUserId)->latest('updated_at')->simplePaginate(1);
return API::response()->array($threads);
and i get this kind of response :
{
"per_page": 1,
"current_page": 1,
"next_page_url": "http://my.app/api/messages?page=2",
"prev_page_url": null,
"from": 1,
"to": 1,
"data": [
{
"id": 1,
"subject": null,
"created_at": "2016-03-18 12:33:38",
"updated_at": "2016-03-18 12:33:38",
"deleted_at": null
}
]
}
What can i do to remove some field of the response ? i just need data and next_page_url …
i tried this way :
$array_response = [
'next_page_url' => $threads['next_page_url'],
'data' => $threads['data']
];
but only null values are returned.
I guess Dingo Api is doing some work behind the scene…
Try this
$threads = Thread::forUser($currentUserId)->latest('updated_at')->simplePaginate(1);
$arrayResponse = json_decode(API::response()->array($threads));
$arrayResponseEdited = [
'next_page_url' => $arrayResponse['next_page_url'],
'data' => $arrayResponse['data']
];
return arrayResponseEdited;

Resources