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

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');
});
}

Related

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');
}

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');
}

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

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');
});
}

Base table or view not found: 1146 Table 'prj_oxbir.permissions' doesn't exist

Permissions file does exist on the folder though. Why this error is showing.
In Connection.php line 647:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'prj_oxbir.permissions' doesn't exist (SQL: select * from
permissions)
In Connection.php line 319:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'prj_oxbir.permissions' doesn't exist
createrolestable.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('permission_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->primary(['role_id' , 'permission_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['role_id' , 'user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
First of all, make sure table permissions exist in the database.
Secondly, create a separate migrations for permissions table.
hope this helps.

Add two queries at store method in controller

I have this migration table for OFFERS:
public function up()
{
Schema::create('offers', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('article_id')->unsigned();
$table->integer('price');
$table->string('comment');
$table->timestamps();
$table->string('key');
$table->string('title');
$table->timestamp('start');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('article_id')
->references('id')
->on('articles')
->onDelete('cascade');
});
}
Also this at model I have:
class Offer extends Model
{
//
protected $fillable = [
'price',
'comment',
'article_id',
'key',
'start'
];
protected $dates = [
'start'
];
public function setStartAttribute($date){
$this->attributes['start']= Carbon::createFromFormat('m/d/Y h:i a', $date);
}
public function getStartAttribute($date){
return (new Carbon($date))->toRfc2822String();
}
public function user(){
return $this->belongsTo('App\User');
}
public function article(){
return $this->belongsTo('App\Article');
}
}
Now at controller I have just store function:
public function store(Requests\OfferRequest $request)
{
$offer = new Offer($request->all());
Auth::user()->offer()->save($offer);
Alert::success('Offer is succesfully added!', 'Good job!')->persistent("Close");
return Redirect::back();
}
Now I also have the same model MAXOFFER. The same is migartion and model.
What I want to do is to add at OffersController store method a query that will chech does start excist in maxoffers table and if not then to add new row with data from request, but if excist row with same start value then to check price and if price is higher than current to update that row...
Please help me to rite that query inside store function at offercontroller...
The following query is created by guessing you have a relation maxoffer in Maxoffer model.
$maxoffer = Auth::user()->maxoffer()
->where('address_id', $request->input('address_id'))
->where('start', $request->input('start'))->first();
if($maxoffer==null)
{
Ath::user()->maxoffer()->create($request->all());
}
else
{
if($maxoffer->price < $request->input('price'))
{
$newOffer = Auth::user()->maxoffer()
->where('address_id', $request->input('address_id'))
->where('start', $request->input('start'))
->update(['price'=>$request->input('price')]);
}
}