Laravel customer API resource with relationships - laravel

Controller function:
public function index () {
// TESTED
// The getAllActiveSuppliers() function just return Supplier::pagniate(10)
$suppliers = $this -> model -> getAllActiveSuppliers();
return new SupplierResource($suppliers);
}
Returned Json:
{
"current_page": 1,
"data": [
{
"id": 23,
"name": "Test Name",
"description": "Test Description",
"created_by": {
"id": 1,
"name": "Test 1",
"email": "admin#admin.com",
"email_verified_at": null,
"active": 1,
"created_at": "2018-10-12 14:17:38",
"updated_at": "2018-10-12 14:17:38"
},
"updated_by": {
"id": 1,
"name": "Test 1",
"email": "admin#admin.com",
"email_verified_at": null,
"active": 1,
"created_at": "2018-10-12 14:17:38",
"updated_at": "2018-10-12 14:17:38"
},
"deleted_at": null,
"created_at": "2018-10-31 01:46:11",
"updated_at": "2018-11-02 22:05:14",
}
],
...
}
What I am trying to do:
In the created_by and updated_by I just want to show name, email nothing else.
What I have tried to do:
I have tried to create an API resource collection
Supplier.php API Resource Collection :
public function toArray($request)
{
return parent::toArray($request);
}

For anyone that may still need this as I was searching for myself in this topic
First create a resource for your relation with name, email fields
class UserResource extends JsonResource
{
public function toArray($request)
{
return [
'name' => $this->name,
'email' => $this->email,
];
}
}
Then create SupplierResource with relation created_by to User
class SupplierResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'created_by' => new UserResource($this->created_by)
];
}
}
In the Controller
public function index () {
$suppliers = $this->model->getAllActiveSuppliers();
return SupplierResource::collection($suppliers);
}

