Laravel model find other column - laravel

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

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

Change id column name in users table for laravel auth ui

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.

Laravel's uuid type

I was reading Laravel's documentation that I noticed Laravel 7 offers a uuid type in the page for database migrations
Unfortunately I couldn't find much information about using it in the documentation. How is such a column filled when I insert a new record? Does Laravel fill it on its own? Does Laravel guarantee it to be unique or should I worry about collisions? Can I use this field to create short links for the posts of a blog, for example?
Laravel should fill this on its own by setting the keyType(untested) on your model:
protected $keyType = 'string';
The key, whether you fill it manually or not, is unique as laravel depends on Ramsey.
To manually acquire a uuid you may use the provided helper:
use Illuminate\Support\Str;
return (string) Str::uuid();

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

Laravel5: How are Eloquent model relationships expressed in the database?

There's a missing link I fail to understand.
I use migrations to create database tables and I define the relationships there. meaning.. if I have a person table and a job table and I need a one to many relationship between the person and jobs, I'd have the job table contain a "person_id".
When I seed data or add it in my app, I do all the work of adding the records setting the *_id = values etc.
but somehow I feel Laravel has a better way of doing this.
if I define that one to many relationship with the oneToMany Laravel Eloquent suports:
in my Person model.....
public function jobs()
{
return $this->hasMany('Jobs);
}
what's done on the database level? how do I create the migration for such table? Is Laravel automagically doing the "expected" thing here? like looking for a Jobs table, and having a "person_id" there?
Yep, Laravel is doing what you guess in your last paragraph.
From the Laravel documentation for Eloquent Relationships (with the relevant paragraph in bold):
For example, a User model might have one Phone. We can define this
relation in Eloquent:
class User extends Model {
public function phone()
{
return $this->hasOne('App\Phone');
}
}
The first argument passed to the hasOne method is the name of the
related model. Once the relationship is defined, we may retrieve it
using Eloquent's dynamic properties:
$phone = User::find(1)->phone;
The SQL performed by this statement
will be as follows:
select * from users where id = 1
select * from phones where user_id = 1
Take note that Eloquent assumes the foreign key of the relationship based on the model name. In this case, Phone model is assumed to use a user_id foreign key.
Also note that you don't actually have to explicitly set the foreign key indexes in your database (just having those "foreign key" columns with the same data type as the parent key columns is enough for Laravel to accept the relationship), although you should probably have those indexes for the sake of database integrity.
There is indeed support to create foreign key relationships inside migration blueprints and it's very simple too.
Here is a simple example migration where we define a jobs table that has a user_id column that references the id column on users table.
Schema::create('jobs', function($table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
You can also use some other methods that laravel provides such as onDelete() or onUpdate
Of course to understand better the options that are available to you please read the documentation here.
Edit:
Keep in mind that Eloquent is just using fluent SQL wrapper and behind the scenes there are just raw sql queries, nothing magical is happening, fluent just makes your life a lot easier and helpers you write maintainable code.
Take a look here about the Query Builder and how it works and also, as #Martin Charchar stated , here about Eloquent and relationships.

Resources