0

I want to get the logged in user id and insert it into the Salesprice table,I have tried the below code but it gives me the this error

"SQLSTATE[HY000]: General error: 1364 Field 'setby_id' doesn't have a default value (SQL: insert into salesprices (book_id, salesprice, remarks, updated_at, created_at) values (2, 5566, ?, 2020-01-31 03:59:58, 2020-01-31 03:59:58))"

code in controller is

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Salesprice;
use Illuminate\Support\Facades\Hash;
use App\User;
use Illuminate\Support\Facades\Auth;
public function store(Request $request) 
{
         try 
          {
              $user = auth('api')->user();

                 $this->validate($request,[
                    'book_id'=>'required', 
                    'salesprice'=>'required', 
                     ]);
                    $Salesprice= Salesprice::create([
                    'book_id'=>$request['book_id'], 
                    'salesprice'=>$request['salesprice'], 
                    // 'setdate'=>$request['setdate'],  
                    'setby'=> $user->id, 
                    'lastmodifiedby'=>$user->id, 
                    'remarks'=>$request['remarks']
                    ]);
                    return response()->json($Salesprice);

            }

          catch (Exception $e) 
          {
                        return response()->json($e->getMessage(), 500);
          }
}

code in migration and model is

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Salesprice extends Model
{
    //
    use SoftDeletes;
    protected $fillable = [
        'book_id','salesprice','setby_id','modifiedby_id','remarks'
    ];
     public function Book()
    {
        return $this->hasOne('App\Book');
    }
     public function User()
    {
        return $this->hasOne('App\User');
    }
    protected $dates=['deleted_at'];
}


 public function up()
    {
        Schema::create('salesprices', function (Blueprint $table) {
            $table->bigIncrements('id');
             $table->bigInteger('book_id')->unsigned();
             $table->bigInteger('salesprice');
             $table->bigInteger('setby_id')->unsigned();
             $table->bigInteger('modifiedby_id')->unsigned();
              $table->string('remarks')->nullable();
              $table->softDeletes();
            $table->timestamps();
        });
    }
3
  • try this Auth::user() instead of auth('api')->user() and check it works or not Commented Jan 31, 2020 at 4:30
  • I did, it dose not work. Commented Jan 31, 2020 at 4:33
  • check the store function it is doing the insertion Commented Jan 31, 2020 at 4:34

3 Answers 3

1

auth()->user()is correct The only thing you have to do is to change your

setby to setby_id Becaseu you're doing:

'setby'=> $user->id,

but your column is named setby_id, so you need:

'setby_id'=> $user->id,
Sign up to request clarification or add additional context in comments.

Comments

1

Because this field cannot be nullable.

So when you insert the value without setby_id, it will fail.

Change setby to setby_id

$Salesprice= Salesprice::create([
                    ... 
                    'setby_id'=> $user->id,  // change to setby_id
                    ...
                    ]);

Comments

1

Nothing's wrong with auth()->user().

You're doing:

'setby'=> $user->id,

but your column is named setby_id, so you need:

'setby_id'=> $user->id,

You'll have the same issue with lastmodifiedby versus modifiedby_id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.