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';
}
Related
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);
The problem is that the query can't find the specific_method(specific_method, specific_model,SpecificModel,specificMethod etc...), that should been joined with the method WITH from Laravel Eloquent. Any ideas how to solve it?
My code:
//SpecificModel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SpecificModel extends Model {
protected $guard_name = 'web';
protected $table = 'SpecificTable';
protected $guarded = ['id'];
public function specificMethod(){
return $this->belongsTo('App\Models\AnotherModel','AnotherModel_id');
}
}
//AnotherModel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AnotherModel extends Model {
protected $guard_name = 'web';
protected $table = 'AnotherTable';
protected $guarded = ['id'];
}
//Query method
$model = app('App\Models\SpecificModel');
$query = $model::with('specificMethod:id,title');
$query = $query->orderBy('specific_method.title','desc');
return $query->get();
//Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'"specific_method.title"' in 'order clause' (SQL: select * from
`SpecificModel` where `SpecificModel`.`deleted_at` is null order by
`specific_method`.`title` desc)
This happens because the belongsTo relationship does not execute a join query as you expect it to (as you can see from the error you get). It executes another query to get the related model(s). As such you will not be able to order the original model by related models columns.
Basically, 2 queries happen:
Fetch the original model with SELECT * from originalModel ...*
Fetch the related models with SELECT * from relatedModel where in id (originalModelForeignKeys)
Then Laravel does some magic and attaches the models from the 2nd query to the correct models from the first query.
You will need to perform an actual join to be able to order the way you want it to.
I try update model in database.
I have request with naw values and value.
'old_log'- old value to find row in database.
public function UserChange2(Request $request){
dump($request);
$data=$request->all();
$log = $request->input('old_log');
dump($log);
$user=userModel::select(['fam','im','ot','phone','log','pass'])->where('log',$log)->first();
$temp=$request->input('fam');
$user->fam=$temp;
$temp=$request->input('im');
$user->im=$temp;
$temp=$request->input('ot');
$user->ot=$temp;
$temp=$request->input('phone');
$user->phone=$temp;
$temp=$request->input('log2');
$user->log=$temp;
$temp=$request->input('pass');
$user->pass=$temp;
dump($user);
$user->save;
}
But the records in the table are not updated.
Model:
class userModel extends Model
{
public $timestamps = false;
protected $fillable=['fam','im','ot','phone','log','pass','reg','token','date','position'];
}
Now:
dump($request);
$data=$request->all();
$log = $request->input('old_log');
dump($log);
$user=userModel::select(['fam','im','ot','phone','log','pass'])->where('log',$log)->first();
$user->fill($request->all())->save();
$user->save;
And error:
Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update user_models set fam = y, phone = gggg, pass = tttyyyyyyyy where id is null)
Your userModel extends Model, which is an Eloquent Model and by default expects that the related table has a primary key called id.
The error
Column not found: 1054 Unknown column 'id' in 'where clause'
indicates that the column id does not exist.
Check your table definition. Is this a table in a DB that was created outside your Laravel project?
If not, check your database migration whether the primary key id is defined. Read here about database migrations.
If there is already a primary key that you want to use instead of id, you can tell Eloquent:
protected $primaryKey = 'your_primary_key';
See here.
You likely don't have fillables completed in your Model.
Go to the model which should be blank and add the fields that can be completed:
example:
protected $fillable = [
'phone', 'log2', 'pass',
];
You can find this in the Laravel manual here:
https://laravel.com/docs/5.5/eloquent#mass-assignment
remove select from your eloquent. try this..
$user=userModel::where('log',$log)->first();
$user::create($request->all());
edit
$user=userModel::where('log',$log)->first();
$request->request->add(['log' => $request->log2]);
$user::create($request->all());
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.