0

Project is on Laravel 5.3, I am collecting data in session for multiple pages and I want to add it to MySQL. The problem is that data doesn't inserted into MySQl only created_at. Data is in variable, but it 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');
}
1
  • do you have use Illuminate\Database\Eloquent\Model; at the top of your model? Commented Nov 8, 2016 at 9:48

4 Answers 4

2

I would like you to recommend to use Laravel 5.3 official syntax only.

As you mentioned you are using Laravel 5.3.

Please try following code.

To debug the value of variables use "dd($variable_name)";

Note: Make sure you have changed access modifier of "addToDb" to protected in order access it 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 your 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 to debug the 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 your controller class declartions and modify the "use App\Post; " with you 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 your model here

Modify Controller method:

public function getAciu (Request $request) {
      //dd($request->all()); //uncomment to 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 you are using laravel 5.3.

  • In laravel 5.3 $request->input('field_name') used instead of Input::get('field_name');

  • And also session can be get using $request->session()->get('key_name');

Please check for ref: https://laravel.com/docs/5.3/requests

Hope above code will work.

Sign up to request clarification or add additional context in comments.

3 Comments

There's no need to define that addToDb method in your model for saving the data in to the database as you can use a built-in static create method to create and save the data . In your case: Post::create($data);
Yes you right @B_CooperA. But if you want to create object of model. after saving then you can use $answer->save(); otherwise you can use Post::create($data);. But you have to create array for create meathod e.g $data should be array.
Yeah, totally. Good you point out that the $data variable should indeed be an array if using the create method. I think this should be the correct answer.
0

Try this first

try{
   $answer->save();
 }
catch(\Exception $e){
   var_dump($e->getMessage());
 }

Comments

0

If it's post/put method and since you're using $fillable, you can do this:

public function getAciu (Request $request) {
    $request->q1 = Session::get('q1');
    $request->q2 = Session::get('q2');
    $request->q3 = Session::get('q3');
    $request->q4 = Session::get('q4');
    Post::create($request()->all());

  return view('aciu');
}

Comments

0

You should remove the line:

public $vardas, $pavarde, $miestas, $amzius, $telefonas, $pastas, $q1, $q2, $q3, $q4;

And try to use something else, like adding phpdoc syntax instead:

/**
* Class Post
* @package App
* @property int $id
* @property string $vardas
* //...
*/
Class Post extends Model { //...

Why? Since you declare a property for the class, when you're trying to set it to your model, it will save that value into that property(instance variable) and not invoke __set() (which Model class use it to save your data into attributes array, which is going to be used when you're trying save it to the DB.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.