I am having such a terrible time trying to achieve a certain thing – I have a model A that needs to access model Profile’s column name for search functionality, but I have been getting all of A instead of “no search returned” or the specific search list. I am new to PHP.
Here’s my code.
// Model A (excerpt) (Models.php)
class A extends Model {
public function user()
{
return $this->belongsTo(User::class);
}
public function profile() {
return $this->hasOneThrough(Profile::class, User::class, 'id', 'user_id', 'user_id');
}
}
Model Profile
class Profile extends Model
{
use HasFactory, UuidTrait;
public $guarded = ['null'];
public function user()
{
return $this->belongsTo(User::class);
}
}
// my error is coming from below
UserController.php
....
if($search != "")
{
$users->orWhereHas('A', function($query) use($search) {
$query->where('A.first_name', 'LIKE', "${search}%"
}
}
Model A columns: id, user_id, address, size
Model Profile columns: id, first_name, gender, user_id
Hence, the relationship of A to Profile is through user_id (say, A=>user_id=>profile.first_name)
Expected Result:
search ‘Fred’ (present in Profile)
return A fields with ‘Fred’ present
Actual Result:
no column A.first_name