Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mouzak's avatar

No query results for model [App\salaries] id

hi, how to resolve this error please? No query results for model [App\salaries] id controller $data['sala'] = salaries::findOrFail($id) ->relationPaie()->save(new paie()); when i do $data['sala'] = salaries::findOrFail('8') ->relationPaie()->save(new paie()); the request is executed but when i change the number by a variable i got an error.

0 likes
10 replies
ftiersch's avatar

First: formatting... it's really hard to read your question.

Where does your $id variable come from? Obviously there is a wrong value in there :)

mouzak's avatar

@FTIERSCH - $id is primary key in table 'salaries' who have model 'salaries' relationPaie is a methode public function relationPaie(){ return $this->hasOne (paie::class); } I hope it's a little understandable

ftiersch's avatar

Not the database... the variable $id has a value that doesn't exist in your table. Where does that variable come from in the controller?

mouzak's avatar

@FTIERSCH - I want to save $ id table 'salaries' in table 'paies' as foreign key. the variable in the controller comes from the 'salaries' table. I tried to see if it will be registered in the 'paie' table and it is registered by cons if I replace it with $ id the error occurs

ftiersch's avatar

@MOUZAK - The error says that in the salaries table there is no entry with the id that has the value of your variable $id... So can you show the complete controller method?

mouzak's avatar

@FTIERSCH - He's there class PaieController extends Controller {

public function B_paie( Request $request)
{
    $data=array();
    $data['sala'] = salaries::findOrFail('id') ->relationPaie()->save(new paie());
    $data['sal'] = salaries::all();
    $data['parametres'] = parametres::all();
    $data['paies' ]= DB::table('salaries')
            ->leftJoin( 'paie','salaries.id', '=', 'paie.salaries_id')
            ->leftJoin( 'primes','salaries.id', '=', 'primes.idsalaries')
            ->select('salaries.*', 'paie.*','primes.*')
            ->selectRaw('(IFNULL(congepaye, "0")+IFNULL(autreprimes, "0")+ salairebase + sursalaire + primetransport) as total')
            ->where('salaries.id', $request->id)
           //-> whereRaw('paie.mois >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)')
            ->get();
    $data['date']=new \DateTime();
    $data['som']=salaries::selectRaw('(salairebase + sursalaire) as total')
         ->where('salaries.id', $request->id)->get();

return view('bulletin_paie',compact("data"));

}
ftiersch's avatar

There we go... findOrFail('id') looks for an entry in your database with the value 'id' in the id column... that doesn't make sense...

You need to change that to a variable.

$id = 5;
::findOrFail($id) 

And then change the value of the variable to the correct value depending on what you want to do.

1 like
mouzak's avatar

@FTIERSCH - it's ok but I want the insertion is done each time there is a new entry in the table 'salaries'. I thought doing $ id = DB :: table ('salaries') -> id; :: findOrFail ($ id) but it does not work.

ftiersch's avatar
ftiersch
Best Answer
Level 28

Oooh... I get it... You want something completely else...

In your salaries model:

public static function boot() {
    parent::boot();

    self::created(function ($model) {
        $model->relationPaie()->save(new paie());
    });
}

Check out eloquent events for more information on how they work.

1 like
mouzak's avatar

@FTIERSCH - so I have to remove $id = 5; ::findOrFail($id) in the controller. model class salaries extends Model{

public function relationPaie(){
    return $this->hasOne (paie::class);
}
public static function boot() {
    parent::boot();

    self::created(function ($model) {
        $model->relationPaie()->save(new paie());
    });

}

Please or to participate in this conversation.