Can*_*ike 2 php mysql sql-server database-migration laravel
你好我得到这个错误Illuminate\Database\QueryException与消息'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.user_id' in 'where clause' (SQL: select * from发布where帖子.user_id = 1 and发布.user_id is not null)'我不知道为什么如果在我的数据库中我没有user_id,我有id_user ...
这是我的迁移表
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('user')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->string('img');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
Run Code Online (Sandbox Code Playgroud)
另一个是我的帖子迁移归档
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddPosts extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('nombre');
$table->longText('contenido');
$table->unsignedInteger('id_user');
$table->timestamps();
});
Schema::table('posts', function($table) {
$table->foreign('id_user')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('posts');
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Post模型
<?php
namespace NacionGrita;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $table = "posts";
protected $fillable = ['nombre', 'contenido', 'id_user'];
public function imagenes() {
return $this->belongsToMany('NacionGrita\Imagen');
}
public function categorias() {
return $this->belongsToMany('NacionGrita\Categoria');
}
public function tags() {
return $this->belongsToMany('NacionGrita\Tag');
}
public function user() {
return $this->belongsTo('NacionGrita\User');
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的用户模型
<?php
namespace NacionGrita;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Model
{
protected $table = "users";
protected $fillable = [
'user', 'email', 'password', 'img'
];
public function posts() {
return $this->hasMany('NacionGrita\Post');
}
protected $hidden = [
'password', 'remember_token',
];
}
Run Code Online (Sandbox Code Playgroud)
如果我将我的"posts"表列从id_user更改为user_id它可以工作,但我不知道为什么我必须更改列名称,如果它应该有效,因为我指定了foreigns键或我做错了什么?
感谢帮助
为了指定外键,您需要在定义关系时在模型中执行此操作.
来自belongsTo关系的文档:
在上面的示例中,Eloquent将尝试
user_id将Phone模型中id的User模型与模型匹配.Eloquent通过检查关系方法的名称并使用方法名称后缀来确定默认外键名称_id.然而,如果在国外键Phone模式是不user_id,你可以通过自定义键名作为第二个参数的belongsTo方法
换句话说,在定义与User的关系的Post模型中,您需要添加指定外键名称的第二个参数:
public function user() {
return $this->belongsTo('NacionGrita\User', 'id_user');
}
Run Code Online (Sandbox Code Playgroud)
来自a hasOne和hasMany关系的文档:
Eloquent根据模型名称假定关系的外键.在这种情况下,
Phone模型自动被假定为具有user_id外键.如果要覆盖此约定,可以将第二个参数传递给该hasOne方法:
换句话说,在您定义与Post关系的User模型中,您需要再次添加指定外键名称的第二个参数:
public function posts() {
return $this->hasMany('NacionGrita\Post', 'id_user');
}
Run Code Online (Sandbox Code Playgroud)
链接到文档:https://laravel.com/docs/5.1/eloquent-relationships