简体   繁体   English

字段'user_id'没有默认值

[英]Field 'user_id' doesn't have a default value

I'm building a web app with laravel. 我正在用laravel构建一个Web应用程序。

First Question: 第一个问题:

There's a simple form on users dashboard to fill and save. 用户仪表板上有一个简单的表单来填充和保存。
Here's the model: 这是模型:

class Salon extends Model
{
    protected $table = 'salons';
    protected $fillable = [
        'salonname', 'saloncity', 'salonaddress', 'salontel', 'salonmob', 'salonsite', 'saloncat', 'salonkhadamat', 'salonkhadamatprice', 'salondesc', 'saloninsta', 'salontelegram', 'salontags'
    ];
    public $timestamps = false;
}

and here is the controller : 这是控制器:

public function store(Request $request)
{
    $user_id = Auth::user()->id;
    Salon::create([
        'user_id' => $user_id,
        'salonname' => $request['salonname'],
        'saloncity' => $request['saloncity'],
        'salonaddress' => $request['salonaddress'],
        'salontel' => $request['salontel'],
        'salonmob' => $request['salonmob'],
        'salonsite' => $request['salonsite'],
        'saloncat' => $request['saloncat'],
        'salonkhadamat' => $request['salonkhadamat'],
        'salonkhadamatprice' => $request['salonkhadamatprice'],
        'salondesc' => $request['salondesc'],
        'saloninsta' => $request['saloninsta'],
        'salontelegram' => $request['salontelegram'],
        'salontags' => $request['salontags']
    ]);

    return 'done!';
}

And the routes: 路线:

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/salons/add', function () {
    return view('add_salon');
})->middleware('auth');
Route::post('salons', 'SalonsController@store');
Route::get('salons', function () {
    return 'Hi';
});

When I complete the form and hit send button, it returns this error : 当我完成表单并点击发送按钮时,它会返回以下错误:

"SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into salons ( salonname ,... “SQLSTATE [HY000]:常规错误:1364字段'user_id'没有默认值(SQL:插入salonssalons salonname ,...

Where am I doing wrong? 我哪里做错了?
I created a table migration as : 我创建了一个表迁移:

public function up()
{
    Schema::create('Salons', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('salonname');
        $table->string('saloncity');
        $table->string('salonaddress');
        $table->integer('salontel');
        $table->integer('salonmob');
        $table->string('salonsite');
        $table->string('saloncat');
        $table->string('salonkhadamat');
        $table->integer('salonkhadamatprice');
        $table->string('salondesc');
        $table->string('saloninsta');
        $table->string('salontelegram');
        $table->string('salontags');
        $table->timestamps();
    });
}

user_id is using a foreign reference from users table. user_id正在使用来自users表的外部引用。
let me explain the process, consider we have some users registered on our app, some of them want to add their salons on our website, so we want to use the user_id from the users table on salons table, so we can return salons with the user's data (profile) on our homepage. 让我解释一下这个过程,考虑我们在我们的应用程序上注册了一些用户,其中一些想在我们的网站上添加他们的沙龙,所以我们想要使用沙龙桌上的用户表中的user_id,这样我们就可以返回沙龙了用户在我们主页上的数据(个人资料)。

Second question: 第二个问题:

If a salon have two separate telephone numbers, How can I store them in this table separately? 如果沙龙有两个单独的电话号码,我如何将它们分别存放在此表中?
I mean, people can add many telephone-numbers as they want. 我的意思是,人们可以随意添加许多电话号码。 Or as many addresses as they have, in separate fields. 或者尽可能多的地址,在不同的字段中。

Third question: 第三个问题:

For creating a portfolio section for each salon, Should I create a new table such as attachments to have pictures addresses and salon id to return them on their respective page later? 为了为每个沙龙创建一个投资组合部分,我是否应该创建一个新的表格,例如附件,以便将图片地址和沙龙ID在以后的各自页面上返回?

Add user_id to the fillable array too: user_id添加到可填充数组中:

protected $fillable = ['user_id', 'salonname', 'saloncity', 'salonaddress', 'salontel', 'salonmob', 'salonsite', 'saloncat', 'salonkhadamat', 'salonkhadamatprice', 'salondesc', 'saloninsta', 'salontelegram', 'salontags'];

Or use the relationship if it is defined: 或者如果定义了关系,请使用该关系:

$salon = auth()->user()->salons()->create([....

If it's not defined: 如果没有定义:

public function salons()
{
    return $this->hasMany(Salon::class);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现此错误:一般错误:1364 字段“user_id”没有默认值 - Why this ERROR: General error: 1364 Field 'user_id' doesn't have a default value “ SQLSTATE [HY000]:一般错误:1364字段'user_id'没有默认值(SQL:插入 - "SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into SQLSTATE [HY000]:一般错误:1364 字段“user_id”没有默认值 Laravel 8 - SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value Laravel 8 SQLSTATE [HY000]:常规错误:1364字段“ user_id”没有默认值(SQL - SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL Laravel 5.4中的'user_id'没有默认值错误 - 'user_id' doesn't have a default value Error in Laravel 5.4 Illuminate\\Database\\QueryException :SQLSTATE[HY000]:一般错误:1364 字段“user_id”没有默认值(SQL:插入“users” - Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `users` MySQL - 字段“id”没有默认值 - MySQL - Field 'id' doesn't have a default value 字段“csrf”没有默认值 - Field 'csrf' doesn't have a default value 字段“ xxx”没有默认值 - Field 'xxx' doesn't have a default value 字段“x”没有默认值 - Field 'x' doesn't have a default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM