Quick question,
With Laravel I want to create a json output.
For this I use the laravel resource,
The standard resource looks like this
return [
'id' => 'test',
'first' => 'test',
'last' => 'test',
];
is there also a way to make the resource looks like something like this?
what would be the best way to accomplish this.
return [
'common' => [
'status' => 'succes',
'message' => 'succes',
],
'data' => [
'm_users' => [
'0' => [
'id' => '1',
'first' => 'test',
'last' => 'test',
],
'2' => [
'id' => '2',
'first' => 'test',
'last' => 'test',
],
'3' => [
'id' => '3',
'first' => 'test',
'last' => 'test',
],
],
],
];
Is there a way to changes the standard output that comes with the resource function?
Thank you.
You can additional data to your response like this.
return (new UsersResource($contacts))
->additional([
'status' => $status,
'message' => $message,
]);
Related
I have this code:
Controller
$this->crud->addField(
[
'name' => 'schedule',
'label' => 'Schedule',
'type' => 'repeatable',
'fields' => [
[
'name' => 'day',
'label' => 'Day',
'type' => 'select_from_array',
'options' => Day::titles(),
'allows_null' => false,
],
[
'name' => 'range',
'label' => 'Range',
'type' => 'select2_from_array',
'options' => $this->getScheduleRange(),
'default' => $this->getDefaultScheduleRange(),
'allows_null' => false,
"allows_multiple" => true,
],
],
]
);
Model
protected $casts = [
'schedule' => 'array',
];
stored data in DB(schedule column):
[{"day": "1", "range[]": ["1:30:00", "2:00:00"]}]
But selected data not showing on the page when it is multi selected.
UPD:
After Pedro's recomandation it's not help me. In DB it's storing as:
[{"day": "1", "range[]": ["0", "1"]}]
viper.
when using multiple with default please use numeric keys like:
'options' => [0 => 'option 0', 1 => 'option 1'],
'default' => [0,1]
I think Backpack can do something here to improve multiple string keys like
'options' => ['option_0', 'option_1'],
'default' => ['option_0', 'option_1']
I will open an issue to discuss this in the package repository.
Thanks
Pedro
I have two different Guzzle post requests that I am trying to merge (solely because they basically do a united job and should be performed together).
Initially I have my donation data:
'donation' => [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
],
And then I have my files that go with it, which are basically two different PDFs that are enabled or disabled for donors, sometimes they have both. I know the multipart would look something like below, but I'm not sure.
foreach ($uploadDocs as $doc) {
'multipart' => [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id',
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
],
}
Since I've usually only handled one file at a time and I'm not sure how to merge the first block of code with the second for an appropriate Guzzle post request.
You can try this:
$donationData = [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
];
$multipart = [];
foreach ($uploadDocs as $doc) {
$multipart[] = [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id,
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
];
}
Than perform your request:
$r = $client->request('POST', 'http://example.com', [
'body' => $donationData,
'multipart' => $multipart,
]);
Request can contain field coord ({x: 1, y: 2}) or not contain it. For example:
Correct (without coord):
[
'another_param' => 'value',
],
Correct:
[
'another_param' => 'value',
'coord' => [
'x' => 1,
'y' => 2,
],
],
Invalid (wrong format of coord):
[
'another_param' => 'value',
'coord' => [
'x' => 1,
],
],
Whether it can be written by standard rules (without custom and closures).
My attempt:
'rules' => [
'coord' => 'array',
'coord.x' => 'required',
'coord.y' => 'required',
],
But if a request don't contain coord then Error: The coord.x field is required.
You can use the sometimes validation rule so it only applies when the field is present.
'rules' => [
'coord' => 'sometimes|array',
'coord.x' => 'required',
'coord.y' => 'required',
],
You can require the fields when the array is provided using required_with:
'rules' => [
'coord' => 'sometimes|array|min:1',
'coord.x' => 'required_with:coord',
'coord.y' => 'required_with:coord',
],
The sometimes rule allows this field to be discarded and min:1 ensures when this coord field is provided, it's not an empty array.
You can try validating the array fields as coord.*.x
I mean:
'rules' => [
'coord' => 'array',
'coord.*.x' => 'required',
'coord.*.y' => 'required',
],
Then if coord array has an element, x and y will be required
Not sure if it's still actual but here's how I solved it:
'rules' => [
'coord' => ['nullable', 'array'],
'coord.x' => 'required_unless:coord,null',
'coord.y' => 'required_unless:coord,null',
],
I have normal ModelSearch with ActiveDataProvider, and I would like to add a virtual/related attribute to sorting in gridview. If I'm doing with setSort, and I'm adding this only attribute, then all other attributes are not sortable any more. Is there a built-in way to add an attribute to Sort? Thanks a lot!
public function search($params) {
$query = Za::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder' => ['aonr' => SORT_ASC]],
'pagination' => [
'pageSize' => 15,
],
]);
$dataProvider->setSort([
'attributes' => [
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
]
]);
$this->load($params);
...
}
You can set the sortable attributes with the setSort method, but in this case you need to set all the columns you want to sort, not just the column from the relation.
If you want to add one column you can try this (merging the currently existing sort attributes with the new one):
$dataProvider->setSort([
'attributes' => array_merge(
$dataProvider->getSort()->attributes,
[
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
]
),
]);
or you can add the missing attributes/columns by hand (which is a far better idea)
$dataProvider->setSort([
'attributes' =>
[
'lwnr' => [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
],
// Other attribute
'id' => [
'asc' => ['id' => SORT_ASC],
'desc' => ['id' => SORT_DESC],
],
...
],
]);
Another way:
$dataProvider->getSort()->attributes['lwnr'] = [
'asc' => ['lwnr' => SORT_ASC],
'desc' => ['lwnr' => SORT_DESC],
'label' => 'lwnr',
'default' => SORT_DESC,
];
I have the following input:
private function addBirthdayElement()
{
return $this->add(
array(
'type' => 'DateSelect',
'name' => 'x_bdate',
'options' => [
'label' => 'astropay_birthday',
'label_attributes' => array(
'class' => 'astropay-label'
),
'create_empty_option' => true,
'render_delimiters' => false,
],
'attributes' => array(
'required' => true,
'class' => 'astropay-input',
)
)
);
}
It has the following filter:
public function addBirthdayFilter()
{
$time = new \DateTime('now');
$eighteenYearAgo = $time->modify(sprintf('-%d year', self::EIGHTEEN_YEARS))->format('Y-m-d');
$this->add(
[
'name' => 'x_bdate',
'required' => true,
'validators' => [
[
'name' => 'Between',
'break_chain_on_failure' => true,
'options' => [
'min' => 1900,
'max' => $eighteenYearAgo,
'messages' => [
Between::NOT_BETWEEN => 'astropay_invalid_birth_date_18',
Between::NOT_BETWEEN_STRICT => 'astropay_invalid_birth_date_18',
]
]
],
[
'name' => 'Date',
'break_chain_on_failure' => true,
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
],
],
]
);
return $this;
}
However, putting an empty date, I get the error message defined for:
Date::INVALID_DATE
But it's not the overridden one. The break_chain_on_failure works for the two validators I have defined, but the default Zend message is always there. For example I get this as an error in my form:
The input does not appear to be a valid date
astropay_invalid_birth_date_18
How can I display only the overidden error messages and 1 at a time?
You can use a message key in your validator configuration instead of a messages array to always show a single message per validator.
For example, replace this:
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
with this one:
'options' => [
'message' => 'Invalid birth date given!',
]