Laravel Insert data into one table and use the same id to fill in another

0

I'm starting in Laravel and I'm having a problem with inserting data into two mysql tables ... explaining better would be: I have a table called questionnaires where I have only ID and Description and another table called statements where I have the ID, a question of type string and another field called questionarios_id, and I have a form where I fill in the following fields: description of the questionnaire and question . What happens is that at the time I try to insert into the database when I get into the table statements the value questionarios_id is not filled occurring an error, I would like to know if it has how I get the id inserted in my table questionnaires and uses it to fill the field questionarios_id from my table

follow the codes:

model Questionnaire:     

namespace App;

use Illuminate\Database\Eloquent\Model;

class Questionario extends Model
{   
  public $timestamps = false;
  protected $fillable = array('descricao');

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

}

model statement:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Enunciado extends Model
{

  public $timestamps = false;
  protected $fillable = array('questao');

  public function questionario(){
    return $this->belongsTo('App\Questionario');
  }

}

My controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Questionario;
use App\Enunciado;
use App\Http\Requests\QuestionarioRequest;


class ControllerPrincipal extends Controller
{
    public function index(){
        return view('ViewFormulario');
}
public function enviar(QuestionarioRequest $request){

    Questionario::create($request->all());
    Enunciado::create($request->all());
    return redirect()->action('ControllerPrincipal@index');
}

}

The error that this code gives when sending the formualario is the following:

QueryException in Connection.php line 647:
SQLSTATE[HY000]: General error: 1364 Field 'questionarios_id' doesn't     have a default value (SQL: insert into 'enunciados' ('questao') values (teste))
    
asked by anonymous 21.02.2017 / 19:14

2 answers

3

The Create command can not bind, and the value of the foreign key (in this case questionario_id ) will not come by request, so I would not recommend using it in this case, but I will show 2 options, one using Create and another not, then you decide which one is best for you.

One comment, your question was about how to peruse the ID, the ID is injected into the object you just created.

With Create

With create you would need to inject the Questionnaire id you just created in Statement (as questionario_id ), this through a temporary variable which I'm calling $filler ):

public function enviar(QuestionarioRequest $request){
    $filler = $request->all();
    $questionario = Questionario::create($filler);

    $filler['questionario_id'] = $questionario->id;
    Enunciado::create($filler);

    return redirect()->action('ControllerPrincipal@index');
}

Without Create

Without Create, at least in the case of Enunciado , would be creating a new Object Enunciado , the result is very similar:

public function enviar(QuestionarioRequest $request){
    $questionario = Questionario::create($request->all());

    $enunciado = new Enunciado($request->all());
    $enunciado->questionario_id = $questionario->id;
    $enunciado->save();

    return redirect()->action('ControllerPrincipal@index');
}

With associate (without create)

Another case is using the associate, I would only change the id line From:

$enunciado->questionario_id = $questionario->id;

To:

$enunciado->questionario()->associate($questionario);
    
21.02.2017 / 19:41
-1

Man, I found this on a gringo website and solved my problem without having to deal with anything other than this command line. The best thing is that you will not need to change your database, created with so much sweat.

Find the config folder and open the database.php file. There, find the 'strict - > true in the database block that you use and change the value to false. BUM! The magic happens:)

    
31.05.2017 / 19:27