Below is an API to upload files, and it displays the following error.
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "Userid" of relation "files" violates not-null constraint\nDETAIL: Failing row contains (7, null, Distance Learning Promo.mp4, null, null, null, 2021-07-07 13:38:07, 2021-07-07 13:38:07). (SQL: insert into "files" ("FileName", "updated_at", "created_at") values (Distance Learning Promo.mp4, 2021-07-07 13:38:07, 2021-07-07 13:38:07) returning "id")
Also, Postman shows the status 500 internal status error. I don't think my Userid column here is null. Even if Userid column is inserted a dummy value like this: $dataToInsert['Userid'] =1. Also, it shows the same error.
Controller
class UploadController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
public function upload(Request $request)
{
$file = $request->file('file_name');
$fileName = $file->getClientOriginalName();
$folder = uniqid() . '-' . now()->timestamp;
$file->storeAs('public/other-document/' . $folder, $fileName);
$filePath = $folder . '/' . $fileName;
$dataToInsert = array();
$dataToInsert['FileName'] = $fileName;
$dataToInsert['FilePath'] = $filePath;
if ($request->has('uploadType')) {
$dataToInsert['FileType'] = $request->uploadType;
}
$dataToInsert['uploadedBy'] = Auth::user()->id;
$dataToInsert['Userid'] = $request->bearerToken();
Uploads::create($dataToInsert);
}
}
Migration
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('Userid');
$table->string('FileName');
$table->string('FilePath');
$table->string('FileType');
$table->integer('uploadedBy');
$table->timestamps();
});
I am so sorry. I can't write comments yet. But did you add fields to Model fillable array?
I had a similar problem. And that was because i didn't add that field to fillable array of Model.
Related
Below given is an API to upload files.
it shows error -
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \"Userid\" of relation \"files\" violates not-null constraint\nDETAIL: Failing row contains (7, null, Distance Learning Promo.mp4, null, null, null, 2021-07-07 13:38:07, 2021-07-07 13:38:07). (SQL: insert into \"files\" (\"FileName\", \"updated_at\", \"created_at\") values (Distance Learning Promo.mp4, 2021-07-07 13:38:07, 2021-07-07 13:38:07) returning \"id\") .
And Postman shows the status 500 internal status error.
I don't think my Userid column here is null . Pls help me with some suggestions
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Uploads;
use Illuminate\Support\Facades\Auth;
class UploadController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
public function upload(Request $request)
{
$file=$request->file('file_name');
$fileName=$file->getClientOriginalName();
$folder = uniqid().'-'.now()->timestamp;
$file->storeAs('public/other-document/'.$folder, $fileName);
$filePath = $folder.'/'.$fileName;
$dataToInsert = array();
$dataToInsert['FileName'] = $fileName;
$dataToInsert['FilePath'] = $filePath;
if($request->has('uploadType'))
{
$dataToInsert['FileType'] = $request->uploadType;
}
$dataToInsert['uploadedBy'] = Auth::user()->id;
$dataToInsert['Userid'] = $request->bearerToken();
Uploads::create($dataToInsert);
}
}
Migration file
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('Userid');
$table->string('FileName');
$table->string('FilePath');
$table->string('FileType');
$table->integer('uploadedBy');
$table->timestamps();
});
$dataToInsert['Userid'] = $request->bearerToken(); // error is here
if(!$dataToInsert['Userid']){
return "Null value in userID";
}
Check the $dataToInsert['Userid'] holds any value.
Also make Sure that Your are Passing bearerToken in header
when I use updateOrCreate method on MySQL it's working fine on both conditions create or update. but when I use in PostgreSQL it's update is fine but when creates, show this error:
"SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "group_id" violates not-null constraint DETAIL: Failing row contains (null, aaa, 2020-06-24 17:14:54, 2020-06-24 17:14:54). (SQL: insert into "groups" ("group_id", "title", "updated_at", "created_at") values (?, aaa, 2020-06-24 17:14:54, 2020-06-24 17:14:54) returning "group_id")"
my code:
protected $primaryKey = 'group_id';
protected $guarded = [''];
public $incrementing = true;
public function updateOrCreateGroup($data, $id)
{
return $this::updateOrCreate(
['group_id' => $id],
$data
);
}
I;m using laravel and vue if that makes any difference. The data input I'm using if coming from an excel file, i can see the array of data when i inspect the website so i know the information is there, I'm trying to insert that info into the users table to do a massive user import option.
Basically how I'm doing it is, I'm receiving an excel that gets turned into an array, i separate that array into smaller arrays with only 5 users inside, and iterating thru those and saving that into the users table. The excel is full of info and there isn't anything that is empty.
My store method on the ImportUserController
public function store(Request $request)
{
ini_set('max_execution_time', 300);
$users = $request->all();
try {
DB::beginTransaction();
foreach ($users as $user) {
$dbUser = $this->getUser($user['id']);
Log::error($dbUser);
$dbUser->name = $user['NAME'];
$dbUser->user_name = $user['USERNAME'];
$dbUser->email = $user['EMAIL'];
$dbUser->last_name = $user['LAST-NAME'];
$dbUser->password = $user['PASSWORD'];
$this->isSet('USER-TYPE-ID', $user);
$user_type_id = $this->getUserId($user['USER-TYPE-ID']);
$dbUser->user_type_id = $user_type_id->id;
$dbUser-> save();
}
DB::commit();
} catch (Exception $e) {
DB::rollBack();
throw new HttpException(500, 'Sucedio un error importando la informaciĆ³n favor intentar de nuevo');
}
}
private function isSet($field, $reqData)
{
if (!isset($reqData[$field])) {
throw new HttpException(500, "Sucedio un error importando la informaciĆ³n. No se encuenta la columna $field");
}
}
private function getUserId($name)
{
$userId = UserType::where('name', $name)->first();
if (empty($userId)) {
$userId = new UserType();
$userId->name = $name;
$userId->save();
}
return $userId;
}
private function getUser($id)
{
$user = User::where('id', $id)->first();
if (empty($user)) {
$user = new User();
}
return $user;
}
this is the create users file
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('user_name')->unique();
$table->string('email')->unique();
$table->string('last_name');
$table->string('password', 60);
$table->integer('user_type_id')->unsigned();
$table->foreign('user_type_id')->references('id')->on('user_types');
$table->rememberToken();
$table->timestamps();
});
I'm not sure where the null is coming from? or why this is producing this error.
first print all of your input values then check that ,all values are coming correctly
I'm new to CakePHP. I have a table to keep record of user's activity by creating a log in it. The table has two columns
+----+------------+-----------+
| id | user_id | comment |
+----+------------+-----------+
I want to pass values from within controller like
$this->ActivityLogs->log($user_id, 'Message sent');
log is a custom function inside ActivityLogs model which will record some more data along with passed data
public function log($user_id = null, $message = null)
{
... record code goes here
return true;
}
But couldn't get how to write insert query inside model.
How can I create custom methods like this and also can anyone suggest me good resource to go through model queries and understanding.
public function log($user_id = null, $message = null){
//I assume here that your table name is 'logs'
$logsTable = \Cake\ORM\TableRegistry::get('Logs', array('table' => 'logs'));
$log = $logsTable->newEntity();
$log->user_id = $user_id;
$log->body = $message ;
if ($logsTable->save($log)) {
return true;
}
return false;
}
I use this syntax in the AppModel
public function __construct($id = false, $table = null, $ds = null)
{
parent::__construct($id, $table, $ds);
$this->setDataSource('new_db');
}
The problem is that when this function is called,
Database Error
Error: SQLSTATE[42P01]: Undefined table: 7 ERROR:
missing FROM-clause entry for table "Aco" LINE 1: ...AND "Permission"."aco_id" IN (3, 2, 1) ORDER BY "Aco"."lft... ^
I think this error appears due to the fact that the component ACL is linked to a database which is the default.