SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'projects_id'

时间:2016-09-24 23:16:59

标签: php laravel

我正在尝试向数据库提交一些数据。但是当插入$ airattributes时会收到一个错误,即project_id(无论是在数据库中还是在任何文件中都存在)

MY CONTROLLER:

        public function newProject(Request $request)
    {
        $data = $request->all();

        $attributes = [];
        $attributes['title'] = $data['title'];
        $attributes['start_date'] = date("Y-m-d h:i:s", strtotime($data['start_date']));
        $attributes['end_date'] = date("Y-m-d h:i:s", strtotime($data['end_date']));
        $attributes['created_by'] = Auth::user()->id;
        $attributes['description'] = $data['description'];
        $attributes['air'] = '10';
        $attributes['water'] = '19';
        $attributes['lat'] = $data['lat'];
        $attributes['lng'] = $data['lng'];
        $airattributes['dust'] = $data['dust'];
        $airattributes['noise'] = $data['noise'];
        $airattributes['temperature'] = $data['temperature'];
        $airattributes['radiation'] = $data['radiation'];



  //        var_dump($attributes);
  //        return;

        $project = Projects::create($attributes);
        $air = $project->air()->create($airattributes);

        var_dump($data);
        return;

我的项目模型

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Projects extends Model
{
    protected $table = 'projects';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'start_date', 'end_date', 'created_by', 'description', 'air', 'water', 'lat', 'lng'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'id',
    ];

    /**
     * Get the phone record associated with the user.
     */
    public function air()
    {
        return $this->hasMany('App\Air');
    }

    /**
     * Get the phone record associated with the user.
     */
    public function water()
    {
        return $this->hasOne('App\Water');
    }

    public function user()
    {
        return $this->hasOne('\App\User', 'id', 'created_by');
    }

    public function enrolls()
    {
        return $this->hasMany('\App\Enroll', 'project_id', 'id');
    }


    public function lastEdited()
    {
        return $this->hasOne('\App\User', 'id', 'last_edited_by');
    }

}

我的空气模型

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Air extends Model
{
    protected $table = 'projects_air';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'project_id', 'temperature', 'radiation', 'dust', 'noise'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'id',
    ];
}

您可以在此处测试以获取更多信息:http://188.166.166.143/projects/add

3 个答案:

答案 0 :(得分:1)

在项目模型中,如果你没有指定外键,那么我相信Air模型你应该改为&#39; projects_id&#39;按照表的名称。

答案 1 :(得分:1)

项目与Air有很多关系:

public function air()
    {
        return $this->hasMany('App\Air');
    }

默认情况下,假设空气模型具有外键projects_id因此错误。

因为,你有project_id外键,

return $this->hasMany('App\Air', 'project_id'); 

会做的。

简单:将模型名称Projects更改为Project即可解决问题。模型名称也总是单数。

答案 2 :(得分:1)

您可以通过两种方式解决此问题。您可以将模型名称更改为Project,也可以在air功能中指定外键。所以你可以改变空气功能到下面。

public function air()
{
    return $this->hasMany('App\Air', 'project_id');
}

选择您想要的任何一种。但是在Laravel中,模型的名称总是单数的,所以我建议你按照第一条规则,不需要指定外键Laravel足够聪明,可以自动识别。