How do I concatenate 2 fields from a related table in a Django Filter Form? - django-forms

I have 2 models, Composer and Piece with a one-to-many relationship (one composer has many pieces).
I have a form filter that gives 2 dropdown controls from which to select Composer and Instrumentation (from the Piece model) in order to filter the pieces belonging to the selected composer.
The Composer dropdown only shows the surname of the composer, which is really confusing when you have a number of generations of composer in the same family e.g. J.S. Bach, C.P.E Bach etc. The dropdown filter control only shows multiple instances of the surname Bach so the user has no idea which is which. Is it possible to show each composer in the dropdown as "surname, firstName"?
The filter is as follows:
import django_filters
from .models import Piece
class PieceFilter(django_filters.FilterSet):
class Meta:
model = Piece
fields = ['composer', 'instrumentation']
I am only importing the Piece model as composer is a related table but I think I probably need to also import the Composer table but not sure. Any help would be greatly appreciated.

This was easy after someone showed me. All I needed to do was to add the following to the composer model:
def __str__(self):
return self.surname +', '+ self.name

Related

Eloquent - Adding custom field like timestamps in all model by default with logged user data

I would like to add to all models on my Laravel project two custom fields called updated_by and created_by, automatically filled and saved on db with the name of the logged user (I'm using JetStream), exactly like the fields updated_at and created_at.
I wonder if there is a clean procedure, before starting to add the fields on model one-by-one, and handle them in the controllers, in every store/update function I have. Extending Model and using the new class for every models seems a good solution, but I'm not really sure. And I do not know how and where to fill one-time the fields with logger user data.
Thanks, I'm newbie in Laravel and hope this is not a basic question.

Laravel 7 | how can i clone eloquent object and add entry in table with all relationship

I want to clone one product with all its relationship like price, attributes, images etc.
also, price and attribute have another relationship (nested relationships)
is there any easy way I can clone all this with few line of code?
You can use the replicate() function for the model itself. But this wont make a deep copy in terms of creating also the related entries.
https://laravel.com/api/7.x/Illuminate/Database/Eloquent/Model.html#method_replicate
You will have to write your own code where you get the related models and replicate them or you can use a package like: https://github.com/Neurony/laravel-duplicate

Laravel group by with select all column

Is there a way on laravel eloquent to query like this:
select users.* from users group by name
to implement it on code:
Users::select('name')->groupBy('name')->get();
Now I need to get other details (columns) of the user. Is there a way to query without aggregating every column on the model, this will not work:
Users::select('users.*')->groupBy('name')->get();
Try this
Users::groupBy('name')->get();
Since laravel is independent of the type of database you use, so your query should be eloquent.
For more details read this official documentation
this is working;
Users::select('users.*')->groupBy('name')->get();
just make sure your model name. You don't have to write class name, and if model name is User then change to
User::groupBy('name')->get();
Don't forget to import the model at the top according to your folder structure use App\User;

Laravel: Calling a common function for multiple Models from anywhere in laravel?

I am working on a Laravel 5.4 app. In that I have a booking in which we add Tours, Flights, Hotels which are separate section along with their separate Model / Controller / Views with operations of CURD. Tours, Flights, Hotels stores data in separate tables along with price of that item.
I have in total 4 tables Bookings, Tours, Flights, Hotels. Bookings have booking_id which related further to Tours, Flights, Hotels and these all can have multiple entries against a booking. I can have 2 hotels, or 4 tours against a booking_id.
Now, I have a billing section where I need to show all the entries with their no of items and price and a sub total of all items as total booking price.
I need a function which I can call from anywhere in the system and can show the booking total and if needed itemized list too.
Should I create a new Model/Controller for that purpose or a Trait maybe? Not able to think how should I do this in Laravel.
Please check and advise some solution/concept for this and I will try to implement it.
Directory:
/app/Http/Helpers
Add some a Helper class there with namespace: App\Http\Helpers.
Put all your methods in there.
You can now call them from anywhere as long as you import the namespace App\Http\Helpers.
If to don't want to typehint and make laravel resolve these dependencies, you can just have static methods, then you won't need to instantiate anything saving you some time.

Magento save many to many models

I have created a custom module with 3 models (student, room, studentroom).
The studentroom is used to store the relation between student & room (manytomany).
I have maked the form + grid to the students and rooms and i can create both of them. i want now to add a multiselect in the student form to select the rooms (until now its ok). When i save or edit my student how can i save or display also the association in studentroom.
Anyone has an idea ? A module with the same functionalities ?
Thx for advance
Magento's ORM has no built-in methods for the sort of one to many and many to many relationships you're describing above. As such, it's up to each individual developer to implement their own save methods that (before or after calling parent::save()) handle any extra relationships an object might have.

Resources