laravel backpack сrud for change primary key? - laravel

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?

Related

Laravel get relation by custom value

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...

Laravel Backpack Create/Update action that customize the foreign field

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”.

Backpack Laravel select2_from_ajax field returns results, but I can't select any

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'.

laravel 5.3 change SQL query of Authentication

i am using laravel5.3 and authentication. i want to change email field as non unique field means same email registered multiple time .Register is successful but on login its take only first credentials true . how i remove first from query in laravel Auth .
Every table in a database should have at least one unique column (primary key/unique key). The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
If same email is inserted multiple times then you are making your database inconsistent.
You can assign different ACL roles(Access Control List) to a user either(admin,member or both).

Updating a model with a unique column in laravel 5

I've tried following this question, which seems like what I want, but have had no luck.
I have a Page model which has an incrementing id, a title, a slug (unique), and an html column. I want to be able to change the title, slug, and html fields. I've tried adding the rule:
'slug' => 'required|unique:pages,slug,{$this->slug}',
for my PATCH request, but when I submit my form (with the same slug), I get an error that it's already taken. If I change the slug, the title and html changes, but the slug does not.
Ideally I'd rather have the slug be generated automatically and update in the background, but I've not been able to get that to work out either.
How can I allow for a changing unique field when updating a model, or generate it automatically?
You need to provide the unique rule with three arguments to add an exception.
The Name of the Table that contains the exception (pages)
The Column Name of the exception (slug)
Primary Key of the row that contains the exception. (id)
In your case the primary key is the id field not the slug field.
'slug' => 'required|unique:pages,slug,' . $this->id

Resources