samedi 27 février 2016

Laravel 5: Data does not saved through blade form

I want to save a form input through Laravel 5.0 The page reloaded and back to the index page..But data does not save to mysql through routes.

Here is controller:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Requests\CreateTaskRequest;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Task;
use Input;


class TodoController extends Controller {


    public function index()
    {
    $data=Task::all();
//  $options=Option::whereNotIn('id',$activeCourse)->lists('option_name','id');
        return view('home')->with('data',$data);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //return view('home');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(CreateTaskRequest $request)
    {
        // dd($request->all());

        /*$data=[
                'name'=>Input::get('name'),
                'status'=>Input::get('status'),
                'description'=>Input::get('description'),
                'task_img'=>Input::get('task_img'), 
                'dob'=>Input::get('dob')
             ];*/

                $response=new Task();
                $response->name=$request->input('name');
                $response->status=$request->input('status');
                $response->description=$request->input('description');
                $response->description=$request->input('task_img');
                $response->description=$request->input('dob');

             $response->save();
            // dd('working?');

             //$response=Task::create(['name'=>$request->input('name'),'status'=>$request->input('status'),'description'=>$request->input('description')]);
             if($response){
                return redirect()->back()->with('success','Task Created Successfully');
             }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $data=Task::find($id);
        return view('todo_edit')->with('data',$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(CreateTaskRequest $request, $id)
    {
        $data=[
                'name'=>Input::get('name'),
                'status'=>Input::get('status')
             ];

             $response=Task::find($id)->update($data);
             if($response){
                return redirect('/');
             }

    }


    public function destroy($id)
    {
            $response=Task::find($id)->delete();
            if($response){
                return redirect('/')->with('success','Task Deleted Successfully');;
            }   
    }



}

Here is my View Page:

@extends('app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12 ">
        <div class="col-lg-12"> 
            <div  class="col-lg-6">
                {!! Form::open(array('route' =>'' ,'method'=>'post')) !!}
                {!! Form::hidden('_token',csrf_token()) !!}
<!--            <form action="" method="POST" class="form-inline">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">        -->
                <div class="form-group">
                    <input type="text" name="name" class="form-control" placeholder="Enter a task name"></br>
                    </br>
                        <lavel> <input name="status" type="radio" value="Complete"> Complete</lavel></br>
                    <label> <input checked="checked" name="status" type="radio" value="Incomplete"> Incomplete</label></br>
                    <textarea class="form-control" name="description" rows="3"></textarea></br> 
                    <label>File input</label>
                          <input type="file" name="task_img"></br>
                    <input type="date" class="form-control datepicker" name="dob"  style="width:95%"> 

<!--                        <input type="submit" class="btn btn-primary" value="Save">      -->

                    {!! Form::submit('Save', array('class' => 'btn btn-primary')) !!}
                </div>

<!--            </form>   -->
            {!! Form::close() !!}
        </div>

            </div> 



            <h3> Todo Application </h3>
            <table  class="table table-striped table-bordered"  id="example">
                <thead>
                    <tr>
                    <td>Serial No</td>
                    <td>Task Name</td>
                    <td>Status</td>
                    <td>Action</td>
                </tr>
                </thead>
                <tbody>
                    <?php $i=1; ?>
                @foreach($data as $row)

                    <tr>
                        <td>{{$i}}</td>
                        <td>{{$row->name }}</td>
                        <td>{{$row->status}}</td>
                        <td>
                            <a href="{{route('getEditRoute',$row->id)}}" class="btn btn-warning">Edit</a>
                            <form action="{{route('postDeleteRoute',$row->id)}}" method="POST" style="display:inline;" 
                            onsubmit="if(confirm('Are you sure?')) {return true} else {return false};">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input type="submit" class="btn btn-danger" value="Delete">
                            </form>

                        </td>
                    </tr>
                <?php $i++; ?>

                @endforeach
                </tbody>


            </table>

        </div>
    </div>
</div>

@section('page-script')
 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
    $('#example').DataTable();
} );
</script>
@stop




@endsection

And here is the routes :

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/



Route::get('/',
 'TodoController@index');
Route::post('/', 'TodoController@store');

Route::get('/{id}/edit',['as'=>'getEditRoute','uses'=>'TodoController@edit']);
Route::post('/{id}/edit',['as'=>'postUpdateRoute','uses'=>'TodoController@update']);
Route::post('/{id}/delete',['as'=>'postDeleteRoute','uses'=>'TodoController@destroy']);

//Route::get('home', 'HomeController@index');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire