Laravel Migration issues – Foreign Key not able to be assigned.

1. First issue, not using INNODB (using MYISAM by default). To fix this, each table will need to have this line

Schema::create(‘branches’, function (Blueprint $table) {

$table->engine = ‘InnoDB’;

 

2. MySQL Error 1215: Cannot add foreign key constraint

To debug this, run the following SQL against the DB.

SHOW ENGINE INNODB STATUS

————————
LATEST FOREIGN KEY ERROR
————————
2017-10-01 19:01:01 0x29fc Error in foreign key constraint of table amanah/#sql-b98_157:
foreign key (`brnch_id`) references `branches` (`brnch_id`) on delete cascade:
Cannot resolve table name close to:
(`brnch_id`) on delete cascade

 

This usually means the system was not able to find the TABLE mentioned .

Also this would mean the table is NOT YET BUILT when the assignment of Foreign key is done.

To fix this issue: Check the date and time of each migration file (for each table). The table referenced to have the foreign key MUST BE BUILT BEFORE. Therefore the filenames MUST BE IN ORDER.

 

Migrating: 2017_09_23_000000_create_users_table
Migrated: 2017_09_23_000000_create_users_table
Migrating: 2017_09_23_100000_create_password_resets_table
Migrated: 2017_09_23_100000_create_password_resets_table
Migrating: 2017_10_01_141720_create_branches_table
Migrated: 2017_10_01_141720_create_branches_table
Migrating: 2017_10_01_151153_create_mettingtypes_table
Migrated: 2017_10_01_151153_create_mettingtypes_table
Migrating: 2017_10_01_215348_create_reports_table
Migrated: 2017_10_01_215348_create_reports_table
Migrating: 2017_10_02_215258_create_meetings_table
Migrated: 2017_10_02_215258_create_meetings_table

 

Once that is fixed, MIGRATION TABLE NEEDS TO BE TRUNCATED (so that it will contain the updated filenames).

$table->integer(‘brnch_id’)->unsigned();
$table->foreign(‘mtg_apprvl_user’)->references(‘user_id’)->on(‘users’)->onDelete(‘cascade’);
$table->foreign(‘brnch_id’)->references(‘brnch_id’)->on(‘branches’)->onDelete(‘cascade’);
$table->foreign(‘mtg_type_descr’)->references(‘mtg_type_descr’)->on(‘meetingtypes’)->onDelete(‘cascade’);

 

The code should  be as above.

 

The foreign key must be of the same type (unsigned integer) and length (10) – else it wont work.

 

Also, the foreign key (in the original/main table) must be added as INDEXED, or PRIMARY.

 

 

Laravel – column declaration $table->increments(‘loc_id’,true, false);

If you found something like this, this is an alternative way to declare $autoIncrement = false, $unsigned = false

$table->increments('type_id',true, false);
$table->unsignedInteger('location_id', false, false);

As found in the following file:

C:\wamp\www\website\vendor\laravel\framework\src\Illuminate\Database\Schema\Blueprint.php

    /**
     * Create a new small integer (2-byte) column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Support\Fluent
     */
    public function smallInteger($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('smallInteger', $column, compact('autoIncrement', 'unsigned'));
    }

 

Laravel: issues encountered (1)

LOCALHOST

this is a headache – mainly because using localhost will cause the FRONT FACING path (public directory) is not the ROOT directory

For example:

localhost/myblog/public/

Path: C:\wamp\www\myblog\public

This pose a problem when routing within the website

Comment:

<form method="POST" action="/posts/{{ $postid->post_id }}/comments">
Route::post('/posts/{postid}/comments', 'CommentsController@Store')->name('storeComment');

RESULTS:
<form method="POST" action="myblog/public/posts/{{ $postid->post_id }}/comments/">
http://localhost/posts/myblog/public/posts/2/comments

<form method="POST" action="/myblog/public/posts/{{ $postid->post_id }}/comments/">
http://localhost/posts/2/comments

So we have to setup a local domain

myblog.lists.this

C:\Windows\System32\drivers\etc

127.0.0.1 myblog.lists.this              # x client host

And APACHE VHOSTS

#

<VirtualHost *:80>
	ServerName myblog.lists.this
	ServerAlias myblog.lists.this
	DocumentRoot C:/wamp/www/myblog/public
	<Directory  "c:/wamp/www/myblog/public">
		Options +Indexes +Includes +FollowSymLinks +MultiViews
		AllowOverride All
		Require local
	</Directory>

 

Laravel App Model codes

This is the codes for the model

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Post;
use Carbon\Carbon; //for timestampe

class PostController extends Controller
{
    public function Index() {

    return view('posts.index');

  }


    public function Store() {

      //
      // $current_time = Carbon::now()->toDateTimeString();
      // // echo $date->getTimestamp();
      // // $post = new App\Post; //Class 'App\Http\Controllers\App\Post' not found
      // $post = new \App\Post;
      //
      // $post->title = request('thisTitle');
      // $post->content = request('thisContent');
      // $post->created_at = $current_time;
      // $post->save();

      // OR

      Post::create( request(  ['title','content'] ) );

    // dd(request()->all() );
    // dd(request('thisTitle') );
    // dd(request(['thisTitle','thisContent']) );
    // return redirect ('/myblog/public/posts'); //http://localhost/myblog/public/myblog/public/posts
    return redirect ('/posts');
  }

}

 

Laravel Database Connection Config

it is in .env file

in C:\wamp\www\myblog

(the root of the site created by Laravel)

 

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel02
DB_USERNAME=root
DB_PASSWORD=

WAMP physical PATH vs Laravel PATH

WAMP:

Lets say the following is the path to WAMP.

C:/wamp/www

www/<something>

is the path to localhost/<something>

To make something accessible on WAMP, use something like this

Path on WAMP: C:\wamp\www\libs\jquery-3.2.1.min.js
<script src=”/libs/jquery-3.2.1.min.js”></script>

LARAVEL:

Path to the project (called “blog2”):

C:\wamp\www\blog2\

Laravel will always reference the public folder in the project, therefore, put the css in the public directory (it is there by default)

C:\wamp\www\blog2\public\css

And access it in the view script this way

<link href=”css/bootstrap.min.css” rel=”stylesheet”>

which Laravel will translate into: http://localhost/blog2/public/css/bootstrap.min.css

LARAVEL: Creating route for CRUD purpose (make:controller -r )

C:\wamp\www\blog2>php artisan make:controller TasksController -r
Controller created successfully.

C:\wamp\www\blog2>

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TasksController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

Laravel: Creating DB Table definition (make:migration)

C:\wamp\www\blog2>php artisan make:migration create_meetings_table –create=meetings
Created Migration: 2017_03_26_062354_create_meetings_table

C:\wamp\www\blog2>

It will create the following file:

C:\wamp\www\blog2\database\migrations\2017_03_26_062354_create_meetings_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMeetingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(‘meetings’, function (Blueprint $table) {
$table->increments(‘id’);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists(‘meetings’);
}
}

 

 

 
C:\wamp\www>cd blog2

C:\wamp\www\blog2>php artisan make:migration create_mtgdelegates_table -create=mtgdelegates
[Symfony\Component\Console\Exception\RuntimeException]
Too many arguments, expected arguments “command” “name”.

C:\wamp\www\blog2>php artisan make:migration create_mtgdelegates_table -create=mtgdelegates

C:\wamp\www\blog2>php artisan make:migration create_mtgdelegates_table -create=mtgdelegates
[Symfony\Component\Console\Exception\RuntimeException]
The “-c” option does not exist.

C:\wamp\www\blog2>php artisan make:migration create_mtgdelegates_table –create=mtgdelegates
Created Migration: 2017_03_30_095555_create_mtgdelegates_table

C:\wamp\www\blog2>composer dump-autoload
Generating autoload files

C:\wamp\www\blog2>composer dumpautoload
Generating autoload files

C:\wamp\www\blog2>

Laravel Journey (003) Installation (OpenSSL issue)

Copy the folder created by the commandline

 

You might encounter this issue

-and this is due to the config on PHP.ini file.

there are 2 PHP.ini file.
1. Wamp

C:\wamp\bin\php\php7.0.10

2. Apache.

C:\wamp\bin\apache\apache2.4.23\bin

(in windows this Apache file is a linked filed but you can edit it from here – open in notepad or similar and edit it).

There is this line in the file that is commented out, which you need to remove the comment , so that the config will be used

extension=php_openssl.dll