I'm having trouble creating the foreign key in migration.
I'm working with PHP, LARAVEL 5.3 and MYSQL. You are giving me the following error:
Below is my code:
Table Migration of the categs
class CreateCategsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categs', function (Blueprint $table) {
$table->increments('id');
$table->text('nome_cat');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categs');
}
}
Product Table Migration
class CreateProdutosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('produtos', function (Blueprint $table) {
$table->increments('id');
$table->text('nome');
$table->text('descricao');
$table->integer('categs_id');
$table->foreign('categs_id')->references('id')->on('categs');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('produtos');
}
}
Model categs
use Illuminate\Database\Eloquent\Model;
class categ extends Model
{
//Relacionamento 1:n Categoria dos produtos
public function produtos()
{
return $this->hasMany('App\produtos');
}
}
Model products
use Illuminate\Database\Eloquent\Model;
class Produtos extends Model
{
//
public function categoria()
{
return $this->belongsTo('App\categ');
}
}