简体   繁体   中英

Field 'user_id' doesn't have a default value

I'm building a web app with laravel.

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 ,...

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.
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.

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?

Add user_id to the fillable array too:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM