If I need to have two different version (say one with name & id and another with name, id, xyz etc) of API Resource for the same model, do I need to create two different Resources for the same model? I can only see one method toArray() inside an API Resource.
Related
I am working on a multi-country application where I have different db schemas for each country. I want to understand how can i call a different query file based on the country a user belongs to.. One way i thought of was maintaining different query files for different countries (IqueryConstantsIndia, IQueryConstntChina etc) and to check the country of the user in the constructor of the repository class and then call the query file based on the same but how do i return a class name is what i am stuck with.. Can anyone help on this or if you think there is a better way of implementing this.
I'm actually learning Django Rest Framework and I have this project that involves multiple user types so for example it consists of Students, Teachers and Admins each one with his attributes so I created a User model that inherits from AbstractUser and 3 other models for each type where each type has a foreign key of it's corresponding user. So here comes the problem how can I use Django-Rest-Auth library with these user types so for example when I use the register endpoint how can I modify it in order to create a Student in the students model based on the request data ?
I would appreciate any ones help.
Thanks in advance
I have one model user and one table against that model users I have different roles for user as patient, hospital and so on, each user have different fields and values to enter during registration so I have created different routes and controllers for different roles.
Now I'm facing problem during authorization process how I can authorize. I have only one model user so I can create only one policy called UserPolicy and use the $this->authorize method in the UserController.
I have also other controllers as PatientController, HospitalController which all bound to the one table and model called user and fetching the record only based on the user type. Now how can I create the policies for them and use the $this->authorize method in the Hospital, Patient controllers?
you are using the same table and you require different data from each kind if user ?
anyway if that the situation , you can create a type field in the users table , then create 2 middlewares , in your middleware check the type of the user then throw an exception or make him pass
I'm just beginning to learn Laravel and the MVC way of thinking, so please bear with me. How can I approach this, and is there a standard in where to put the processed data?
I would fetch for example, a department_id (e.g. ABC) and personnel_number (e.g. 1234) from the database and I want to piece them together as one (e.g. ABC1234). So I want to prepare an object property code after an Eloquent Personnel is prepared.
Should I just alter the Eloquent object after it is created like:
$personnel = App\Personnel::find(1);
$personnel->code = $personnel->department_id . $personnel->personnel_number;
//or
$personnel->code(); //returns ABC1234
Or should I process the data in the Personnel model and fit everything into a new object? Like:
$personnel_data = new PersonnelData(App\Personnel::find(1));
//so I can access the personnel code using this, which is processed in constructor
$personnel_data->code;
//and access the model using this:
$personnel_data->model;
Or some other way?
There must be some general practice I can follow, because there are times when this is needed, e.g.:
site URL when you only store a part of it, e.g. Google Drive file URL when you only have the file ids
human readable time when you only store the timestamps
person's full name when you store their first name and last name separately
...
Is there a common/standard way to prepare these beforehand and not process them only when you need them?
If you just want a property that is a concatenated string of other properties on the same model, then overload the attribute in the model:
public function getCodeAttribute() {
return $this->department_id . $personnel->personnel_number;
}
Then you can just call $personnel->code
A user requests latest news , the news get data from multiple sources (posts, users , photos, comments) . How would you model the news?
Is it good to have a gateway that couples these tables + a service that gets the data from the coupled gateway and handles the data as a response ? Or a domain model that couples the other models (this would mean to add in one of those gateways a joined long query that , in my opinion needs a separate gateway ).
I would create a NewsService, as it would be coordinating the creation of the news, but would defer any specific responsibility to the appropriate model. If it's a news feed, like in facebook, I would create another model, NewsItem which is created upon the entry of a new post, photo etc. This way, the responsibility of build the news would fall more into your domain model and your NewsService would be really just orchestrating the construction of the list. You could even, depending on your app, just use a NewsRepository.