hasen39's avatar

laravel scout returns empty

my model

<?php

namespace App\Organization;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\ExamTaker\Answer;

class ExamTaker extends Authenticatable
{   use Searchable;
    protected $guards = "exam_taker";

    protected $table="exam_taker";
    protected $primaryKey="username";
    protected $keyType="string";
    public $incrementing=false;

    protected $hidden=[
      'password',
      'username',
      'exam_code',
  
    ];
  
    public function Answer(){
       return $this->hasMany(Answer::class,'username','username')->whereColumn('answer','correct');
    }

    public function getScoutKey()
    {
        return $this->username;
    }
  


    
}


and my controller

<?php

namespace App\Http\Controllers\Organization;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Organization\ExamTaker;

class SerachingController extends Controller
{
    public function search_exam_taker(Request $request){
       $request->validate([
        'search' => 'string|required',
       ]);

       

       $error = ['errors'=>'result not found'];
        // dd($request->get('search'));
       $result = ExamTaker::search($request->get('search'))->get();
        return $result;
       if ($result->count()>0) {
          return $result;
       }

       return $error;
    }
}



i asked it before but no one solve please any one

0 likes
10 replies
JohnBraun's avatar

Could you show the relationship to Exam() on your user model, which you are referring to?

$old_exams = User::find($email)->Exam()->where('progress',0)->paginate(6);

Because for now, I can not see how the ExamTaker model is involved in your controller.

hasen39's avatar

sorry i update the question that is not the controller the controller is now updated

JohnBraun's avatar

@HASEN39 - Do you get the desired results when you run the following query?

$examTaker = ExamTaker::where('username', $request->search)->get();
dd($examTaker);
JohnBraun's avatar

@HASEN39 - So probably Scout is not the problem. Could you check your database? Does it have entries (for that username)?

hasen39's avatar

@JOHNBRAUN - sorry this code gives me an array of username it is not empty

$examTaker = ExamTaker::where('username', $request->search)->get();
dd($examTaker);
kevinbui's avatar

Please go to Algolia Client Dashboard and enter the same search term to see if it actually works.

Please or to participate in this conversation.