J
J
Jedi2018-05-18 13:33:49
Laravel
Jedi, 2018-05-18 13:33:49

many to many. Why does an error occur when creating a record: id does not exist?

No matter how much I searched for the answer in the documentation, I did not find it.
Here is the structure of the User categories table:

Schema::create('user_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->unsignedInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories');
            $table->timestamps();
        });

Categories table structure:
Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title_ru', 50);
            $table->string('title_en', 50);
            $table->string('slug', 50);
            $table->timestamps();
        });

Attempting to create an entry:
$user_category = new UserCategory();
        $user_category->user_id = Auth::user()->id();
        $user_category->category_id = $request->category_id;

        if ($user_category->save()) {
            return response()->json(['response' => 'success']);
        }

I ask you to help solve the problem ...
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2018-05-18
@PHPjedi

Auth::user()->id();
does he exist?
and in general it would not be bad to show the code of the models.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question