0
votes

I am trying to submit some data to database. However when inserting the $airattributes im getting an error that projects_id (WHICH DOESNT EXIST ANYWHERE, NEITHER IN DB NOR ANY FILE)

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;

MY projects model

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

}

My Air Model

    <?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',
    ];
}

you can test it here for more info: http://188.166.166.143/projects/add

3
project_id is in your Air Model file: in $fillable = ['project_id',....] - Paul Coldrey
yes thats ok. But im getting a message that projects_id has not been found. projectS <---- with an s - dailyadd

3 Answers

1
votes

In the projects model if you don't specify the foreign key, then I believe in the Air model you should change to 'projects_id' in accordance to the name of the table.

1
votes

Projects hasMany relationships with Air:

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

This, by default assumes that air model have foreign key projects_id, hence the error.

Since, you have project_id foreign key,

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

will do.

Simply: Changing model name Projects to Project will solve the problem. Also model names are always singular.

1
votes

You can solve this in two way. One you can change you model name to Project or you can specify you foreign key in your air function. So you can Change you air function to below.

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

Choose whichever you want. But in Laravel Model name is always singular so I will recommend you to follow the first rule in that way you don't need to specify the foreign key Laravel is smart enough to recognize that automatically.