Laravel Scout Wrong ID mapping. I'm using scout w...
# community-help
r
Laravel Scout Wrong ID mapping. I'm using scout with typesense and having trouble with the return of data. I have a non-standard primary key which is identified on the model as id_student. When I do a raw return I get and id field which contain the id in typsense and the id_student field which has the correct id and record. When I use a get, it returns the typesense id as the id_student and returns the incorrect records.
Copy code
class ResStudent extends Model
{
    use Notifiable;
    use Searchable;

    protected $guarded = [];

    protected $table = 'tbl_student';

    protected $primaryKey = 'id_student';

    protected function casts(): array
    {
        return [
            'created_at' => 'immutable_datetime',
            'updated_at' => 'immutable_datetime',
            'std_create_date' => 'immutable_datetime',
            'std_modify_date' => 'immutable_datetime',
        ];
    }

  
    public function searchableAs(): string
    {
        return 'id_student';
    }

    public function toSearchableArray(): array
    {
         return array_merge($this->toArray(), [

            'id_student' => (string) $this->id_student,
            'std_group_id' => $this->std_group_id,
            'std_groupleader' => (string) $this->groupleader,
            'std_phone' => (string) $this->std_phone,
            'std_email' => (string) $this->std_email,
            'std_last_name' => (string) $this->std_last_name,
            'std_first_name' => (string) $this->std_first_name,
        ]);
    }
}
Copy code
'model-settings' => [
            \App\Models\ResStudent::class => [
                'collection-schema' => [
                    'fields' => [
                        [
                            'name' => 'id_student',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'std_last_name',
                            'type' => 'string',
                            'sort' => true
                        ],
                        [
                            'name' => 'std_first_name',
                            'type' => 'string',
                            'sort' => true
                        ],
                        [
                            'name' => 'std_email',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'std_group_id',
                            'type' => 'string',
                            'sort' => true
                        ],
                    ],
                    'default_sort_field' => 'std_first_name',
                ],
                'search-parameters' => [
                    'query_by' => 'std_last_name, std_first_name, std_email, std_group_id',
                ],
            ],
        ],
    ],
Copy code
'model-settings' => [
            \App\Models\ResStudent::class => [
                'collection-schema' => [
                    'fields' => [
                        [
                            'name' => 'id_student',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'std_last_name',
                            'type' => 'string',
                            'sort' => true
                        ],
                        [
                            'name' => 'std_first_name',
                            'type' => 'string',
                            'sort' => true
                        ],
                        [
                            'name' => 'std_email',
                            'type' => 'string',
                        ],
                        [
                            'name' => 'std_group_id',
                            'type' => 'string',
                            'sort' => true
                        ],
                    ],
                    'default_sort_field' => 'std_first_name',
                ],
                'search-parameters' => [
                    'query_by' => 'std_last_name, std_first_name, std_email, std_group_id',
                ],
            ],
        ],
    ],
Copy code
ResStudent::search("sullivan")->raw();

[
    "facet_counts" => [],
    "found" => 2,
    "hits" => [
      [
        "document" => [
          "canceled" => 0,
          "created_at" => "2007-07-20T04:15:06.000000Z",
          "id" => "469",
          "id_student" => "517",
          "ip_address" => null,
          "ns_fee" => null,
          "std_address" => null,
          "std_city" => null,
          "std_class_id" => 383,
          "std_create_date" => "2007-07-20T04:15:06.000000Z",
          "std_deposit" => null,
          "std_dob" => null,
          "std_email" => "mpsully55@xxxxxcom",
          "std_first_name" => "Mike",
          "std_group_id" => "19907201154",
          "std_groupleader" => "",
          "std_isleader" => 0,
          "std_jump_type" => null,
          "std_last_name" => "Sullivan",
          "std_message" => "",
          "std_modify_date" => null,
          "std_noshow" => 0,
          "std_pcode" => "",
          "std_phone" => "",
          "std_prospect_id" => null,
          "std_sid" => "",
          "std_state" => null,
          "std_studentid" => null,
          "std_user" => null,
          "std_video" => "Video",
          "std_weight" => null,
          "std_zip" => null,
          "updated_at" => null,
          "waiver1" => null,
          "waiver2" => null,
          "waiver_checked" => null,
        ],
Copy code
App\Models\ResStudent {#1721
        id_student: 469,
        std_prospect_id: null,
        std_last_name: "Desai",
        std_first_name: "Anand",
        std_address: null,
        std_city: null,
        std_state: null,
        std_zip: null,
        std_email: "hiren_desai@xxxxxxcom",
        std_phone: "",
        std_dob: null,
        std_weight: null,
        std_video: "Video",
        std_class_id: 108,
        std_pcode: "",
        std_group_id: "19207165239",
        std_isleader: 0,
        std_studentid: null,
        std_groupleader: null,
        std_message: "",
        std_sid: "",
        std_create_date: "2007-07-12 20:58:46",
        std_modify_date: null,
        std_user: null,
        canceled: 0,
        ns_fee: null,
        std_deposit: null,
        std_noshow: 0,
        std_jump_type: null,
        waiver1: null,
        waiver2: null,
        waiver_checked: null,
        ip_address: null,
        created_at: "2007-07-12 20:58:46.
f
I'd suggest trying to have the same id for the document you do in your database, to keep a 1-1 mapping of documents between Typesense and Laravel