I have a model User in Laravel 6.5.1.
class User extends Authenticatable
{
use Notifiable;
public $table = 'usuario';
public $primaryKey = 'cif_usu';
public $incrementing = false;
public $timestamp = false;
//...
}
However, when I'm trying to select a user, I'm getting the following error:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist
LINE 1: select * from "usuario" where "id" = $1 limit 1 ^ (SQL: select
* from "usuario" where "id" = 24 limit 1)
How can I rename the id column?
Edit:
I changed:
public $primaryKey = 'cif_usu';
to:
protected $primaryKey = 'cif_usu';
and the result is the same
Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention:
/**
* The primary key associated with the table.
*
* #var string
*/
protected $primaryKey = 'key';
Read more about Laravel - Primary Keys
The visibility of the $primaryKey attribute should be protected. I think without traffic that change you aren't actually overriding the primary key in the base Model class.
If that's not the case it may be useful to see the code that triggers this query
Put primarykey and table as protected.
protected $table = 'usuario';
protected $primaryKey = 'cif_usu';
public $incrementing = false; //only if your primary key is an assignable field,not auto incremented
You can use model as
DB::table('usuario')->where('cif_usu',$cif_usu)->first();
OR
DB::table('usuario')->find($cif_usu);
Related
OrderProducts Eloquent:
class OrderProduct extends Model
{
use HasFactory;
protected $table = 'order_products';
}
I create a model for my table name called order_products.
My Eloquent name is OrderProduct. Then I change the table name in Eloquent to protected $table = "order_products"`.
Still, I'm getting order_product table doesn't exist issue?
Why?
Changing the Model would not automatically change the table name within your DB
You can specify a custom table name by defining a table property on your model:
class OrderProduct extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'order_products';
}
Ref: https://laravel.com/docs/8.x/eloquent#table-names
i am using the resource in laravel when I get the data its ask me for the id but the primary key in my table is user_menu_id how to change the using resource route
route
Route::resource('/UserMenuController', 'UserMenuController');
error
Unknown column 'user_menus.id' in 'where clause' (SQL: select * from
user_menus where user_menus.id = 1 limit 1)
model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class user_menu extends Model
{
protected $fillable = ['menu_parent_id', 'menu_title', 'menu_class',
'menu_id', 'menu_link', 'created_by', 'updated_by', 'created_at', 'updated_at'];
}
According to this article, you need to override $primaryKey property:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class user_menu extends Model
{
protected $fillable = [
'menu_parent_id', 'menu_title', 'menu_class',
'menu_id', 'menu_link', 'created_by', 'updated_by', 'created_at', 'updated_at'
];
protected $primaryKey = 'user_menu_id';
}
The error says there is not a table with named user_menus. Set the real table name $table property in the model:
protected $table = 'user_menu';
Or you may follow Laravel naming conventions as described in my repo and just rename the table to user_menus (plural for user_menu class).
You said the primary key is not standard too, so change it too to $primaryKey:
protected $primaryKey = 'user_menu_id';
Define the primary key in your model class and you will be golden.
The reason you're encountering an error is due Laravel always assuming the primary key is id. Reading the error log, you will notice:
1054 Unknown column 'user_menus.id' - you can right away know it's trying to find a non-existent column.
The fix will is to assign $primaryKey to your model class, e.g:
protected $primaryKey = 'user_menu_id';
Eloquent will assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention.
class user_menu extends Model
{
protected $primaryKey = 'user_menu_id';
}
I have made two tables Inventories and Inventory_images. Primary key of Inventory table is the foreign key of inventory_images table now i am trying to fetch all the images of same inventory but getting error.
Here's my code
Inventory Model:
/**
* The table name that should be hidden from other modules
*/
protected $table = 'inventories';
protected $PrimaryKey = 'id';
public function test(){
return $this->belongsTo('App\InventoryImage', 'i_id');
}
InventoryImage Model:
protected $table = 'inventory_images';
protected $PrimaryKey = 'id';
public function inv_det(){
return $this->belongsTo('App\Inventory', 'id');
}
Controller:
$inventory = Inventory::with('test')->orderBy('id', 'DESC')->paginate('10');
dd($inventory);
Can some one please help me find out the issue
You are making some mistakes in your code which you should resolve first (and which might help you solve your problem).
First, the variable name to overwrite the primary key should be $primaryKey and not $PrimaryKey (variable names normally always start with a small letter.
This should not have any influence though, since Laravel assumes the primary key field to be named id anyway.
More importantly, you are in both cases using the belongsTo method, although in one case it should be hasMany. In a 1-n relation the parent model should return the hasMany relationship, and the child model (which holds the column with the foreign key) the belongsTo.
Furthermore, the second argument of the hasMany or belongsTo method is the foreign key column name, in case it is different of the snake case representation of the model (appended by _id). So IF your inventory_images table has a differently named foreign key column other than inventory_id, you need to pass along the second argument with the correct name. I assume that your foreign key name is i_id, so you need to pass it to both functions.
https://laravel.com/docs/5.4/eloquent-relationships#one-to-many
Please check if this works:
/**
* The table name that should be hidden from other modules
*/
protected $table = 'inventories';
protected $primaryKey = 'id';
public function test(){
return $this->hasMany('App\InventoryImage', 'i_id');
}
And the child table:
protected $table = 'inventory_images';
protected $primaryKey = 'id';
public function inv_det(){
return $this->belongsTo('App\Inventory', 'i_id');
}
I have a database running with username, password and user_id as primary key
and I'm trying to get the highest number out of the user_id column using eloquent.
This is my current error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from users where users.id = 2 limit 1)
This is what my model looks like:
users.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
use Eloquent;
class users extends Eloquent
{
public static function register()
{
$user_id = users::find(DB::table('users')->max('user_id'));
return $user_id;
}
}
Usually, Laravel considers the primary key name is id. You must declare your primary key name if it's not id. So, add this in your user class:
protected $primaryKey = 'user_id';
Here is how the whole class should look:
class users extends Eloquent
{
protected $primaryKey = 'user_id';
public static function register()
{
$user_id = users::find(DB::table('users')->max('user_id'));
return $user_id;
}
}
Get the highest user like this
$user = users::orderBy('user_id', 'desc')->first();
I have two models User and UserType declared as follows:
class User extends Model {
protected $table = 'user';
protected $primaryKey = 'user_id';
public function company() {
return $this->hasOne('App\Models\Company', 'company_id', 'company_id');
}
public function userType() {
return $this->hasOne('App\Models\UserType', 'user_type_id', 'user_type_id');
}
}
class UserType extends Model {
protected $table = 'user_type';
protected $primaryKey = 'user_type_id';
}
Now, I query the relationships using:
User::with('userType', 'company')->all()
Strangely, I get the company but userType is always null.
The MySQL query log shows that Laravel was able to get the user_type record.
The only difference between company and userType relationships are the data type of the primary keys. company.company_id is numeric while user_type.user_type_id is a string.
I think it is related to the data type of the keys however, I have a similar setup on Laravel 5.1 and it runs perfectly.
Laravel supports non-numeric primary keys but you need to set:
public $incrementing = false;
on your model class.
I corrected the issue by changing UserType definition to:
class UserType extends Model {
protected $table = 'user_type';
protected $primaryKey = 'user_type_id';
public $incrementing = false;
}
The first issue i notice with your relationship is that the first user_type_id you have passed to the hasOne function is wrong because you have the user_type_id as the primary key of the user_type table. The second argument of the hasone must be the foreign key of the parent table which is the user. So if you have anything like user_id in the user_type table use that instead.
But if thats not the case and user rather belongs to UserType then you have to change the hasOne to belongsTo.