I'm making an API using Eloquent: API Resources.
This is my article Resource:
class Article extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'slug' => $this->slug,
'name' => $this->name,
'comments' => $this->when($this->showComments(), function () {
$comments = config('eblogger.models.comment')::where([
'commentable_type' => get_class($this),
'commentable_id' => $this->id,
'parent_id' => 0,
])->orderBy('created_at', 'desc')->get();
$paginator = makePaginationCollection($comments, route('blog.comments'));
return CommentResource::collection($paginator);
}),
];
}
}
It's an article with comments.
I want to get pagination with my comments, so i call a custom helper
function makePaginationCollection($collection, $path)
{
$request = request();
$page = request('page', 1);
$perPage = config('settings.items_by_pages');
$paginate = new \Illuminate\Pagination\LengthAwarePaginator(
$collection->forPage($page, $perPage),
$collection->count(),
$perPage,
$page,
['path' => $path]
);
return $paginate;
}
Update : this my resource collection
class CommentCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
];
}
}
but when i inspect the response in the devtools, i see
I tried with CommentCollection class without success.
Do you have an idea?
Thanks
My solution
finally, thanks to #Rwd, i found this solution but I think it is possible to do better:
This is my article Resource:
public function toArray($request)
{
return [
// ...
'comments' => $this->when($this->showComments(), function() {
return new CommentCollection(
$this->comments()
->orderBy('created_at', 'desc')
->paginate(config('settings.items_by_pages'))
->withPath(route('blog.comments'))
);
})
];
}
And this is my resource collection :
public function toArray($request)
{
return [
'data' => $this->collection,
'links' => $this->resource,
'meta' => $this->resource
];
}
Related
I need to get my data with pagination when I use collection.
Couldn't find any way, and nothing works that written on documents.
Here's my controller;
...
$data = $process->paginate(30);
$data = OrderResource::collection($data);
And here's my resource:
<?php
namespace App\Http\Resources;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Resources\Json\JsonResource;
class OrderResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
$user = Auth::user();
return [
"id" => $this->id,
"customer" => $this->customer,
"vehicle" => $this->vehicle,
"basket" => $this->basket,
"total" => money_formatter($this->total),
"discount" => $this->discount,
"net_total" => money_formatter($this->net_total),
"status" => $this->status,
"payment_type" => $this->payment_type,
"main_name" => $this->vehicle->fleet_id ? $this->vehicle->fleet->title : ($this->customer->company_id ? $this->customer->company->title : $this->customer->fullname),
"sub_name" => $this->vehicle->fleet_id ? ($this->customer->company_id ? $this->customer->company->title : $this->customer->fullname) : '',
"created_at" => Carbon::parse($this->created_at)->formatLocalized('%a, %d %B %Y'),
];
}
}
You can add a macro inside your AppServiceProvider.php for this, inside the boot method.
/**
* Paginate a standard Laravel Collection.
*
* #param int $perPage
* #param int $total
* #param int $page
* #param string $pageName
* #return array
*/
Collection::macro('paginate', function ($perPage = 15, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
And then you can use it like this (assuming $data is a regular laravel collection)
$data = $data->paginate(50);
dd($data);
You can't add any metadata (pagination links) with the collection method. First create a ResourceCollection with php artisan make:resource -c OrderCollection.
Then, in that newly created file, you can do the following.
class OrderCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
$collection = [
'data' => OrderResource::collection($this->collection)
];
if ($this->resource instanceof \Illuminate\Pagination\LengthAwarePaginator) {
$collection['pagination'] = [
'current_page' => $this->resource->currentPage(),
'last_page' => $this->resource->lastPage(),
'first_page_url' => $this->resource->url(1),
'last_page_url' => $this->resource->url($this->resource->lastPage()),
'prev_page_url' => $this->resource->previousPageUrl(),
'next_page_url' => $this->resource->nextPageUrl(),
'from' => $this->resource->firstItem(),
'to' => $this->resource->lastItem(),
'total' => $this->resource->total(),
'per_page' => $this->resource->perPage(),
'path' => $this->resource->path(),
];
}
return $collection;
}
}
dd(json_encode(new OrderCollection(Order::paginate(3)), JSON_PRETTY_PRINT));
I'm trying to write a test unit for the route 'store' in my controller, pass a fake file to be tested too through the validator of my method, but all i got is that the data is not a file :
Illuminate\Foundation\Testing\TestResponse {
+baseResponse: Illuminate\Http\JsonResponse {
#data: "{
"message":"The given data was invalid.",
"errors":{"invoice":["The invoice must be a file."]}
}"
Code :
Test :
$data = factory('App\Domain\X\X')->raw(['creator_id' => $user->id]);
$data['invoice'] = UploadedFile::fake()->create('invoice.xlsx');
$response = $this->json('POST', route('x.store', $data));
Controller :
public function store(XXXRequest $request)
{
...
Request :
class XXXRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required',
'invoice' => 'nullable|file',
];
}
try this:
Test:
$data = factory('App\Domain\X\X')->raw(['creator_id' => $user->id]);
$data['invoice'] = UploadedFile::fake()->create('invoice.xlsx');
$response = $this->json('POST', route('x.store', $data), [
'name' => 'abc',
'invoice' => $data['invoice']
]);
Just create an UploadedFile from a local file and add it to your request:
use Illuminate\Http\UploadedFile;
$filename = public_path('tests/invoice.pdf');
$file = new UploadedFile($filename, 'invoice.pdf', 'application/pdf', filesize($filename), null, true);
$this->post('/', [
'invoice' => $file,
]);
You can read about testing file uploads here or visit official documentation
/**
* Example
*
* #test
*/
public function test_correct_file_uploading(): void
{
// Set fake storage ('local', 's3', etc)
Storage::fake('local');
$response = $this->json('POST', '/path_to_your/controller/method', [
'file' => UploadedFile::fake()->create('invoice.xlsx', 1024)
]);
// Assert response successful
$response->assertSuccessful();
// Assert the file was stored
Storage::disk('local')->assertExists('invoice.xlsx');
}
i have some problem...
here is my code..
i can't get what i swrong with my code.....
here is the error
here is my user class
this is the full DashboardController
/**
* '/home' calls this route
*
* #param none
* #return view dashboard
*/
public function index()
{
$this->permission();
$data = [
'pagetitle' => 'Dashboard',
'permission' => Session()->get('permission'),
'name' => Auth::user()->name,
];
return view('dashboard',$data);
}
/**
* Checks if session has permission in it if not they adds to it
*
* #param null
* #return null
*/
private function permission()
{
if (!Session()->has('permission')) {
$permission = User::find(Auth::user()->id)->details;
$permission_arr = [
'department' => $permission->permission_department,
'asset' => $permission->permission_asset,
'users' => $permission->permission_users,
];
Session()->put('permission', $permission_arr);
}
}
}
i have no idea how solve it..
any help would be great..
You get this kind of problem for you are getting only access of details column From your User table . Remove the details from $permission = User::find(Auth::user()->id);
private function permission(){
if (!Session()->has('permission')){
$permission = User::find(Auth::user()->id);
$permission_arr = [
'department' => $permission->permission_department,
'asset' => $permission->permission_asset,
'users' => $permission->permission_users,
];
Session()->put('permission', $permission_arr);
}
}
Note I have only remove the details object from your permission variable
I have table comments with column parent_id.
And this is content of CommentsTable.php:
namespace App\Model\Table;
use App\Model\Entity\Comment;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Comments Model
*/
class CommentsTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config)
{
$this->table('comments');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Posts', [
'foreignKey' => 'post_id',
'joinType' => 'INNER'
]);
$this->belongsTo('ParentComments', [
'className' => 'Comments',
'foreignKey' => 'parent_id'
]);
$this->hasMany('ChildComments', [
'className' => 'Comments',
'foreignKey' => 'parent_id'
]);
}
/**
* Default validation rules.
*
* #param \Cake\Validation\Validator $validator Validator instance.
* #return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
->requirePresence('body', 'create')
->notEmpty('body')
->requirePresence('path', 'create')
->notEmpty('path')
->add('status', 'valid', ['rule' => 'numeric'])
->requirePresence('status', 'create')
->notEmpty('status')
->add('created_at', 'valid', ['rule' => 'datetime'])
->requirePresence('created_at', 'create')
->notEmpty('created_at')
->add('updated_at', 'valid', ['rule' => 'datetime'])
->requirePresence('updated_at', 'create')
->notEmpty('updated_at');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* #param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* #return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['user_id'], 'Users'));
$rules->add($rules->existsIn(['post_id'], 'Posts'));
$rules->add($rules->existsIn(['parent_id'], 'ParentComments'));
return $rules;
}
}
I want to build rule for field parent_id: exist in ParentComments or equal to 0.
Can you help me?
Thank you very much.
Rules are just callable functions or callable classes. The existsIn() function is just an alias for the ExistsIn class. We can use the to our advantage:
...
use Cake\ORM\Rule\ExistsIn;
class CommentsTable extends Table
{
...
public function buildRules(RulesChecker $rules)
{
...
$rules->add(
function ($entity, $options) {
$rule = new ExistsIn(['parent_id'], 'ParentComments');
return $entity->parent_id === 1 || $rule($entity, $options);
},
['errorField' => 'parent_id', 'message' => 'Wrong Parent']
);
return $rules;
}
}
I have a JsonResource of Post that should return a single post. But after joining some other data I get this error: array_merge_recursive(): Argument #2 is not an array.
This does not work:
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($slug)
{
// $post = Post::findOrFail($id);
$post = Post::where('slug', $slug)->first();
// return single post as resource
return new PostResource($post);
}
When I directly return $posts, I get a json back, almost fine. But it doesnt contain the joined data comment.
Here is the class Post extends JsonResource.
public function toArray($request)
{
// return parent::toArray($request);
$img = '.'.pathinfo('storage/'.$this->image, PATHINFO_EXTENSION);
$imgName = str_replace($img,'', $this->image);
$img = $imgName.'-cropped'.$img;
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'excerpt' => $this->excerpt,
'image' => asset('/storage/' . $this->image),
'image_small' => asset('storage/' . $img),
'author_id' => $this->author_id,
'category_id' => $this->category_id,
'seo_title' => $this->seo_title,
'slug' => $this->slug,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'status' => $this->status,
'featured' => $this->featured,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user' => User::find($this->author_id),
'commentCount' => $this->comment->where(['status' => 1, 'id_post' => $this->id])->count(),
];
}
// **Big mistake below here**:
public function with($request)
{
// return [
// 'version' => '1.0.0',
// ];
}
Model:
class Post extends Model
{
public $primary_key = 'id';
public $foreign_key = 'id_post';
public function user()
{
return $this->belongsTo('App\User', 'id_author', 'id');
}
public function comment()
{
return $this->belongsTo('App\Comment', 'id', 'id_post');
}
}
Why do I get a warning about array_merge_recursive()?
I wan't reproduce issue with your code, but - are you sure you included everything? Looking at https://laravel.com/docs/5.6/eloquent-resources#writing-resources it's possible to define additional data data will be returned too like this:
/**
* Get additional data that should be returned with the resource array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function with($request)
{
return [
'meta' => [
'key' => 'value',
],
];
}
So I was able to reproduce the issue when I added to this Post resource class the following method:
public function with($request)
{
return 'test';
}
as you see it's returning just string and not array and then I was getting same error as you did.
But when I didn't have this method implemented at all or when I return just an array, everything is fine.
So to sum up - make sure you don't have with method defined that returns something else than array.