jeudi 3 septembre 2015

Store query in laravel 5

I want to create new task in the list. I have url "lists/1/newTask".

Have Tasks.php (model)

    class Tasks extends Model {

        protected $fillable=[
            'description'
        ];

        public function lists()
        {
            return $this->belongsTo('App\Lists');
        }
    }

Lists.php (model)

class Lists extends Model {

    protected $fillable=[
        'name'
    ];


    public function tasks()
    {
        return $this->hasMany('App\Tasks');
    }
}

tasks table

public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
        $table->increments('id');
        $table->string('description');
        $table->integer('list_id')->unsigned();
        $table->timestamps();

        $table->foreign('list_id')
              ->references('id')
              ->on('lists')
              ->onDelete('cascade');

    });
}

So now I tryed to store all data from form and create task, but I actually don't know how to use relationship and how to store NewTask with list_id = $id (from url)

I tryed manualy store id in list_id like this, but it's not working

  public function store(CreateTasksRequest $request, $id)
    {

        $listID = Lists::findorfail($id);

        DB::table('tasks')->insert([
            ['description' => $request, 'list_id' => $listID]
        ]);


    }

Im new in this, so for sure I do something wrong, also it would be great that storing data could work automaticaly, not manualy.

My route

Route::get('lists/{id}/newTask', 'TasksController@create');
Route::post('lists/{id}', 'TasksController@store');



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1KLeMDx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire