dimanche 20 septembre 2015

Save a model in Laravel 5

Believe it or not I'm having an extremely hard time figuring this out, I'm trying to save a model and I keep getting duplicate entry. I have a table with 2 columns id and data, id is not an auto increment column and is a primary key, here's my model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TestResult extends Model {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'results';
    protected $fillable = ['id', 'data'];
    protected $guarded = [];
    public $incrementing = false;
    public $timestamps = false;

}

Here's my controller

public function postIndex() {
    $id = ...

    $result = new TestResult(['id' => $id]);
    $result->results = Input::get("results");
    $result->save();

    return redirect()->action('TestController@success');
}

I tried just about everything to get this working with 1 query and I couldn't. I even tried building my own query using laravel's DB object and had an awfully unpleasant time. At this point I'm beginning to develop an attitude towards the framework since such a simple job is taking so long.

The only thing I could get to work is set $result->exists = true, which again needs 2 queries to determine if that is the case or not.

Can anyone please let me know how I can do this in one query. Much appreciation!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire