I am using Laravel 8 and want to store a model called "domain." A domain belongs to an organization.
Route
Route::apiResource('organizations/{organization}/domains',
DomainController::class)
->whereUuid(['organization', 'domain']);
As the route is:
{{path}}/organizations/1a52ef80-9f9e-4172-9d8c-e63595ad3712/domains
I have two choices to send the data to the server. First, I can post something like the following.
{
"organization_id": "1a52ef80-9f9e-4172-9d8c-e63595ad3712",
"domain_name": "domain name"
}
Or without the organization_id since it is in the path.
{
"domain_name": "domain name"
}
What is a good practice? With or without the parent in the JSON?
Related
I have 3 tables. 2 of them belong to 1 table.
company:
id
company_info (FK to the id of company_infos)
address (FK to to the id of addresses)
company_info:
id
company_name
owner
address:
id
country
street
number
My relations in the company model:
public function address(){
return $this->hasOne(Address::class, $this->primaryKey, 'address');
}
public function company_info(){
return $this->hasOne(CompanyInfo::class, $this->primaryKey, 'company_info');
}
(My relations do work; I can get the company with their relations in my get request.)
The only way I can think of create a new company is to create company_info and address first and pass their id's to create a new company.
How do I create those 2 models so that the company model gets those 2 models' ids by their relations?
So when I send the data from my front end to Laravel like this to create a new company, how would I make a new company based on this data that's going to fill the other 2 models?
"company_info": {
"company_name": "aCompany",
"owner": "someone"
},
"address": {
"country": "some_country",
"street": "some_street",
"number": 123
}
You would first need to create the company_info and the address data via their models (There is many ways to create a instance of the model this is just the way I prefer, other alternatives include using the new keyword then saving etc.. etc...) like:
$address = Address::create([
// Attributes
]);
// and
$companyInfo = CompanyInfo::create([
// Attributes
]);
which would then have the 2 pieces of data required for creating the Company which can be done the same way as above but set the keys to the respective $address->id and $companyInfo->id
Ex.
Company::create([
// Attributes
]);
If you need any more clarification I am more than willing to elaborate further.
I am trying to create an API call to add data into a custom field that was created on the Sales Order page using AL Extensions. The issue is when I try to do the api call through postman, I am getting "The Property "propertyName" does not exist on type 'Microsoft.NAV.salesOrder'". First of all, I don't even know if the API allows for this, so is it even possible? And secondly, if it is possible, is there a certain way to set up the API call or the Field through the AL Extension?
tableextension 50100 "AddProjectIdToSalesOrder" extends "Sales Header"
{
fields
{
field(50100; "CrmProjectId"; Guid)
{
Caption = 'Crm Project Id';
DataClassification = OrganizationIdentifiableInformation;
}
}
}
pageextension 50100 "AddProjectIdToSalesOrder" extends "Sales Order"
{
layout
{
addlast(General)
{
field("CRM Project Id"; Rec.CrmProjectId)
{
ApplicationArea = all;
ToolTip = 'The Guid of the related Project Record in the CRM environment';
}
}
}
}
This is how I am setting up the field with the AL extension, and for the post call, I am just creating a new Sales Order with a post and the body looks like:
{
"customerNumber" : "10000",
"CrmProjectId" : "random-guid"
}
And the error is "Bad Request": "The property 'CrmProjectId' does not exist on type 'Microsoft.NAV.salesOrder'. Make sure to only use property names that are defined by the type." Any help would be appreciated.
The Sales Order API is a separate page. It is not equivalent to the Sales Order page so you have to modify the API to accomplish what you want.
However the standard API's provided by Microsoft can't be extended.
You are left with two options:
Make a copy of the standard Sales Order API (this involves making a copy of all the linked APIs as well e.g. the Sales Line API).
Create a new API page with the single purpose of updating your new field. Then you would use the standard Sales Order API to create the Sales Order and then update CrmProjectId with a second call to your custom API page.
The title says it all.
If I have something like this:
type Model {
id
name
}
and only specific users should be able to access the ID field. How I can do it?
Same thing if specific users can access only Model > relationship data. How I can hide attributes based on the user capabilities?
You can use #can directive, like:
type Model {
id: ID! #can(...)
}
I have three tables: users, emails,attachments. User table is connected with emails by user_id. Emails table is connected with attachments by email_id.
My question is: How should I make it look eloquent in laravel to get all users their emails and their attachments? (I know how get all user and they emails but I don't know how to add attachments.)
Depending on your database relationship,you may declare a relationship method in your Email model, for example:
// One to One (If Email has only one attachment)
public function attachment()
{
return $this->hasOne(Attachment::class);
}
Otherwise:
// One to Many (If Email contains more than one attachment)
public function attachments()
{
return $this->hasMany(Attachment::class);
}
To retrieve the related attachment(s) from Email model when reading a user using id, you may try something like this:
$user = User::with('email.attachment')->find(1); // For One-to-One
$user = User::with('email.attachments')->find(1); // For One-to-Many
Hope you've already declared the relationship method in the User model for Email model using the method name email.
Note: make sure, you have used right namespace for models. It may work if you've done everything right and followed the Laravel convention, otherwise do some research. Also check the documentation.
I'm encountering an annoying problem with Laravel and I'm hoping someone knows a way to override it...
This is for a system that allows sales reps to see inventory in their territories. I'm building an editor to allow our sales manager to go in and update the store ACL so he can manage his reps.
I have two related models:
class Store extends Eloquent {
public function StoreACLEntries()
{
return $this->hasMany("StoreACLEntry", "store_id");
}
}
class StoreACLEntry extends Eloquent {
public function Store()
{
return $this->belongsTo("Store");
}
}
The idea here is that a Store can have many entries in the ACL table.
The problem is this: I built a page which interacts with the server via AJAX. The manager can search in a variety of different ways and see the stores and the current restrictions for each from the ACL. My controller performs the search and returns the data (via AJAX) like this:
$stores = Store::where("searchCondition", "=", "whatever")
->with("StoreACLEntries")
->get();
return Response::json(array('stores' => $stores->toArray()));
The response that the client receives looks like this:
{
id: "some ID value",
store_ac_lentries: [
created_at: "2014-10-14 08:13:20"
field: "salesrep"
id: "1"
store_id: "5152-USA"
updated_at: "2014-10-14 08:13:20"
value: "salesrep ID value"
]
}
The problem is with the way the StoreACLEntries name is mutilated: it becomes store_ac_lentries. I've done a little digging and discovered it's the toArray method that's inserting those funky underscores.
So I have two questions: "why?" and "how do I stop it from doing that?"
It has something in common with automatic changing camelCase into snake_case. You should try to change your function name from StoreACLEntries to storeaclentries (lowercase) to remove this effect.