The error message you're seeing indicates that when you're trying to save a MarketResearch model, the user_id field is not being set, and since it doesn't have a default value in the database, the operation fails.
To resolve this issue, you need to ensure that the user_id field is being set before you attempt to save the MarketResearch model. Here's how you can modify your submit method to include the user_id:
public function submit()
{
$this->validate();
// Assuming you want to associate the market research with the currently authenticated user
$this->marketResearch->user_id = auth()->id();
$this->marketResearch->save();
return redirect()->route('admin.market-researchs.index');
}
In the above code, auth()->id() is used to get the ID of the currently authenticated user and assign it to the user_id field of the MarketResearch model before saving it.
Additionally, make sure that the user_id field is fillable in your MarketResearch model. You can do this by adding it to the $fillable array in the model:
class MarketResearch extends Model
{
protected $fillable = [
// ... other fields ...
'user_id',
// ... other fields ...
];
// ...
}
This will allow mass assignment on the user_id field when you're creating or updating a MarketResearch instance.
Lastly, ensure that the user_id field is correctly defined in your database migration for the market_researches table and that it references the id field on the users table if it's supposed to be a foreign key.
Here's an example of how the migration might look:
Schema::create('market_researches', function (Blueprint $table) {
$table->id();
// ... other columns ...
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
// ... other columns ...
});
Make sure to run your migrations if you make any changes to them.