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();
});
}
Auth::user()instead ofauth('api')->user()and check it works or not