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