I have 2 tables :-
-
users – id,name,email, mobile
-
user_info – id,user_id, store_name, startup_date
-
User Model
class EloquentUser extends Model
{
protected $table = ‘users’;/** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'mobile' ];
}
-
User Info Model
class UserInfo extends Model
{
use HasFactory, SoftDeletes;
public $table = ‘user_info’;}
Below is relationship on above 2 tables :-
public function info() {
return $this->hasOne(UserInfo::class,'user_id','id');
}
I want to order on base of startup_date
but it is giving error column not found. Below is the query :-
$reponse = EloquentUser::with('info')->has('info')->orderBy('info.startup_date')->get();
2
Answers
This error is due to the fact that the column
startup_date
is not present in theuser_info
table.You need to ensure that the column name matches the actual column in the table.
If the column name is different or has been changed, you should update your code accordingly. Assuming the correct column name is
start_date
in theuser_info
table, you can modify your query as follows:Update the column name to actual column in your
user_info
table.You must modify your query and define the inverse relationship in the UserInfo model. The ORM you provided will not give the expected result.
Create inverse relationship
Change the query to