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

Emokores's avatar

foreign key not being stored in the database

I have a model Order that stores orders, and has a foreign key team_id that identifies a specific order for a particular team. I used boot() method in a trait to store the team_id automatically:


trait FilterByTeam
{
     protected static function boot()
     {
          parent::boot();

          self::creating(
             fn ($model) => $model->team_id = auth()->user()->current_team
          );

      }
}

Every other model works fine, and the data is stored. With this Order model, I get a QueryException from Eloquent: SQLSTATE[HY000]: General error: 1364 Field 'team_id' doesn't have a default value. I don't know what I'm doing wrong. In my $fillable[], I have team_id as fillable.

0 likes
29 replies
anilkumarthakur60's avatar

@Emokores

 protected static function boot()
    {
        parent::boot();
        self::creating(function ($model) {
            $model->team_id = auth()->user()->current_team;
        });
        
    }

this should probably work .....

Sinnbeck's avatar

Can you show one model that works and one that does not?

Emokores's avatar

@Sinnbeck In the create(), Laravel Ignition highlights this line in the create() method:


Order::create([
     $request->validated(),
     'customer_id' => $request->customer_id,
     'order_date' => $request->order_date ?? date('Y-m-d'),
     'service_id' => $request->service_id,
     'discount' => $request->discount ?? 0,
     'user_id' => auth()->id(),
     'quantity' => $request->quantity,
     'balance' => ceil($total - ($total * $request->discount)),
     'updated_by' => auth()->id(), //* <---- this line
]);

Sinnbeck's avatar

@Emokores yeah that makes sense as that is where the error originates. I have never set a parameter like that in a hook, but if you say it works on other models, I assume it has to do with the model.

Emokores's avatar

@Sinnbeck It was working before until I added the team management functionality into the project.

Sinnbeck's avatar

@Emokores so that team_id isn't actually working for other models?

Order::create([
    ...$request->validated(),
     'customer_id' => $request->customer_id,
     'order_date' => $request->order_date ?? date('Y-m-d'),
     'service_id' => $request->service_id,
     'discount' => $request->discount ?? 0,
     'user_id' => auth()->id(),
     'quantity' => $request->quantity,
     'balance' => ceil($total - ($total * $request->discount)),
     'updated_by' => auth()->id(),
    'team_id' => auth()->user()->current_team 
]);
Emokores's avatar

@Sinnbeck But I can't understand why the trait is not working for this particular model?

Emokores's avatar

@Sinnbeck But I have another boot() method in the Order model that generates order numbers (alpha-numeric). Could it be affecting the trait?


public static function boot()
    {
        parent::boot(); //* runs the autogenerated stub

        //* autogenerated stub
        self::creating(function ($model) {
            $model->number = $model->whereMonth('order_date', Carbon::now()->month)->max('number') + 1;
            $model->order_no = 'H5M-O' . date('ym') . str_pad($model->number, 5, '0', STR_PAD_LEFT);
        });
    }

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Emokores yeah that overwrites it. You need to import the method from the trait and run it.

Might just be easier to add it manually here (at least for testing)

1 like
Emokores's avatar

@Sinnbeck Yeah I thought the same thing. I copied and pasted the code in the boot() method of the trait into that of the model and everything works fine.

Just a question: Can you merge two boot() methods in Laravel?

Sinnbeck's avatar

@Emokores you can import a method from a trait with another name and run it. But not sure how it will work as you are hooking into creating twice then

1 like
Emokores's avatar

@Sinnbeck Oh, I see! Then using the boot() method of the model is the best solution, and putting all the logic you want there. So now, that model no longer uses the trait, but its boot() method.

Sinnbeck's avatar

@Emokores something like this btw

use FilterByTeam {
       boot as teamBoot;
   }

And

public static function boot()
    {
        parent::boot(); //* runs the autogenerated stub
        self::teamBoot();
1 like
Emokores's avatar

@Sinnbeck Wow! This is new! I've just tried this but I have a problem with my editor. It changes the public static function boot() { .. } of the model to public function boot(){ ... } and that gives me an error from Eloquent. I'm using VS Code with Intelephense extension

Emokores's avatar

@Sinnbeck But when I edit the file outside of my editor, everything still works as fine.


use FilterByTeam {
      boot as teamBoot;
}

public static function boot()
{
        parent::boot(); //* runs the autogenerated stub

       // ...  <--- boot logic for the model

        self::teamBoot(); //* boot logic from the trait
}

Sinnbeck's avatar

@Emokores you might need to change it slightly as it's a static function. I wrote it from memory and I am unsure of static methods. Perhaps

use FilterByTeam {
       boot as public static teamBoot;
   }
1 like
Sinnbeck's avatar

@Emokores sorry will take some research to find out how to do it. I cannot currently find anything sadly

Emokores's avatar

@Sinnbeck Actually, your first answer was correct. I think Laravel is opinionated about the naming of the boot() method. I think you have to name it with bootTheAction()


use FilterByTeam {
    boot as bootTeamIdentifier;
}

I changed it to this and my editor stopped formatting my static method wrongly, bringing the error.

anilkumarthakur60's avatar

slightly change your Order::create() method

Order::create(
     $request->validated()+[
     'customer_id' => $request->customer_id,
     'order_date' => $request->order_date ?? date('Y-m-d'),
     'service_id' => $request->service_id,
     'discount' => $request->discount ?? 0,
     'user_id' => auth()->id(),
     'quantity' => $request->quantity,
     'balance' => ceil($total - ($total * $request->discount)),
     'updated_by' => auth()->id(), //* <---- this line
]);

Emokores's avatar

@anilkumarthakur60 I have team_id in my $fillable[] array. But I have another boot() method in my Order model that autogenerates order numbers (alpha-numeric). Could it be affecting the trait?


public static function boot()
    {
        parent::boot(); //* runs the autogenerated stub

        //* autogenerated stub
        self::creating(function ($model) {
            $model->number = $model->whereMonth('order_date', Carbon::now()->month)->max('number') + 1;
            $model->order_no = 'H5M-O' . date('ym') . str_pad($model->number, 5, '0', STR_PAD_LEFT);
        });
    }

Please or to participate in this conversation.