I had my model name different from table name - "CompanyUser" against "company_users". I was unable to index this particular Model. I am constantly getting the below message whenever I run the 'php artisan scout:import "App\Models\CompanyUser"'
$ php artisan scout:import 'App\Models\CompanyUser'
BadMethodCallException
Call to undefined method Illuminate\Database\Eloquent\Builder::searchable()
at C:\xampp\htdocs\geovetra\geovetra-laravel-api\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:71
67▕ * @throws \BadMethodCallException
68▕ */
69▕ protected static function throwBadMethodCallException($method)
70▕ {
➜ 71▕ throw new BadMethodCallException(sprintf(
72▕ 'Call to undefined method %s::%s()', static::class, $method
73▕ ));
74▕ }
75▕ }
i Bad Method Call: Did you mean Illuminate\Database\Eloquent\Builder::each() ?
1 C:\xampp\htdocs\geovetra\geovetra-laravel-api\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:36
Illuminate\Database\Eloquent\Builder::throwBadMethodCallException("searchable")
2 C:\xampp\htdocs\geovetra\geovetra-laravel-api\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:1864
Illuminate\Database\Eloquent\Builder::forwardCallTo(Object(Illuminate\Database\Query\Builder), "searchable")
I even changed the table name to "company_user" instead of "company_users". Still no change in error.
I have a different Model named "CompanyUserInvitation" and its table "company_user_invitations" , Scout indexed this Model without any problem. Thats why I'm confused why this model is not able to index. All google searches take me nowhere also even ChatGPT is unable to address my problem. Please help!
Below is my Model "CompanyUser"
<?php
namespace App\Models;
use App\Http\Traits\OfCompany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Builder;
use Laravel\Scout\Searchable;
use Illuminate\Support\Facades\URL;
enum CompanyUserRole: string
{
case ADMIN = 'admin';
case MANAGER = 'manager';
case OPERATOR = 'operator';
case MANAGEMENT = 'management';
}
class CompanyUser extends Model
{
use HasFactory;
use CrudTrait;
use Searchable;
function __construct()
{
// static::addGlobalScope('ofUser', function (Builder $builder) {
// if (auth()->user()->id) {
// $builder->where('user_id', auth()->user()->id);
// }
// });
}
public function getTable()
{
return 'company_users';
}
protected $fillable = [
'company_id',
'user_id',
'role',
'invited_by_user_id',
'invitation_accepted_at',
'invitation_rejected_at',
];
protected $casts = [
'id' => 'integer',
// 'invitation_accepted_at' => 'timestamp',
// 'invitation_rejected_at' => 'timestamp',
];
function user()
{
return $this->belongsTo(User::class);
}
function invited_by_user()
{
return $this->belongsTo(User::class);
}
function company()
{
return $this->belongsTo(Company::class);
}
function scopeOfUser($builder, $id = null)
{
return $builder->where('user_id', $id ?? auth()->user()->id);
}
function scopeOfCompany($builder, $id = null)
{
return $builder->where('company_id', $id ?? auth()->user()->company?->id);
}
function getAdminsAttribute()
{
return $this->where('role', CompanyUserRole::ADMIN);
}
function getOperatorsAttribute()
{
return $this->where('role', CompanyUserRole::OPERATOR);
}
function getManagementsAttribute()
{
return $this->where('role', CompanyUserRole::MANAGEMENT);
}
function getManagersAttribute()
{
return $this->where('role', CompanyUserRole::MANAGER);
}
public function searchableAs()
{
return 'company_users';
}
public function getAcceptTokenAttribute()
{
$token = URL::signedRoute('invitationAction', [
'id' => $this->id,
'action' => 'accept'
], null, false);
return (env('APP_HOST_DIGITAL') . $token);
}
public function getRejectTokenAttribute()
{
$token = URL::signedRoute('invitationAction', [
'id' => $this->id,
'action' => 'reject'
], null, false);
return (env('APP_HOST_DIGITAL') . $token);
}
public function toSearchableArray()
{
return [
'company_id' => $this->company_id,
'user_id' => $this->user_id,
'role' => $this->role,
'invited_by_user_id' => $this->invited_by_user_id,
'invitation_accepted_at' => $this->invitation_accepted_at,
'invitation_rejected_at' => $this->invitation_rejected_at,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
?>
Please note: I'm not the original developer of this code, this project has been handed over to me, after my senior left my institution. I'm still like a rookie in Laravel.