php - Laravel add data to MySQL -
project on laravel 5.3, collecting data in session multiple pages , want add mysql. problem data doesn't inserted mysql created_at. data in variable, doesn't go saved in mysql.
model:
class post extends model { public $vardas, $pavarde, $miestas, $amzius, $telefonas, $pastas, $q1, $q2, $q3, $q4; protected $fillable = ['vardas', 'pavarde', 'miestas', 'amzius', 'telefonas', 'pastas', 'q1', 'q2', 'q3', 'q4']; public function addtodb($vardas, $pavarde, $miestas, $amzius, $telefonas, $pastas, $q1, $q2, $q3, $q4) { $answer = new post; $answer->vardas = $vardas; $answer->pavarde = $pavarde; $answer->miestas = $miestas; $answer->amzius = $amzius; $answer->telefonas = $telefonas; $answer->pastas = $pastas; $answer->q1 = $q1; $answer->q2 = $q2; $answer->q3 = $q3; $answer->q4 = $q4; $answer->save(); } }
database:
schema::create('answers', function (blueprint $table) { $table->increments('id'); $table->string('vardas'); $table->string('pavarde')->nullable(); $table->string('miestas')->nullable(); $table->string('amzius')->nullable(); $table->string('telefonas')->nullable(); $table->string('pastas')->nullable(); $table->string('q1')->nullable(); $table->string('q2')->nullable(); $table->string('q3')->nullable(); $table->string('q4')->nullable(); $table->timestamps(); });
and controller:
public function getaciu () { $vardas = input::get('vardas'); $pavarde = input::get('pavarde'); $miestas = input::get('miestas'); $amzius = input::get('amzius'); $telefonas = input::get('telefonas'); $pastas = input::get('pastas'); $q1 = session::get('q1'); $q2 = session::get('q2'); $q3 = session::get('q3'); $q4 = session::get('q4'); $answer = new post; $answer->addtodb($vardas, $pavarde, $miestas, $amzius, $telefonas, $pastas, $q1, $q2, $q3, $q4); return view('aciu'); }
i recommend use laravel 5.3 official syntax only.
as mentioned using laravel 5.3.
please try following code.
to debug value of variables use "dd($variable_name)";
note: make sure have changed access modifier of "addtodb" protected in order access on controller without int. class object e.g: $result = post::addtodb($vardas, $pavarde, $miestas, $amzius, $telefonas, $pastas, $q1, $q2, $q3, $q4);.
model: use "use illuminate\database\eloquent\model;" before declaring model class
eg:
namespace app; use illuminate\database\eloquent\model; class post extends model { protected $fillable = ['vardas', 'pavarde', 'miestas', 'amzius', 'telefonas', 'pastas', 'q1', 'q2', 'q3', 'q4']; protected function addtodb($vardas, $pavarde, $miestas, $amzius, $telefonas, $pastas, $q1, $q2, $q3, $q4) { //dd($vardas); uncomment debug value of $vardas $answer = new post(); $answer->vardas = $vardas; $answer->pavarde = $pavarde; $answer->miestas = $miestas; $answer->amzius = $amzius; $answer->telefonas = $telefonas; $answer->pastas = $pastas; $answer->q1 = $q1; $answer->q2 = $q2; $answer->q3 = $q3; $answer->q4 = $q4; return $answer->save(); } }
controller:
please use following line before controller class declartions , modify "use app\post; " post model namespace.
e.g:
namespace app\http\controllers; use illuminate\http\request; use app\http\controllers\controller; use app\post; // please specify namespace of model here
modify controller method:
public function getaciu (request $request) { //dd($request->all()); //uncomment debug complete request body $vardas = $request -> input('vardas'); $pavarde = $request -> input('pavarde'); $miestas = $request -> input('miestas'); $amzius = $request -> input('amzius'); $telefonas = $request -> input('telefonas'); $pastas = $request -> input('pastas'); $q1 = $request -> session() - > get('q1'); $q2 = $request -> session() - > get('q2'); $q3 = $request -> session() - > ('q3'); $q4 = $request -> session() - > ('q4'); $result = post::addtodb($vardas, $pavarde, $miestas, $amzius, $telefonas, $pastas, $q1, $q2, $q3, $q4); return view('aciu'); }
as using laravel 5.3.
in laravel 5.3 $request->input('field_name') used instead of input::get('field_name');
- and session can using $request->session()->get('key_name');
please check ref: https://laravel.com/docs/5.3/requests
hope above code work.
Comments
Post a Comment