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

Antonella's avatar

problems with inserting in the DB

I am creating a project for a multilingual site using this package: https://github.com/spatie/laravel-translatable

unfortunately it does not allow me to enter the data:

I created an Article table with three fields: id, title, body.

then I created a middleware to manage languages:

class SetLanguage
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        \App::setLocale($request->language);

        URL::defaults(['language' => $request->language]);

        $request->route()->forgetParameter('language');

        return $next($request);
    }

then the web routes:

Route::group(['prefix'=>'{language}'], function(){

    Route::get('articles/create', [ArticleController::class, 'create']);
    Route::post('articles', [ArticleController::class, 'store']);
   
});

then I created the following methods in the Article controller to be able to insert the data

public function create()
    {
        return view('createArticle');
    }

    public function store()
    {
         $article = new Article();
         $article->title = request('title');
         $article->body = request('body');
         $article->save();
    }

when I go to one of the following links (http://towerb.test/it/articles/create or http://towerb.test/fr/articles/create ecc) to enter the data I get the following error:

Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into articles (title, body, updated_at, created_at) values ({"it":"I try to insert the title"}, {"it":"I try to insert the body"}, 2020-12-15 09:42:35, 2020-12-15 09:42:35))

Also how can I enter data in multiple languages at the same time?

0 likes
2 replies
Snapey's avatar

did you read the error message?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Check your migration, and rewrite this part

I created an Article table with four fields: id, title, body and user_id

Please or to participate in this conversation.