lundi 3 septembre 2018

Call to a member function rawFirms() on null

I have a model called Raw that has many RawFirms

class Raw extends Model
{

    public function rawFirms(){
        return $this->hasMany(RawFirm::class);
    }
}

and in my Seeder I have this code part:

try{
    $raw = $raw_children->raws()->create([
        'name'=>$leaf,
        'quantity'=>0
     ]);
    // if(!$raw){
    //     throw new \Exception;
    // }
}
catch(Illuminate\Database\QueryException $e){
    $error_code = $e->errorInfo[1];
    if($error_code == 1062){
        $raw = Raw::where('name', $l)->first();
    }
}

$position_id = $faker->numberBetween(1, 432);
//creating rawFirm for three types
if($firm_id != ''){
    foreach(range(0, 2) as $type){
        $raw->rawFirms()
        ->create([
            'firm_id'=>$firm_id,
            'position_id'=>$position_id,
            'quantity'=>0,
            'type'=>$type,
        ]);
    }
}

When I want to create rawFirm by $raw->rawFirms(), it shows an error

Call to a member function rawFirms() on null.

However, I debugged raw object and it returned not-null object. Can anyone help me find the problem.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire