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

Asked

Viewed 4,266 times

0

I am starting in Laravel and I have a problem related to inserting data in two tables of mysql... explaining better would be the following: I have a table called questionnaires where I have only ID and Description and another table called statements where I have the ID, a string question and another field called questionarios_id, and I have a form where I fill in the following fields: Description of questioning and question. What happens is that at the time I try to insert in the database when arriving at the table enuncias the value questionarios_id is not filled in occuring an error, I wonder if I have how to get the id inserted in my table questionnaires and usesTo fill in the question field

follows the codes:

model Questionario:

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 Enunciation:

<?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 form is as follows:

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))

  • id is injected into the object you just created, you would only need to place it in a variable and use, e.g.: $q = Questionario::create(.... ; and then catch with the $q->id`, but to mount a more complete answer.

2 answers

4


The Create command cannot make associations, and hardly the value of the foreign key (in your case the questionario_id) will come by request, so I wouldn’t recommend using it in this case, but I’ll show you 2 options, one using Create and the other not, then you decide which one is best for you.

One remark, your question was about asking the ID, the ID is injected into the object you just created.

Com Create

With create you would need to inject the id of the Questioning that you have just created, in the Enunciation(as questionario_id), this by means of a temporary variable (which here I am 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');
}

Sem 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, would only change the id line
Of:

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

To:

$enunciado->questionario()->associate($questionario);
  • Pliavi worked, that’s right, I tested the method with create!

-2

Dude, I found this on a gringo site and solved my problem without having to touch anything but this command line. The best is that you won’t need to alter 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 you use and change the value to false. BUM! The magic happens :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.