SQLSTATE[HY000]: General error: 1364 Field 'requisition_id' doesn't have a default value - laravel-5.4

Hi following are my relations.
SubmitApplication.php
public function requisition()
{
return $this->hasMany(Requisition::class);
}
Requisition.php
public function submitApplication()
{
return $this->belongsTo(SubmitApplication::class);
}
My blade
<form class="btn-group" action="{{ route('requisitions.update', ['id' => $requisition->id]) }}" method="post">
{{ method_field('PATCH') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-success btn-xs">تایید</button>
</form>
RequisitionController.php
public function update(Request $request, SubmitApplication $submitApplication)
{
$submitApplication->approved = 1;
$submitApplication->save();
return redirect()->back();
}
For requisitions table:
public function up()
{
Schema::create('requisitions', function (Blueprint $table) {
$table->increments('id');
$table->integer('school_id')->unsigned();
$table->string('type');
$table->string('status');
$table->string('date');
$table->timestamps();
$table->foreign('school_id')->references('id')->on('schools');
});
}
For submit_applications table:
public function up()
{
Schema::create('submit_applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('requisition_id')->unsigned();
$table->integer('school_id')->unsigned();
$table->integer('approved');
$table->string('application');
$table->timestamps();
$table->foreign('requisition_id')->references('id')->on('requisitions');
$table->foreign('school_id')->references('id')->on('schools');
});
}
RequisitionController.php
public function update(Request $request, SubmitApplication $submitApplication)
{
$submitApplication->approved = 1;
$submitApplication->save();
return redirect()->back();
}
I Get this error.
SQLSTATE[HY000]: General error: 1364 Field 'requisition_id' doesn't
have a default value (SQL: insert into submit_applications
(approved, updated_at, created_at) values (1, 2018-06-18
04:22:48, 2018-06-18 04:22:48))
When I click approved button. it added. id do not want add
Maybe am I doing wrong?

In your submit_applications table, set nullable to requisition_id
public function up()
{
Schema::create('submit_applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('requisition_id')->nullable()->unsigned();
$table->integer('school_id')->nullable()->unsigned();
$table->integer('approved')->nullable(true);
$table->string('application')->nullable(true);
$table->timestamps();
$table->foreign('requisition_id')->references('id')->on('requisitions');
$table->foreign('school_id')->references('id')->on('schools');
});
}

Related

SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`members` (errno: 150 "Foreign key constraint is incorrectly formed")

