Change id column name in users table for laravel auth ui - laravel

the dafault code for the id column for the users table is like such:
**$table->id();**
But I changed in to:
$table->bigIncrements('user_id');
Now i got error saying that 'id' column is not found in a file that doesnt run the query for users table at all. I dont know where i'm wrong.
Or is the id column is unchangeable after all since it is package?

$user->id() default checks for primary key column 'id'.
So you can rename your primary key in your model like this :
protected $primaryKey = 'user_id';

id() is a is an alias of the bigIncrements method. so you can write it like
$table->id('user_id');

Please just go to your Model and add below line of code, it'll solve your problem.
it's because laravel by default searches for id and it doesn't find it because you've changed it, if you want to let the Laravel know that your primary key is not id but 'user_id' you should add it in your model that Laravel should get it from there.
protected $primaryKey = 'user_id';
that is it.

Related

Laravel model find other column

Is it possible to overwrite the Laravel model 'find' function?
When I use model::find() it search in for the 'id' column but my table doens't have a id column but a SubscriptionId colum.
I know I can use: model::where('subscriptionId', $id) but my queued jobs are not working right now..
model::where('subscriptionId', $id)->first()
Make sure your subscriptionId is unique.
Let me know if my snippets code not working
Or
model::findOrFail($id, 'subscriptionId')
Find in Laravel searches the table based on the primary key. If you are not using id as your key, you can tell Laravel this within your model.
protected $primaryKey = 'subscriptionId';
This will tell Laravel to search this model based on the subscriptionId primary key instead of id. This should solve it for you. Docs on Primary Keys

Laravel auth in custom fields

I have an existing laravel project, where i create role base authentication like (user & admin) and this table relation with multiple table where relation make with id (primary key). Now i want to change primary key from id to user_id.
protected $primaryKey = "user_id";
It's call in User model. User table's changed id to user_id. But after change this, i have face many problem because of relational database with others table. How can i solve this problem without any change of migration table?

SoftDelete doesn't work when I change primary key in Laravel

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

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

why model name in eloquent are not as same as table name?

When you create model in laravel for eloquent, the User model is treated like a users table. Why is that? Can we use an exact table name for the model?
I think more than anything it's convention. I don't see why you couldn't create a Users model for your users table, but a User is an instance of users, which is why it's done that way. You can always specify the table the model uses with:
protected $table = 'name_of_table';
which can be different than the model name. For example, a Data model can use the table userdata, as long as you specify that.
Hope that helps.

Resources