I want the "Unit" field that saved to my model is not the default field (id), but instead customized field (e.g.: I want to save the Unit field with name from the foreign table, not the id)
How to do that? currently why CrudController is like this
If there’s a 1-to-1 relationship (hasOne) defined in your model, you should be able to do that by giving the field the name “unit.name”.
Related
Is it possible to change the primary key in laravel backpack?
The model has 2 fields - "id" and "name". I can create an entry with any id and name, everything is created perfectly. If I change the field "name" - no problem. However, if i change the id, a 404 error "No query results for model [Client] new_id" falls and the field in the database does not change. This raises the question, is it possible to change the primary key using the Laravel backpack?
Model - User
id [999]
name [foo]
Model - Post (without User Foreign Key)
id [1]
unique_key [USR_00000999]
data [bar]
I would like to get all user with related posts (one to many) by using "custom key" value, is this possible with build in eloquent?
I only manage to looping using foreach one by one with
Post::query()
->where('unique_key', sprintf('USR_%08d', $user_id))
->count();
That is not built-into Laravel by default... if you want to know why it's because it's not a common thing that everyone does...
BUT, you can use scope query so you don't need to do the sprintf everytime...
Laravel Query Scopes Documentation
But I want to ask, why wouldn't you just add user_id on your post table and just have an Accessors on your post model for generating the unique_key? That would be much easier on my perspective...
Laravel Accessor Documentation
UPDATE: (See Comment)
Is it possible to have an empty user_id on Posts table?
then populate it when the user is created?
say you have a posts with key of USR_00000999... and you have a user with an id of 999... when you create that user you'll just have to update all posts with key of USR_00000999 to have a user_id of 999...
I'm implementing a 'select2_from_ajax' field using Backpack for Laravel.
I've implemented this in other places and it works correctly. But for some reason when implementing it this time it will not let me select any of the options and doesn't show the highlight when mousing over the options. It lists out the options correctly, I just can't select any of them.
The only thing I can think of is that the relationship it's trying to reference doesn't have a primary 'id' field in the database, but I'm not sure why that would affect this.
I have implemented both the index and show routes.
The issue was that the relationship field's primary key was not 'id' it was setup with a different column name.
I reworked the data structure so the foreign key referenced was pointing to a column labeled 'id'.
I have changed the primary key in a table from id to pr_id and mobile.
$table->primary(['pr_id' , 'mobile']);
also I added SoftDelete Trait in model. but when I want to delete a record it doesn't work.
I believe this is because of not mentioning primary key into model, and your model still consider primary key is id however you have changed it. So you just to add following script into relevant model,
class YourModelClass extends Model
{
protected $primaryKey = 'pr_id';
}
This in way model won't consider primary key as id.
You need to override few methods also like getKeyForSaveQuery, setKeysForSaveQuery with defining the primary key in model. For soft delete you need to override one more method runSoftDelete.
Reference links
For Save Query Method
For Soft Delete Method
I have many custom entities for that I need to change primary field, I already know creating workflow that will update values of primary field by coping it from different field. But I don't want to do that.
Is there any way to change primary field of custom entity after it has been created?
The primary field of an Entity (as well other custom fields) cannot be changed after the creation. If you must change the primary field name, you need to delete and recreate the Entity.
You said change, what kind of change you need. Pick your answer from below.
On Create of new entity: changes in primary field (new_name)
Display name change - possible.
Data type change - impossible.
Schema name change - possible.
Requirement level change - possible.
On Update of entity: changes in primary field (new_lookupdisplaytext)
Display name change - possible.
Data type change - impossible.
Schema name change - impossible.
Requirement level change - impossible.