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']);
Related
Thank you in advance
I have 2 tables as below
1) users
2) reviews
The schema I have created is like below
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('first_name',100)->nullable();
$table->timestamps();
});
Schema::create('reviews', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->string('name',100)->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
When i am deleting the user, it is not deleting reviews belongs to the deleted user
I am deleting user like below
User::where('id',1)->delete();
is there any short and simple and perfect way to do them for all the eloquent models?
you can try this code in your parent model :
public static function boot()
{
parent::boot();
static::deleting(function ($model) {
$model->relations()->delete();
});
}
*update for softDeletes
public static function boot()
{
parent::boot();
static::deleting(function ($model) {
if($model->isForceDeleting()){
$model->relations()->delete();//or forceDelete if child also using softDelete
});
}
}
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');
}
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');
});
}
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');
}
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');
});
}