dane5890's avatar

Setting Up A One To One Relationship With User Model

Hi all. I'm still working on my Laravel 8 ecommerce project. I arleady have a user model with a user table for email and password. But now I need to give registered users the ability to create a shipping address to shipping rates to them. I created a user address table, and in the UserAddress model, tried to build a relationship with the User's id in the user table, but when the user submits the address form, I keep getting a General error: 1364 Field 'member_id' doesn't have a default value (SQL: insert into user_addresses error.

Here is my code in the user address model

class UserAddress extends Model { use HasFactory;

protected $guarded = [];

public function user(){

return $this->belongsTo(User::class,'member_id','id'); }

Here is the code for the user address migration

public function up()
{
    Schema::create('user_addresses', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('member_id');
        $table->foreign('member_id')->references('id')->on('users')->onDelete('cascade');;
        $table->string('name');
        $table->string('shipping_last_name');
        $table->string('company')->nullable();
        $table->string('city');
        $table->string('street1');
        $table->string('street2')->nullable();
        $table->string('state');
        $table->string('country');
        $table->string('phone');
        $table->string('email');
        $table->integer('zip');
        $table->text('notes')->nullable();
        $table->integer('address_status')->default(0);
        $table->timestamps();
    });
}

Lastly, here is the code for my address form in the user dashboard

                                            @csrf

                                                <div class="checkbox-form">
                                                    <h3>Billing Details</h3>
                                                    <div class="row">

                                                        <div class="col-md-12">
                                                            <div class="country-select clearfix">
                                                                <label>Country <span class="required">*</span></label>
                                                                <input placeholder="Country" name="country" type="text">
                                                            </div>
                                                        </div>

                                                        <div class="col-md-6">
                                                            <div class="checkout-form-list">
                                                                <label>First Name <span class="required">*</span></label>
                                                                 <input placeholder="First Name" name="name" type="text">
                                                            </div>
                                                        </div>

                                                        <div class="col-md-6">
                                                            <div class="checkout-form-list">
                                                                <label>Last Name <span class="required">*</span></label>
                                                                 <input placeholder="Last Name" name="shipping_last_name" type="text">
                                                            </div>
                                                        </div>
                                                        <div class="col-md-12">
                                                            <div class="checkout-form-list">
                                                                <label>Shipping Email</label>
                                                                <input placeholder="Email" name="email" type="email">
                                                            </div>
                                                        </div>
                                                        <div class="col-md-12">
                                                            <div class="checkout-form-list">
                                                                <label>Company Name</label>
                                                                <input placeholder="" name="company" type="text">
                                                            </div>
                                                        </div>
                                                        <div class="col-md-12">
                                                            <div class="checkout-form-list">
                                                                <label>Address <span class="required">*</span></label>
                                                                 <input placeholder="Street address" name="street1" type="text">
                                                            </div>
                                                        </div>
                                                        <div class="col-md-12">
                                                            <div class="checkout-form-list">
                                                                <input placeholder="Apartment, suite, unit etc. (optional)" name ="street2" type="text">
                                                            </div>
                                                        </div>
                                                        <div class="col-md-12">
                                                            <div class="checkout-form-list">
                                                                <label>Town / City <span class="required">*</span></label>
                                                                <input type="text" name="city">
                                                            </div>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="checkout-form-list">
                                                                <label>State / County <span class="required">*</span></label>
                                                                <input placeholder="" name="state" type="text">
                                                            </div>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="checkout-form-list">
                                                                <label>Postcode / Zip <span class="required">*</span></label>
                                                                <input placeholder="" name="zip" type="text">
                                                            </div>
                                                        </div>

                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="checkout-form-list">
                                                                <label>Phone <span class="required">*</span></label>
                                                               <input type="text" name="phone">
                                                            </div>
                                                        </div>

                                          <div class="order-button-payment">
                                              <input value="Submit Address" type="submit">
                                          </div>
                                </form>

Am I missing member_id input in the form? If so, how do I include it.

Thanks

0 likes
6 replies
Snapey's avatar

isn't it the current user?

Its the controller code we need to see

dane5890's avatar

@Snapey That's the idea for the current user to create the shipping address. Here's the controller code

public function UserShip(Request $request) {

UserAddress::insert([

  'name' => $request->name,
  'shipping_last_name' => $request->shipping_last_name,
  'email' => $request->email,
  'phone' => $request->phone,
  'zip' => $request->zip,
  'state' => $request->state,
  'street1' => $request->street1,
  'street2' => $request->street2,
  'company' => $request->company,
  'city' => $request->city,
  'country' => $request->country,
  'address_status' => 1,
  'notes' => $request->notes,
  'member_id' => $request->member_id,

]);

$notification = array(

  'message' => 'User Address Inserted Successfully',
  'alert-type' => 'success'

);

return view('dashboard')->with($notification);

}

jonlink's avatar

@dane5890 doesn't look like you're passing in the member id, so it's null. Try this:

public function UserShip(Request $request) { 
    dd($request->all());
}

If you don't see member_id in the array, that's the issue.

If you just want it to be the user that's currently logged in you can do:

'member_id' => Auth::id(),
frankielee's avatar

or set the relationship in User model https://laravel.com/docs/8.x/eloquent-relationships#the-create-method

public function address()
{
      return $this->hasOne(UserAddress ::class, 'member_id');
}

//Controller

auth()->user()->address()->create([

  'name' => $request->name,
  'shipping_last_name' => $request->shipping_last_name,
  'email' => $request->email,
  'phone' => $request->phone,
  'zip' => $request->zip,
  'state' => $request->state,
  'street1' => $request->street1,
  'street2' => $request->street2,
  'company' => $request->company,
  'city' => $request->city,
  'country' => $request->country,
  'address_status' => 1,
  'notes' => $request->notes,
  'member_id' => $request->member_id,

]);

dane5890's avatar

@frankielee I've done something similar in the User Address Model like

public function user(){

return $this->belongsTo(User::class,'member_id','id'); }

frankielee's avatar

@dane5890 You need to declare for both models, so that both models are connected to each others.

Edit: The answer provided by @jonlink is working too.

Please or to participate in this conversation.