I'm using my migrations in Laravel to create the relationships between tables, and I have 4 tables: users, members, member_skills, and skills. I have the following code for the users table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->boolean('admin');
});
}
the members table:
public function up()
{
Schema::create('members', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->string('status');
$table->date('date')->nullable();
$table->text('project')->nullable();
$table->date('start')->nullable();
$table->foreign('name')->references('name')->on('users');
});
}
the member_skills table:
public function up()
{
Schema::create('member_skills', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->string('skill');
$table->foreign('name')->references('name')->on('members');
});
}
and the skills table:
public function up()
{
Schema::create('skills', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('skill');
$table->text('description');
$table->foreign('skill')->references('skill')->on('member_skills');
});
}
However, running my migrations results to (errno: 150 "Foreign key constraint is incorrectly formed"). I have read that changing the migration order should fix the problem, so I have arranged the 4 tables to be migrated in the order of users, members, member_skills, and skills, but I am still receiving the same error. Is there anything else I'm doing wrong?
Here is the right way todo this
public function up()
{
Schema::create('members', function (Blueprint $table) {
...
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
}
public function up()
{
Schema::create('member_skills', function (Blueprint $table) {
...
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('id')->on('members');
});
}
public function up()
{
Schema::create('skills', function (Blueprint $table) {
...
$table->unsignedBigInteger('member_skill_id');
$table->foreign('member_skill_id')->references('id')->on('member_skills');
});
}
more:https://laravel.com/docs/8.x/migrations#foreign-key-constraints
You should try using the id of the member table as the foreign key rather than the name in the member_skills schema
public function up()
{
Schema::create('member_skills', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('member_id');
$table->string('skill');
$table->foreign('member_id')->references('id')->on('members');
});
}
You are getting this error because you are trying to reference name in the member table which is already a foreign key to the users table.
You can access the name of the member through the id foreign key in your blade.

Laravel SQLSTATE[42S22]: Column not found: 1054 Unknown column

I am trying to make a relationship with lecturers table and users table. So this is the code of create_lecturers_table
public function up()
{
Schema::create('lecturers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('class03');
$table->integer('class03_stream');
$table->date('class03_from');
$table->date('class03_to');
$table->string('remarks')->nullable();
$table->integer('created_user_id')->unsigned()->nullable();
$table->integer('updated_user_id')->unsigned()->nullable();
$table->foreign('created_user_id')->references('id')->on('users');
$table->foreign('updated_user_id')->references('id')->on('users');
$table->timestamps();
});
}
This is the Lecturer model
class Lecturer extends Model
{
protected $table = 'lecturers';
protected $fillable = [
'class03', 'class03_stream' ,'class03_from','class03_to','remarks','created_user_id','updated_user_id',
];
public function user(){
return $this->hasMany('PMS\User');
}
}
This is the lecturer index function of the controller
public function index(Lecturer $model)
{
return view('lecturers.index',['lecturers' => $model->paginate(15)]);
}
This is the create_users_table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('user');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('usertype');
$table->boolean('activestatus')->default(false);
$table->rememberToken();
$table->timestamps();
});
}
This is the User model
class User extends Authenticatable
{
use Notifiable;
public function lecturer(){
return $this->belongsTo('PMS\Lecturer');
}
protected $fillable = [
'name', 'email', 'password','usertype',
];
With this what I wants to do is to view the User's name who will create the lecturer via System.So that I have echo the user's name as below in the view.blade.php
<td>{{ $lecturer->user->name }}
When I go to the view it generate this error.
ErrorException (E_ERROR)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.lecturer_id' in 'where clause' (SQL: select * from `users` where `users`.`lecturer_id` = 1 and `users`.`lecturer_id` is not null) (View: E:\BIT FINAL YEAR PROJECTS\20190419-using-template\resources\views\lecturers\index.blade.php)
Could someone please tell me what is the wrong .
Thanks
I think you have your Eloquent relationships the wrong way around. Try changing them to as follows:
Lecturer
public function user(){
return $this->belongsTo('PMS\User', 'created_user_id');
}
User
public function lecturers(){
return $this->hasMany('PMS\Lecturer', 'created_user_id');
}

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'schools.submit_application_id' in 'where clause'

I get this error.
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'schools.submit_application_id' in 'where clause' (SQL: select * from
schools where schools.submit_application_id = 1 and
schools.submit_application_id is not null) (View:
C:\xampp\htdocs\project\tvto\resources\views\Admin\inspectors-list\all.blade.php)
InspectionListController.php
public function index()
{
$inspectors_lists = SubmitApplication::latest()->where('approved', 1)->get();
return view('Admin.inspectors-list.all', compact('inspectors_lists'));
}
all.blade.php
<td>{{ $inspectors_list->school_list->user->first_name }}</td>
SubmitApplication.php
public function school_lists()
{
return $this->hasMany(SchoolsList::class);
}
SchoolsList.php
public function user()
{
return $this->belongsTo(User::class);
}
public function submit_application()
{
return $this->belongsTo(SubmitApplication::class);
}
SubmitApplication.php
public function school_lists()
{
return $this->hasMany(SchoolsList::class);
}
User.php
public function schools()
{
return $this->hasMany(SchoolsList::class);
}
For SubmitApplication table:
public function up()
{
Schema::create('submit_applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('requisition_id')->nullable()->unsigned();
$table->integer('school_id')->nullable()->unsigned();
$table->integer('approved')->nullable(true);
$table->string('application')->nullable(true);
$table->timestamps();
$table->foreign('requisition_id')->references('id')->on('requisitions');
$table->foreign('school_id')->references('id')->on('schools');
});
}
For SchoolList table:
public function up()
{
Schema::create('schools', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('province_name');
$table->string('center_name');
$table->string('national_number_founder');
$table->string('school_name');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}

How to update approved from 0 to 1 in laravel

Hi following are my relations.
SubmitApplication.php
public function requisition()
{
return $this->hasMany(Requisition::class);
}
Requisition.php
public function submitApplication()
{
return $this->belongsTo(SubmitApplication::class);
}
My blade
<form class="btn-group" action="{{ route('requisitions.update', ['id' => $requisition->id]) }}" method="post">
{{ method_field('PATCH') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-success btn-xs">تایید</button>
</form>
RequisitionController.php
public function update(Request $request, SubmitApplication $submitApplication)
{
$submitApplication->approved = 1;
$submitApplication->save();
return redirect()->back();
}
For requisitions table:
public function up()
{
Schema::create('requisitions', function (Blueprint $table) {
$table->increments('id');
$table->integer('school_id')->unsigned();
$table->string('type');
$table->string('status');
$table->string('date');
$table->timestamps();
$table->foreign('school_id')->references('id')->on('schools');
});
}
For submit_applications table:
public function up()
{
Schema::create('submit_applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('requisition_id')->nullable()->unsigned();
$table->integer('school_id')->nullable()->unsigned();
$table->integer('approved')->nullable(true);
$table->string('application')->nullable(true);
$table->timestamps();
$table->foreign('requisition_id')->references('id')->on('requisitions');
$table->foreign('school_id')->references('id')->on('schools');
});
}
When I click approved button. it added. id do not want add
Maybe am I doing wrong?
In your RequisitionController.php, you must retrieve first the model before using save method.
$submitApplication = SubmitApplication ::find($request['id']);

Cannot add foreign key constraint [Laravel 5.6]

tl;dr. Solution:
Thanks to Jonas.
the problem was that the tables I was referring as foreign, were not InnoDB.
I added raw SQL statements in alter migrations and then added the foreign keys:
DB::statement("ALTER TABLE table ENGINE='InnoDB';");
Original question
First, before the Stackoverflow police bust me, I know this question is probably 83% of the database of this website. But me is special (Kidding, I know I'm not). But I've tried most of the common stuff and nothing seems to work. So probably I'm overseeing something.
Error
General error: 1215 Cannot add foreign key constraint (SQL: alter table applications add constraint applications_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
This is my migration:
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->timestamps();
});
Schema::table('applications', function($table) {
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned()->index();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
What I've already tried:
1.
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
2.
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
Splitting the migration into two files (A create and an alter). Even adding each reference one by one.
4.- using DB::statement('SET FOREIGN_KEY_CHECKS=0;'); and =1 at the beginning an the end og the migration.
5.- removing the unsigned() and the index().
Might mean something:
1.- When I rollback the migration, it doesn't delete the table. So if I rollback and migrate, would give me a "already exists error".
2.- I already have migrations which reference the same items, i.e:
Schema::create('job_requests', function (Blueprint $table) {
...
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
...
});
UPDATE
For the drop methods I've tried:
For the create migrations
public function down()
{
Schema::drop('applications');
}
public function down()
{
Schema::dropIfExists('applications');
}
2.- For the alter migrations
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropColumn('user_id');
$table->dropForeign(['job_request_id']);
$table->dropColumn('job_request_id');
});
}
UPDATE 2:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
public function up()
{
Schema::create('job_requests', function (Blueprint $table) {
$table->increments('id');
$table->integer('status')->default(0);
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('job_requests');
}
I added three more alter migration:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->engine = "InnoDB";
});
}
///////////////////////////
public function up()
{
Schema::table('job_requests', function (Blueprint $table) {
$table->engine = "InnoDB";
});
}
///////////////////////////
public function up()
{
Schema::table('applications', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['job_request_id']);
});
}
Without luck yet.
The referenced tables also have to use the InnoDB engine.
You can change them with raw SQL statements:
DB::statement("ALTER TABLE users ENGINE='InnoDB';");
DB::statement("ALTER TABLE job_requests ENGINE='InnoDB';");
for deleting the table:
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('applications');
}