Can I change Eloquent model primary key.
I want to set primary key for example admin_id instead of 'id'?
I know I can change table name for model like
protected $table = "admin";
Is there something similar for primary key?
Yes
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
If you are wanting to use a composite key (a string)
You need to make sure you set public $incrementing = false otherwise laravel will cast the field to an Integer, giving 0
class User extends Model {
protected $primaryKey = 'my_string_key';
public $incrementing = false;
}
class User extends Eloquent {
protected $primarykey = 'admin_id';
}
but
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
note the letter K (capital) on the variable $primaryKey
The primary key variable is case sensitive and must be $primaryKey to work.
Example:
protected $primaryKey = 'your_primary_key_id';
Example within a Model class:
class User extends Eloquent {
protected $primaryKey = 'your_primary_key_id';
}
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
As per Laravel documentation :
Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.
To assign a primary key you should:
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
Related
Can I change Eloquent model primary key.
I want to set primary key for example admin_id instead of 'id'?
I know I can change table name for model like
protected $table = "admin";
Is there something similar for primary key?
Yes
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
If you are wanting to use a composite key (a string)
You need to make sure you set public $incrementing = false otherwise laravel will cast the field to an Integer, giving 0
class User extends Model {
protected $primaryKey = 'my_string_key';
public $incrementing = false;
}
class User extends Eloquent {
protected $primarykey = 'admin_id';
}
but
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
note the letter K (capital) on the variable $primaryKey
The primary key variable is case sensitive and must be $primaryKey to work.
Example:
protected $primaryKey = 'your_primary_key_id';
Example within a Model class:
class User extends Eloquent {
protected $primaryKey = 'your_primary_key_id';
}
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
As per Laravel documentation :
Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.
To assign a primary key you should:
class User extends Eloquent {
protected $primaryKey = 'admin_id';
}
The illegal offset type shows this error when
protected $primaryKey = ['user_id'];
public $incrementing = false;
The primaryKey takes a string not array all you need is to change it to string
protected $primaryKey = 'user_id';
I need some help to delete a movie from my database.
I know that since I have pivot tables I will get some troubles with foreign key(my delete controller gives this error), but I don't know how to solve it.
The error i'm getting is the following:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (sakila.film_category, CONSTRAINT fk_film_category_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON UPDATE CASCADE) (SQL: delete from film where film_id = 999)
My database tables
Here are my models:
model Film:
class film extends Model{
protected $table = "film";
protected $primaryKey = 'film_id';
public $timestamps = false;
use HasFactory;
protected $sql=['film_id', 'title', 'length'];
public function category(){
return $this->belongsToMany(category::class,'film_category', 'film_id', 'category_id');
}
public function actor(){
return $this->belongsToMany(actor::class,'film_actor', 'film_id', 'actor_id');
}
}
model Category:
class category extends Model{
protected $table = "category";
protected $primaryKey = 'category_id';
public $timestamps = false;
use HasFactory;
protected $sql=['category_id','name'];
public function film(){
return $this->belongsToMany(film::class,'film_category', 'category_id', 'film_id');
}
}
model Actor:
class actor extends Model{
protected $primaryKey = 'actor_id';
public $timestamps = false;
use HasFactory;
protected $sql=['actor_id', 'first_name', 'last_name'];
public function film(){
return $this->belongsToMany(film::class,'film_actor', 'actor_id', 'film_id');
}
}
Now I need some help to make my controller function to delete records.
public function remove($film_id){
$film=film::findOrFail($film_id);
$film->delete();
return redirect('/film/view');
Thanks
To delete a movie, you must first delete the records from the Film_category and Film_actor table before you can delete your record from the Film table.
Instead do something like :
(assuming you have created your models from the Film_category and Film_actor tables)
public function remove($film_id){
$film_category = FilmCategory::where('film_id', $film_id)->delete();
$film_actor = FilmActor::where('film_id', $film_id)->delete();
$film = film::findOrFail($film_id);
$film->delete();
return redirect('/film/view');
}
I have two models:
TRUCK
DRIVER
TRUCK has two fields which are FKs. Driver (FK) and Driver2 (FK).
When I try to get the truck with driver and driver2, I get two same records.
$truck = $this->instance->truck()->where('id', $id)
->with(['driver', 'driver2',])
->firstOrFail();
My Truck Model:
class Truck extends Model
{
use SoftDeletes;
protected $table = 'trucks';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function driver()
{
return $this->hasOne('App\Models\Driver');
}
public function driver2()
{
return $this->hasOne('App\Models\Driver');
}
My Driver Model:
class Driver extends Model
{
use SoftDeletes;
protected $table = 'drivers';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function truck()
{
return $this->belongsTo('App\Models\Truck');
}
I am still new to laravel and something I get stuck. Should I create another model instead maybe?
By default laravel will use default foreign key,
Eloquent assumes the foreign key of the relationship based on the
model name #Further reading
So both relation are pointing to the same FK, So you need to specify the foreign key as below
return $this->hasOne('App\Models\Driver', 'Driver');
return $this->hasOne('App\Models\Driver', 'Driver2');
Full code
class Truck extends Model
{
use SoftDeletes;
protected $table = 'trucks';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function driver()
{
return $this->hasOne('App\Models\Driver', 'Driver');
}
public function driver2()
{
return $this->hasOne('App\Models\Driver', 'Driver2');
}
I'm attempting to seek a table to see if a column named "name" exists if so return the value and if not create that row with a null value i saw firstOrCreate but i cannot figure out how to use it for the life of me.
This is what i currently have, can someone lend a hand?
class Settings extends Eloquent
{
protected $table = 'settings';
protected $primaryKey = 'name';
public static function get($settingName)
{
return self::firstOrCreate(array('name' => $settingName));
// return self::select(array('value'))->where('name', '=', $settingName)->first()->value;
}
}
The create() method does mass assignment and this is a big security issue, so Laravel has a protection against it. Internally it has guarded = ['*'], so all your columns will be protected against mass assignment. You have some options:
Set the fillable columns of your model:
class User extends Eloquent {
protected $fillable = array('first_name', 'last_name', 'email');
}
Or set only the ones you want to keep guarded:
class User extends Eloquent {
protected $guarded = array('password');
}
You may, at your own risk also do:
class User extends Eloquent {
protected $guarded = array();
}
And everything will be unguarded.
Take a look a the docs: http://laravel.com/docs/eloquent#mass-assignment
You also could use your Facade to call it:
class Settings extends Eloquent
{
protected $table = 'settings';
protected $primaryKey = 'name';
public static function get($settingName)
{
return Settings::firstOrCreate(array('name' => $settingName));
}
}