Tasmin's avatar

Data is not saved in database

I want to make a book shop management system. Now, to add new books details in the booklist I created a form. But the problem is when I press the submit button though it redirect me to the expected page but the data of the form is not saved to database.

this is my route:

Route::post('books', 'BooksController@store');

this is the table Schema::create('books', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('author'); $table->string('publisher'); $table->timestamp('year'); $table->integer('price'); $table->string('image_path'); $table->timestamp('added_at');

        $table->timestamps();
    });

this is the controller

public function store(){

    $book=Request::all();
    $book['added_at']=Carbon::now();
    Book::create($book);
    return redirect('books');
}

and the model is class Book extends Model { protected $fillable=[ 'title', 'author', 'publisher', 'year', 'price', 'image_path', 'added_at'

];

}

0 likes
13 replies
tomopongrac's avatar

Your code looks ok ... do you get any error?

Try with this

public function store(Request $request){

    $input=$request->all();
    $input['added_at']=Carbon::now();
    Book::create($input);
    return redirect('books');
}
Tasmin's avatar

your approach gives the following problem

Call to undefined method Illuminate\Support\Facades\Request::all()

JoolsMcFly's avatar

You need to include it at the top of your script:

use Illuminate\Support\Facades\Request;

What does your form look like? Are you posting to the right route?

katifrantz's avatar

It looks like your form has an image field. How do you process this before sending to the database ? Or is it already processed ? And I suggest you use the default created_at timestamp rather that using the added_at field. Also, check and make sure in your routes file, you do not have duplicate routes. Please check these things and tell us what you get . And also show us what your form looks like.

Tasmin's avatar

@tomi I changed "use Illuminate\Http\Request;" to "use Request". as it shows the following error Non-static method Illuminate\Http\Request::all() should not be called statically

@maitrefrantz still a have not processed it.

Tasmin's avatar

@JoolsMcFly yes I posted to right route.

{!! Form::open(['url'=>'books']) !!} Book's Name {{--{!! Form:: label ('name','Title') !!}}--}} {{--{!! Form::text('name',null,['class'=>'form_control']) !!}}--}} Author Publisher

     </div>
     <div class="col-xs-3">
         {!! Form::label('year', 'Publishing Year') !!}
         {!! Form::input('date','year',date("Y"), ['class'=>'form-control'] ) !!}

     </div>
     <div class="col-xs-7">
         <label for="image_path">Icon</label>
         <input class="form-control" id="image_path" type="text">
     </div>


     <div class="col-xs-3">
         <label for="price">Price</label>
         <input class="form-control" id="price" type="text">
     </div>
{!! Form::submit('Add',['class'=>'btn btn-primary form-control' ]) !!}
 </div>
     {!! Form::close() !!}
katifrantz's avatar

Oh ! I think you did not specify the method as post(in your form), so it just sends a get request to your "books" route

katifrantz's avatar

Just like in a normal form, you have to specify a method post :


{!! Form::open(['url' => 'books', 'method' => 'post']) !!}

But I really am not sure, because it is supposed to default to a post method. But try specifying the method as I just showed above . If this does not work, I would suggest using a normal form , It's much cleaner, and it's easier to see errors. Tell me what you get plz.

And also, what happened to ALL YOUR NAME ATTRIBUTES ? I don't see them.

katifrantz's avatar

Your fields do not have NAME ATTRIBUTES

<input type="text" name="image_path" id="image_path">

you need to give names to all your input fields . If not, it won't work.

Tasmin's avatar

I changed my form like,

{{--Book's Name--}} {{----}}

to

{!! Form:: label ('title','Title') !!} {!! Form::text('title',null,['class'=>'form_control']) !!}

now it worked. :\

Please or to participate in this conversation.