Laravel添加新条目时,将当前用户的ID添加到表中

发布于 2025-02-07 14:02:44 字数 1921 浏览 3 评论 0原文

是的,所以我有一个带有用户ID和另一个避难所的用户表,用户将在表中添加避难所,我想向该人的用户ID展示,这些人在我使用的$ table-> foreferid('id') - >co点('id') - > on('用户');那给了我 SQLSTATE [HY000]:常规错误:1364字段'ID'没有默认值

避难所表

 public function up()
    {
        Schema::create('animal_shelters', function (Blueprint $table) {
            $table->id('shelter_id');
            $table->timestamps();
            $table->foreignId('id')->references('id')->on('users');
            $table->string('shelter_name');
            $table->string('shelter_address');
            $table->string('shelter_mobile_number');
            $table->string('shelter_type');
        });
    }
 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->string('profile_photo_path', 2048)->nullable();
            $table->timestamps();
        });
    }

错误:

INLUMINES \ DATABASE \ QUERYEXECTION

SQLSTATE [HY000]:常规错误:1364字段'ID'没有默认值

插入进入数据库

public function store(Request $request)
    {
$Animal_shelter = new Animalshelter();
$Animal_shelter->shelter_name = $request->input('shelter_name');
 $Animal_shelter->shelter_address = $request->input('shelter_address');
 $Animal_shelter->shelter_mobile_number = $request->input('shelter_mobile_number');
 $Animal_shelter->shelter_type = $request->input('shelter_type');
  $Animal_shelter->save();
  return redirect()->back()->with('status', 'shelter added successfully');
    }

yeah so I have a user table with user id and another table of shelters, users will add shelters to the table and I want to show the user id of the person that adds that particular shelter to the table i am using $table->foreignId('id')->references('id')->on('users'); that gives me
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value

Shelter Table

 public function up()
    {
        Schema::create('animal_shelters', function (Blueprint $table) {
            $table->id('shelter_id');
            $table->timestamps();
            $table->foreignId('id')->references('id')->on('users');
            $table->string('shelter_name');
            $table->string('shelter_address');
            $table->string('shelter_mobile_number');
            $table->string('shelter_type');
        });
    }
 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->string('profile_photo_path', 2048)->nullable();
            $table->timestamps();
        });
    }

Error :

Illuminate \ Database \ QueryException

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value

inserting into database

public function store(Request $request)
    {
$Animal_shelter = new Animalshelter();
$Animal_shelter->shelter_name = $request->input('shelter_name');
 $Animal_shelter->shelter_address = $request->input('shelter_address');
 $Animal_shelter->shelter_mobile_number = $request->input('shelter_mobile_number');
 $Animal_shelter->shelter_type = $request->input('shelter_type');
  $Animal_shelter->save();
  return redirect()->back()->with('status', 'shelter added successfully');
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苯莒 2025-02-14 14:02:44

这仅仅是因为您未设置user_id字段。只需在save()之前将其添加到您的

$Animal_shelter->user_id = auth()->id() // In case you need to get authenticated user id

$table->foreignId('user_id')->nullable()->constrained();

对象 ,您可以链函数,类似

auth()->user()->shelters()->create(['your data']);

第二个解决方案
第三个解决方案

It's just because you not set the user_id field. simply add it to your object before save() will fix the problem

$Animal_shelter->user_id = auth()->id() // In case you need to get authenticated user id

Or just change your shelter migration file so the column accept null value

$table->foreignId('user_id')->nullable()->constrained();

Or last solution, if you set up relationship between User and Shelter on eloquent level, you can chain function, something like this

auth()->user()->shelters()->create(['your data']);

Second solution
Third solution

巷子口的你 2025-02-14 14:02:44

不,它不会自动添加它。当您添加到庇护所表中时,您必须将外国ID设置为正在创建它的用户的用户ID。

No, it won't add it automatically. When you add to the shelter table you must set the foreignid to the user id of the user that is creating it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文