You first need to define a structure for a singular JsonResource object:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
...
'created_by' => $this->created_by->only('name', 'email'),
...
];
}
And then tell your ResourceCollection to use the class:
Customizing The Underlying Resource Class
Typically, the
$this->collection property of a resource collection is automatically
populated with the result of mapping each item of the collection to
its singular resource class. The singular resource class is assumed to
be the collection's class name without the trailing Collection
string.
For example, UserCollection will attempt to map the given user
instances into the User resource. To customize this behavior, you may
override the $collects property of your resource collection
(From https://laravel.com/docs/5.7/eloquent-resources#concept-overview)
If your ResourceCollection doesn't do anything extra, you might not need it. Every JsonResource can transform itself for a collection with the collection() method, e.g. JsonResource::collection($models)

Think we can add item to parent::toArray($request)
public function toArray($request)
{
$array = parent::toArray($request);
$array['created_by'] = $this->created_by->only('name', 'email');
return $array;
}

Related

How to show field name of the relation of the relation in laravel using backpack?

Role model relation
public function companies()
{
return $this->belongsToMany(Company::class);
}
User model standart role relation
public function roles(): BelongsToMany
{
return $this->morphToMany(
config('permission.models.role'),
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
'role_id'
);
}
add to User model
public function roleCompanies()
{
return $this->roles()->with('companies');
}
Now i see in logging data i need
...
"App\\Models\\User": {
...
"role_companies": [
{
"id": 1,
"name": "administrator",
....
},
"companies": [
{
"id": 1,
"name": "Company name"
but how can i output role_companies - companies - name in backpack field like that(this actual doesnt work):
UserCrudController
...
$this->crud->addColumns([
...
[
'type' => 'relationship',
'name' => 'roleCompanies',
'attribute' => 'companies.name',
],
Is there any native method to do that. Or i need to create custom backpack field?

how to change query so it only shows name instead whole array (laravel 8)

I got a query that gets the data into a collection, problem is that it shows foreign id but i want it to display what i have given in the url parameters.
columnsGiven is the parameter from url. contains column names with child: "language.name". so column=active,title,language.name
For example i get this:
"name": "george",
"active": 1,
"language": 1,
and this is what i want:
"name": "george",
"active": 1,
"language": "Dutch",
this is my code:
public function index(Request $request){
$columnsGiven = explode(',', $request->columns);
$tableName = $request->table_name; //example: 'support_guide_translations'
$modelName = $request->model_name; //example: "App\Models\SupportGuideTranslation";
if($request->search){
$query = $modelName::search($request->search);
} else{
$query = $modelName::query();
if($request->sort){
$sort = explode('?', $request->sort);
$query->orderBy($sort[0], $sort[1]);
}
}
foreach ($request->query() as $key => $value) {
// dd($value);
if(!$value == ""){
if(Schema::hasColumn($tableName, $key)){
$query->where($key, $value);
}
if(in_array($key, $columnsGiven)){
dd('true');
}
// $searchWord = Str::contains('account123',$request->search);
}
}
$guides = $query->get();
return GuideResource::collection($guides);
}
}
this is GuideResource, it sends data to vue by making it json first. Not allowed to make changes here, it has to be done in the function index. :
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'active' => $this->active,
'language' => $this->language,
'support_guide' => $this->support_guide,
'support_guide_group' => $this->support_guide_group,
];
}
"language": {
"id": 1,
"name": "Dutch",
"code": "NL",
"created_at": "2021-06-14T10:10:32.000000Z",
"updated_at": "2021-06-14T10:10:32.000000Z"
},
I believe "language" has a relationship with another model.
Isn't this what you are looking for?
return [
// your other attributes...
'language' => $this->language->relationshipName->name,
];
where relationshipName is the method name in the Language model.

Why Graphql cannot get the args by using variable but worked when using static var

My mutation scheme:
mutation edit($id: Int!) {
user_edit(id:$id) {
user_name
}
}
query variable is follow
{
"id": 1
}
I use this with laravel-graphql. Here is my definition of user_edit
class UserEdit extends Mutation
{
protected $attributes = [
'name' => 'UserEdit',
'description' => 'A mutation'
];
public function type()
{
return GraphQL::type('UserInfo');
}
public function args()
{
return [
'id' => [
'type' => Type::int(),
],
];
}
public function resolve($root, $args, $context, ResolveInfo $info)
{
var_dump($args);exit;
$data = User::find($args['id']);
return $data;
}
}
I use my query string to request the graphql server, then server return my error
{
"data": null,
"errors": [
{
"message": "Variable \"$edit1\" of required type \"Int!\" was not provided.",
"locations": [
{
"line": 1,
"column": 11
}
]
}
]
}
l tried a lot things and read the document on github [https://github.com/Folkloreatelier/laravel-graphql#creating-a-mutation][1]
and read the document of graphql's website ,and change my args definition style in many ways, but all failed, and Strange is l can get args but use static variable like follow
mutation edit{
user_edit(id:1) {
user_name
}
}
and then it worked! l tried to goole but get nothing about this. l think l really need some help
Comparing the docs vs your code, seems in your args method, you needs to specify the arg name, like so:
public function args()
{
return [
'id' => [
'type' => Type::int(),
'name' => 'id'
],
];
}
Ref: https://github.com/Folkloreatelier/laravel-graphql#creating-a-query
Hope this help!
Regards
Reason is because of lower version, if you install laravel-graphql version with '~1.0.0',
go to modify config/graphql.php
// The name of the input that contain variables when you query the endpoint.
// Some library use "variables", you can change it here. "params" will stay
// the default for now but will be changed to "variables" in the next major
// release.
'variables_input_name' => 'variables', // modify here from 'params' to 'variables'

Laravel Collection Return

I am trying to figure out why laravel 5.6 when you have a collection it prints out the json response paginated than if you are using a normal return response with json on a paginated list. So I have right now this returning in my collection.
{
"data": [],
"links": {
"first": "http://local/api/v3/x?=1",
"last": "http://local/api/v3/x?=1",
"prev": null,
"next": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"path": "http://local/api/v3/x",
"per_page": "5",
"to": 3,
"total": 3
}
}
And what I want it to look like is:
{
"current_page": 1,
"data": [],
"first_page_url": "http://local/api/v3/x?=1",
"from": null,
"last_page": 1,
"last_page_url": "http://local/api/v3/x?=1",
"next_page_url": null,
"path": "http://local/api/v3/x",
"per_page": 50,
"prev_page_url": null,
"to": null,
"total": 0
}
I have tried to use this following code in the collection and it doesnt work, I have found a few functions to get the data I Want but I am still missing most of the data.
<?php
namespace App\Http\Resources\V3;
use Illuminate\Http\Resources\Json\ResourceCollection;
class DeedTypeCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\V3\x';
/**
* Transform the resource collection into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'current_page' => $this->currentPage(),
'data' => $this->collection,
'first_page_url' => $this->links['first'],
'from' => $this->meta['from'],
'last_page' => $this->meta['last_page'],
'last_page_url' => $this->links['last'],
'next_page_url' => $this->links['next'],
'path' => $this->meta['path'],
'per_page' => $this->meta['per_page'],
'prev_page_url' => $this->links['prev'],
'to' => $this->meta['to'],
'total' => $this->meta['total']
];
}
}
Why does laravel change the default pagination on the collections and how can I fix this to match the default/second dataset.
Here is where the query is built.
/**
* Function: prepareIndexSelect.
*
* #param: $request - Request sent to controller
*
* Description: Returns queried collection.
*/
public static function prepareIndexSelect($request)
{
$model = new self();
$per_page = 50;
$page = 1;
$order_by = $model->getKeyName();
$sort_by = 'asc';
if ($request->has('per_page')) {
$per_page = $request->input('per_page');
}
if ($request->has('sortBy')) {
$order_by = $request->input('sortBy');
}
if ($request->has('sortByAsc')) {
$sort_by = $request->input('sortByAsc');
}
if ($request->has('page')) {
$page = $request->input('page');
}
return $model->when($request->has('fields'), function ($query) use ($request) {
self::prepareSelectColumns($query, $request->input('fields'));
})->when($request->has('includes'), function ($query) use ($request) {
self::prepareIncludedTables($query, $request->input('includes'));
})->whereSearch($request->input('search'))
->orderBy("{$order_by}", "{$sort_by}")
->paginate($per_page, null, null, $page);
}
Looking over the documentation it appears as though you will need to return your derived collection at the respective endpoint.
Your derived resource collection:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class DeedTypeCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* #param \Illuminate\Http\Request
* #return array
*/
public function toArray($request)
{
return [
'current_page' => $this->currentPage(),
'data' => $this->collection,
'first_page_url' => $this->links['first'],
'from' => $this->meta['from'],
'last_page' => $this->meta['last_page'],
'last_page_url' => $this->links['last'],
'next_page_url' => $this->links['next'],
'path' => $this->meta['path'],
'per_page' => $this->meta['per_page'],
'prev_page_url' => $this->links['prev'],
'to' => $this->meta['to'],
'total' => $this->meta['total']
];
}
}
Your endpoint:
use App\DeedType;
use App\Http\Resources\DeedTypeCollection;
Route::get('/deed-types', function () {
return new DeedTypeCollection(DeedType::all());
});
You may also extend PaginatedResourceResponse this should allow you to override paginationInformation to format the response the way you need it.
protected function paginationInformation($request)
{
return $this->resource->resource->toArray();
}
Then in your derived collection:
public function toResponse($request)
{
return (new MyDerivedPaginatedResourceResponse($this))->toResponse($request)
}

Laravel: Unexpected Behavior of Date in JSON Response

I am making a web service in Laravel which is returning JSON.
I have created an Account model like so:
class Account extends Eloquent {
// The database table used by the model.
// (If not defined then lowercase and plural of class name is consider as a table name)
protected $table = "account";
// define which column can be mass assign
protected $fillable = array("user_id", "account_group_id", "generated_by", "image", "name",
"address", "zip", "area_id", "mobile", "email", "phone", "fax",
"website", "pan", "cst", "tin", "ecc", "iesc", "transport",
"other", "outstanding", "cform", "status", "mitp");
// To prevent column from mass assignment.
protected $guarded = array('id');
// Change Variable for CREATED_AT and UPDATED_AT
const CREATED_AT = 'itp';
const UPDATED_AT = 'utp';
}
I am fetching fields from Account using user_id and returning JSON via Response::json() in my controller
$accountData = Account::select('name', 'status', 'id', 'user_id', 'utp')->where('user_id', Auth::id())->first();
$return = array(
'result' => 'success',
'msg' => 'Login Successfully.',
'data' => $accountData
);
return Response::json($return);
In this, utp behaves as expected and returns a date as a string:
{
"result": "success",
"msg": "Login Successfully.",
"data": {
"name": "Demo",
"status": 0,
"id": 143,
"user_id": 207,
"utp": "2015-07-01 18:38:01"
}
}
However if I take each value separately from the account model like so:
$return = array(
'result' => 'success',
'msg' => 'Login Successfully.',
'data' => $accountData['user_id'],
'account_id' => $accountData['id'],
'utp' => $accountData['utp'],
'usertype' => 'account',
'status' => $accountData['status']
);
Then this gives some unexpected behavior from utp
{
"result": "success",
"msg": "Login Successfully.",
"data": 207,
"account_id": 143,
"utp": {
"date": "2015-07-01 18:38:01",
"timezone_type": 3,
"timezone": "Asia\\/Kolkata"
},
"usertype": "account",
"status": 0
}
Why does this happen with my timestamp field?
Because utp is a Carbon\Carbon instance. Model::toJson (actually Model::toArray, but both are used) handles that usually, and serializes a date to it's usual ISO3601-ish format
For expected behavior, you need to format the Carbon instance.
"utp" => $accountData['utp']->format("Y-m-d H:i:s"),
Alternatively, cast it to a string
"utp" => (string) $accountData['utp'],

Resources