How to limit the amount of embedded entity's data in GET operations in api-platform - api-platform.com

Say we have two entities: Question and Answer. They are related as follows:
#[ApiResource]
class Question
{
//...
#[ORM\OneToMany(mmappedBy: "question", targetEntity: Answer::class)]
private Collection $answers;
}
#[APiResource]
class Answer
{
//...
#[ORM\ManyToOne(inversedBy: 'answers')]
#[ORM\JoinColumn(nullable: false)]
private Question $question;
}
We are requested to return Question with only the 5 (five) more recent answers embedded in it, while we could use another endpoint to fetch all the question's answers paginated. How can we do that with API Platform? Thanks in advance for any help.

Related

Swagger documentation for REST response

Good afternoon.
I am trying to generate Swagger documentation but fails to generate clean result if the response is not a simple class.
if we consider a user with family members, the following REST function as a partial documentation
public ResponseEntity<FamilyMembersResponse> getUserFamily(#PathVariable("user_uuid") String UUID) {
...
FamilyMembersResponse response= new FamilyMembersResponse();
...
return new ResponseEntity<>(response, HttpStatus.OK);
}
By partial, I mean that swagger will say that result is of type FamilyMembersResponse (that is correct) but the FamilyMembersResponse class itself is not documented (return without any attributes).
The issue may be that the class FamilyMembersResponse is created within the controler but even with such definitions the class description is always empty:
class FamilyMembersResponse {
#Schema(name = "user" )
User user;
#Schema(name = "family_members" )
List<FamilyMember> family_members;
}
Any idea why? Issue seems "only" on the generation of FamilyMembersResponse "class", not the route
Just found the answer... as in simply posting in SO after looking for hours helped solving the issue (why, that is the question :D).
JsonProperty was expected, .. which kind of makes sense.
#JsonProperty("user")

laravel return number and boolean as string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
in prod version laravel return number and boolean as string !!
Localhost :
{ "age": 24, "is_admin": 0, }
Prod :
{ "age": "24", "is_admin": "0", }
you can use Attribute Casting, it provides functionality similar to accessors and mutators without requiring you to define any additional methods on your model. Instead, your model's $casts property provides a convenient method of converting attributes to common data types.
class MyModel extends Model
{
protected $casts = ['age' => 'integer','is_admin' => 'boolean'];

Update records in intermediate(pivot) table in Laravel in a specific way

I have three table "questionaires", "questions" and "PivotQuestionaires". "questionaires" and "questions" are connected via foreign key through "PivotQuestionaires".
One questionaire *hasMany questions and question *belongsTo certain questionaire.
For example, if i have certain record in "PivotQuestionaires" which states: "Questionaire with id of 2 has questions with ids of 1,2 and 3 and if i want to change that to have only one specific question with for example with id 14. How would i do it?
Or if did it in different direction, if pivot has questionaire with only one question, but i want to update it with several?
What i'm trying to ask, questionaire should have ability to have variable number of questions.
How to achieve such update? Is there a "Laravel" of doing this or should i devise specific logic for it?
Here is screenshot of schema to better visualize
Kudos in advance.
Its a many to many relation between questionnaire and question like
class Questionnaire extends Model
{
public function questions()
{
return $this->belongsToMany(Question::class, 'pivot_questionnaire', 'questionnaire _id', 'question_id');
}
}
class Question extends Model
{
public function questionnaires()
{
return $this->belongsToMany(Questionnaire::class, 'pivot_questionnaire', 'question_id','questionnaire_id');
}
}
So if you want to assign multiple questions to questionnaire you can use attach & sync
$questionIds = [4,5];
$questionnaire->questions()->attach($questionIds);
this will add new associations [4,5] to existing ones like if previously if questionnaire has [1,2,3] then after attach() questionnaire will have [1,2,3,4,5]
$questionnaire->questions()->sync($questionIds);
If you use sync() it will removed all previous associations and add these new associations
Same goes for questions
$questionnaireIds = [4,5];
$question->questionnaires()->attach($questionnaireIds);
// OR
$question->questionnaires()->sync($questionnaireIds);

Angular performance: component exposes public object with 500+ (sub-) members

I am using angular 4 with Typescript.
I have a static class with lots of public static/constant string members whose values will never change. This class is exposed on many of my components in order to have access to the members from the templates:
Static class:
export class Foo {
public static foo1: string = "foo 1";
// ...
public static foo1000: string = "foo 1000";
}
Example component:
export class FooComponent {
public foo: Foo = Foo;
}
Example usage in component template:
<div>{{foo.foo123}}</div>
<div>{{foo.foo321}}</div>
The question is:
Is this good design regarding performance / change detection?
Is there a way to prevent angular from checking (during change detection) specific members (as they don't change anyway)?
Or in other words: Can I expose a public member/object with many (string) members in my components without having a negative impact on performance?
By the way: I deliberately don't want to go into details about what and why in order to keep the question simple.
This has been answered here in the meanwhile.
Short answer: There is no problem with big objects, as angular only checks fields which are actually used/referenced in the template.

How to use softdelete withTrashed in Laravel [duplicate]

This question already has an answer here:
How to use withTrashed when I'm querying using eager loading?
(1 answer)
Closed 5 years ago.
I'm building an application in laravel where I've two models company and contact with belongs to relationship as following:
class Contact extends Model
{
use SoftDeletes;
public function company()
{
return $this->belongsTo('App\Company');
}
}
I'm using softdeletes in company model too, Now suppose even if any company is deleted, I want the contact to be shown with there company details.
I'm trying to do something like this to retrieve the data:
$allData = Contact::with('company')->withTrashed()->get();
It is not working, it is only showing company details which are not deleted. Any ideas to overcome this problem?
Try this solution:
Contacts with trashed:
$allСontacts = Contact::withTrashed()->get();
Without:
$contacts = Contact::all();
Get companies:
$companies = $contacts->company()->withTrashed()->get();
$allCompanies = $allСontacts->company()->withTrashed()->get();

Resources