Yii2 cant sort in calculated values - sorting

In my model i have
public function getRating(){
$rating=ShopRating::getRating($this->id);
if($rating)
return $rating->rating;
else
return 0;
}
And in search model i have created a virtual column like
public $rating;
and marked it as safe
[['rating'], 'safe']
And in the controller defined my sort
private function getSort(){
$sort = new Sort([
'attributes' => [
'date-asc' => [
'asc' => ['date' => SORT_ASC],
'label' => Inflector::camel2words('Old to New'),
],
'date-desc' => [
'desc' => ['date' => SORT_DESC],
'label' => Inflector::camel2words('New to Old'),
'default' => SORT_DESC,
],
'rating-desc' => [
'desc' => ['rating' => SORT_DESC],
'default' => SORT_DESC,
],
'rating-desc' => [
'desc' => ['rating' => SORT_DESC],
'default' => SORT_DESC,
],
],
]);
return $sort;
}
like this.
$sort=$this->getSort();
And my date-wise sort is working perfectly.But the sorting on the calculated value doesn't work. Meaning sorting with the rating asc and desc is not working. How to can I make it to work

Related

Symfony validation/comparison between two dates in a form

So I want to create a form with two dates. I want those dates to have constraints, that check if one of each is bigger than the other. This way I can get a valid time span.
I build a custom FormType like this:
class MainFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add( 'date_from', DateType::class, [
'constraints' => [
new Assert\NotBlank()
]
])
->add( 'date_to', DateType::class, [
'constraints' => [
new Assert\NotBlank(),
new Assert\GreaterThanOrEqual([
'value' => $builder->get('date_from'),
'message' => 'datepickers.to_less_than_from'
])
]
]);
}
}
At this point symfony does not throw an error, but the validation wont work either.
Is there any way to compare two dates, that are located in the same form, via Symfonys validator?
You can do this with this code
->add('dateToPublish', DateTimeType::class, array(
'label' => 'notifications.date_to_publish',
'required' => true,
'widget' => 'single_text',
'constraints' => [
new NotBlank(),
new DateTime(),
]))
->add('dateToEnd', DateTimeType::class, array(
'label' => 'notifications.date_to_end',
'required' => true,
'widget' => 'single_text',
'constraints' => [
new NotBlank(),
new DateTime(),
new GreaterThan([
'propertyPath' => 'parent.all[dateToPublish].data'
]),
]));
As you can see I'm making a constraint on dateToEnd to be greater than dateToPublish
Hopes this helps you

Laravel: Add custom data to resource

First I get the translator by his id using this line of code
$translator = Translator::where('id', $translator_id)->first();
Then I send a notification to him by this code:
$response = Http::withHeaders([
'Authorization' => 'key=myKey',
'Content-Type' => 'application/json'
])->post('https://fcm.googleapis.com/fcm/send', [
"notification" => [
"title" => "title",
"body" => "body",
],
"data" => [
"title" => "title",
"body" => "body",
],
"to" => $token,
]);
Everything works fine but my problem is that when I return the TranslatorResource I want to add the notification response to it, so I do this in my controller
$resource = new TranslatorResource($translator);
$resource->notif = $response;
return $resource;
And in TranslatorResource I have this code:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'phone' => $this->phone,
'cv' => $this->cv,
'specialization' => $this->specialization,
'tr_languages' => $this->tr_languages,
'all_languages' => $this->all_languages,
'isVerified' => $this->isVerified == 0 ? false : true,
'isActive' => $this->isActive == 0 ? false : true,
'completed_orders' => $this->completed_orders,
'canceled_orders' => $this->canceled_orders,
'rejected_orders' => $this->rejected_orders,
'current_orders' => $this->current_orders,
'isTranslator' => true,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
But I only get the data specified in the resource, the notif key isn't added, anyone know how to add this data to my resource when I return it ?
You can use additional method provided by laravel.
return (new TranslatorResource($translator))->additional(['notif ' => $response]);
Reference: Eloquent: API Resources
You can look for the section Adding Meta Data When Constructing Resources.

How can I do store multidimensional array base on name in Laravel collection?

In image you can see name and I want to store on base of name
Array index 0 & 2 have the same name. And I want to store same name in one array
Like This:
Josue Koepp DDS => {
id=>2,
item_name=>"Domenic Labadie"
},
{
id=>0,
item_name=>"Prof. Jakayla Willms"
}
}
You can use groupBy()
$collection = collect([
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
['account_id' => 'account-x11', 'product' => 'Desk'],
]);
$grouped = $collection->groupBy('account_id');
$grouped->toArray();
/*
[
'account-x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'account-x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/
In your case:
$lists = $lists->groupBy('name')->toArray();

How can I create a Laravel Resource relationship for a belongsTo?

I have created a UserResource that is successfully returning all of my user attributes, including the organization it belongs to. It looks something like this:
Resources/User.php
return [
'type' => 'users',
'id' => (string)$this->id,
'attributes' => [
'name' => $this->name,
'email' => $this->email,
...
'relationships' => [
'organization' => $this->organization,
],
];
In my User model, there is a belongsTo relationship for User->Organization.
Instead of returning the actual organization model, I'd like to return the organization resource.
For example, an organization hasMany locations:
Resources/Organization.php
return [
'type' => 'organizations',
'id' => (string)$this->id,
'attributes' => [
'name' => $this->name,
...
'relationships' => [
'locations' => Location::collection($this->locations),
],
];
I can successfully return the collection of locations that belong to the organization. I have not been able to return a belongsTo relationship.
I've tried:
Resources/User.php
'relationships' => [
'organization' => Organization::collection($this->organization),
],
// or this
'relationships' => [
'organization' => Organization::class($this->organization),
],
// or this
use App\Http\Resources\Organization as OrganizationResource;
...
'relationships' => [
'organization' => OrganizationResource($this->organization),
],
How can I return a single model as a related resource? Thank you for any suggestions!
Have you tried it with the new keyword?
'relationships' => [
'organization' => new OrganizationResource($this->organization),
],

How to add fields which are not available in database?

I am not able to add field which is not available in my database
I have tried adding
$this->crud->addFields([
[
'name' => 'coupon_type',
'label' => 'Coupon For',
'type' => 'select_from_array',
'options' => [
// Options
],
'allows_null' => true,
'default' => 1,
'attributes' => [
'id' => 'coupon_type'
]
]
]);
I want to add fields in my create page.
You can do this by defining accessors for your "virtual" attributes
public function getIsAdminAttribute()
{
return $this->attributes['admin'] == 'yes';
}
and defining the appends on your model
protected $appends = ['is_admin'];
Find everything in the docs here:
https://laravel.com/docs/5.8/eloquent-serialization#appending-values-to-json

Resources