Voyager dropdown relationship ajax call - ajax

How i can override model query which populate a belongsto relationship dropdown via ajax call.
Tha call is like http://santinisacri.local/admin/santini-soggetti/relation?type=santini_soggetti_hasone_santini_soggetti_relationship&method=add&page=1 and the response is a JSON.
I want to add a where clausole.
I have tried in model but nothing. I need to create a new controller?

I resolved by a overriding controller and return an array like:
$results['results'] = $soggetti;
return $results;

Related

Pagination on Django Rest Framework when the data does not come from a model from the database

I'm trying to create an endpoint who returns the values from an Enum, but I need to make it with pagination because the UI library we use was designed to received exactly the same format that Django Rest Framework return when we read from a database.
The problem is that I cannot make the pagination to work. No matter which base class I use (ApiView, ListView, etc), the Response return doesn't has the count, next, previous and results fields (in results is where the real json should be).
I tried to overwrite pagination with pagination_class on the view class, and also creating a custom pagination class based on PageNumberPagination, but nothing.
Also, I tried to create a custom get_paginated_response method on my custom pagination class, but when I debug it looks like the method is not called.
What I'm doing wrong? I imagine it should be a way to tell DRF "take this data, even if is not from a model, and return that as a paginated list of fields"
When this happened to me in the past, I just called paginate_queryset with the enum and the request. Then, you need to return the response of get_paginated_response(page). So, something like this:
class SampleViewSet(views.ViewSet):
def list(self, request):
values = [e.value for e in MyEnum]
paginator = YourPaginationClass()
page = paginator.paginate_queryset(values, request)
if page is not None:
return paginator.get_paginated_response(page)
return Response(data)

How to pass Eloquent data with relation to vue template

i'm trying to pass model relation from blade, to vue.js template.
I have 2 models with relation, shortly:
class Emails extends Model
{
public function template()
{
return $this->belongsTo(Template::class);
}
}
and the other one with has many relation.
With such request $mails = Emails::all(); i can reach templates using blade after foreach like $mail->template.
But if i pass it to vue.js template <sometemplate :mails="{{$mails}}", after foreach mail.template would be empty.
I know, that i'm doing something wrong, but can't find a solution.
Sorry, found my solution. I had to add with('template') to my request

Eloquent find() with joins and eager loading

I want to retrieve one record via the time-tested method of this URL:
public/api/laptop/1
hitting this route:
Route::get('laptop/{id}', 'LaptopController#getLaptop');
then this controller method:
$laptop = Laptop::find($id)->addJoins()->selectListCols()->with('earmarks', 'movements')->get();
return $laptop;
Problem is this doesn't work (it returns every record). To make it work I have to do this:
$laptop = Laptop::where('laptops.id', $id)->addJoins()->selectListCols()->with('earmarks', 'movements')->get();
return $laptop;
But I'm just wondering why find() doesn't work? earmarks and movements are Many-To-One models, by the way.
find() is just a shortcut for where()->first() so it will return an object and Query Builder methods will not work with it:
User::find(1); // Will return User object with ID = 1.
That's why you need to use where(), which returns Query Builder object, so you can use with() and other builder methods to build your query.

Yii2: ActiveRecord How to unload / unset a model of all(some) attributes?

Yii2 ActiveRecord has a method to automatically load a form data into a model using load() which is very good as it safely loads the model with data, However I am not able find a equivalent method to unload the model of all the attributes.
i.e. Is there a method to unset all attributes of a model in Yii2, like the unSetAttributes() method in Yii 1.x ?
Currently the only way to do this seems to be either
$model->setAttributes(['attribute1'=>NULL,'attribute2' => NULL ... ]);
or
foreach ($model->attributes as $attribute) {
$model->$attribute = NULL;
}
Edit: To clarify in response to Samuel Liew's answer, while at this point I only wanted to unset all attributes which I could do by reiniting the model, I would also like to control which attributes are getting reset, which unSetAttributes provided
You could simply create a new instance of the model.
$model = new MyModel;
Or as you can see, unsetAttributes in Yii 1 is like this, you could simply implement it in your base model:
public function unsetAttributes($names=null)
{
if($names===null)
$names=$this->attributeNames();
foreach($names as $name)
$this->$name=null;
}

controller action returning View(tuple) asp.net mvc 3

Is there a way for the controller to pass a tuple as a model to the View?
Something like this:
return View(Tuple<LandingPage, Models.FilterModel>(landingPage, filter));
Thank you
Yes, you just need to instantiate the Tuple:
return View(new Tuple<LandingPage, Models.FilterModel>(landingPage, filter));
Although you're probably better off creating a composite view model type which contains properties for the landing page and the filter model.

Resources