Alter custom ID column name Laravel 7 - laravel

We are transforming an old project to Laravel 7
the users table doesn't follow laravel naming
the id column is named "UserId" and the problem it is not auto increment
I already set in the User model
protected $primaryKey = 'UserId';
how can I alter the column to be Auto-Increment, keep in mind it is used in foreign constraints, thank you

If the name of the user table is different, you should specify this naming in the model section. Also, why not change the related column type of the migration process to include auto-increment. To set the auto-increment property, in the model part protected $incrementing = true; You can specify.

Related

Implementing Composite Keys in CodeIgniter Models

Is there any way you could implement composite primary keys using Models in CodeIgniter 4?
Like this?
class SomeModel extends Model{
protected $table = 'table';
protected $primaryKey1 = 'primary_composite_id1';
protected $primaryKey2 = 'primary_composite_id2';
protected $primaryKey3 = 'primary_composite_id3';
// numbers in the identifiers were only added for clarity
...
}
I think you can define the table structure using the Forge class. Here is a snippet of how I defined the table in Migrations.
in SomeMigration.php
class SomeClass extends Migration{
/* added fields here */
...
// Set them as foreign keys
$this->$forge->addForeignKey('item_id', 'item', 'item_id', 'CASCADE', 'CASCADE');
$this->$forge->addForeignKey('poster_uid', 'user', 'user_id', 'CASCADE', 'CASCADE');
$this->$forge->addForeignKey('customer_uid', 'user', 'user_id', 'CASCADE', 'CASCADE');
// Set them as primary keys
$this->$forge->addPrimaryKey('item_id');
$this->$forge->addPrimaryKey('poster_uid');
$this->$forge->addPrimaryKey('customer_uid');
...
}
Also, if setting composite primary keys in Models are not possible, should I
Create a new primary key column for that table instead?
Leave out the $primarykey value as empty and use just use queries (e.g. using WHERE)?
Use any one of the columns(set as PK) in the table as the value for $primarykey?
questions were created based on this post Codeigniter MY_Model composite primary key, since the answer did not directly answer the question
I am currently using the framework for our school project. There was none mentioned in the documentation, so I got stuck. Any kind of alternative solution is very much appreciated. Thanks! Cheers!
I have run into the same problem these days.
It seems that Codeigniter doesn't support this functionality for the model's primary key.
I searched in documentation and in Model's source code and I saw that it handles the primary key like one value.
e.g. at models find method I can see this code:
$row = $builder->whereIn($this->table . '.' . $this->primaryKey, $id)
In my case I will add an extra ID column and use this as a primary key. If I find a better alternative in the future I will update here.
Best Regards

Laravel Eloquent relation for getting user name for a specific id

I have a table named project. In this table I have a column named student_id. Model for this table is Project. And I have another table named user. And model name is User. Now I am retrieving all project table details in a page including student_id. Now I want to show user name instead of student_id. I mean I want to show this student name instead of student id. For example if student_id is 10 then I want to print name for that user from user table whose id is 10.
Any one please help.I have read document from laravel. But i don't know why I am not getting the eloquent concept properly.
Define belongsTo() relationship in Project model class:
public function student()
{
return $this->belongsTo(User::class, 'student_id');
}
And use it:
$project->student->name;

get only one row from first table in table left join in laravel

I have two table
1. Bog_post
2. blog_image
Now I want only one image from blog_image table.
I also save post id in blog_image table.
how can i do this query in laravel ?
Create a model as "BlogImage" and edit it.
class BlogImage extends Model
{
protected $table = 'blog_image';
}
Then create a query with this model.
$blog_image = BlogImage::where('post_id',$post_id)->pluck('image')->first();
You can get just "image" column data in this way, as you want.
Also you can set releationship with this two tables. Here is documentation

Laravel assumes the database table is the plural form of the model name

By default, Laravel is assuming that the database table is the plural form of the model name. But what if my table name is "news" and i still want to use this feature? should i change it to "newses" or should i use "new" for the model name?
You may specify a custom table by defining a table property on your model as below
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'my_flights';
}
Ref:https://laravel.com/docs/5.1/eloquent
If you have a model ending with the letter 's', it will keep the table name the same. In your case, your model will name your table news by default.
If you want to use another name though, you can use:
protected $table = 'tablename';
inside of your model.
EDIT: I tested this in my application. I made a model named News. Then I made a new instance of News and retrieved the table name:
$news = new News();
dd($news->getTable());
It returns: news
Inside your eloquent model you have to define table name. For example if my model is named user and table in database is named user_of_application then i do it this way
class user extends Model
{
protected $table = 'user_of_application';
}
Laravel uses a "standard set" rule that defines:
A Model is a single instance of a record
A Collection is one or more records
Therefore it assumes that a table is a collection of records.
The nomenclature has a problem when it clashes with various features / gotchas of human languages. For example, what if I have a Model called Sheep? That does mean my Database Table should be called "Sheeps"?
It's up to the developer to avoid "Gollum/Smeagol" syntax. Indeed, you wouldn't want a table called "Newses" as much I'd like to end up with a table called "Sheeps".
Ultimately, I construct Migrations with:
sudo php artisan make:migration create_sheep_table --create=sheep
As for Models, you'll notice in the documentation that they have a different table name for "Flights" called "my_flights"
https://laravel.com/docs/master/eloquent#defining-models
Again, it's up to the developer / DB manager to make decisions on naming conventions that make sense in an application context.

Laravel 4: Relation executing query using the wrong row

I have a relation I'm trying to get working that seems to be searching by the wrong column. I have a model, userwords, that should be getting an associated word with it. I want it to be using the word_id column from the userword table to search for a word by the id in the word table, but instead it seems to be using the id of the userword row to search for the word. I thought that perhaps that if I told it which column to use in the third parameter of hasOne() it would work, but to no avail. The code in question is:
public function word(){
return $this->hasOne('Word', 'id', 'word_id');
}
any help or ideas would be appreciated! Also if you need more information, please just let me know and i'll update this here! Thanks a lot!
Your parent table is userword and related child table is word, in this case, Userword model should contain following method for making the relation with word table:
class Userwords {
protected $table = 'userword';
public function word(){
return $this->hasOne('Word', 'userword_id'); // use the foreign key here
}
}
In this case, your word table should contain the userword_id as a foreign key. So, if you have a different foreign key defined word table then use that foreign key in the place of userword_id.
Also remember that, tables should use plural name of the word, for example, words should be the table name but you used word and the Model should use a singular name, for example, Word for the words table, so you have a different name convension here so use the protected $table = 'word' in your Word model and in Userwords model use protected $table = 'userword'. So, finally, it should be something like this:
class Userword {
// change the table name in database (use userwords)
protected $table = 'userwords';
public function word(){
return $this->hasOne('Word', 'userword_id'); // use the foreign key here
}
}
For words table, it should be:
class Word {
// change the table name in database (use words)
protected $table = 'words';
public function userwords(){
return $this->belongsTo('Userword');
}
}
Read the manual for more information about Laravel Model Relationships.

Resources