Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShane McC
3,005 PointsUser input isn't being saved into database?
Hi Everyone,
I'm attempting to save user search phrases into my mysql database. I'm having a difficult time saving the search phrases in database. I'm not sure what I'm doing wrong. My form, model, controller and database schema is below. What am I doing wrong? Any help would be appreciated it. Thanks
My Syntax http://laravel.io/bin/Jxz27
{{ Form::open(array('action' =>('ProductController@postUseSearch'))) }}
{{ Form::text('keyword', null, array('placeholder' => 'search by keyword')) }}
{{ Form::submit('search') }}
{{ Form::close() }}
Route::get('/search/', array(
'as' => 'name-search',
'uses' => 'ProductController@showSearchPage'
));
Route::post('/search/', array(
'as' => 'name-search-post',
'uses' => 'ProductController@postUseSearch'
));
public function postUseSearch(){
$searchString = Input::get('keyword');
$insert = new Searchword();
$insert->keyword = $searchString; // not saving into the database?
more code...
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Searchword extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable = array (
'keyword'
);
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'searchword';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array();
}
Database Schema
id (int)
keyword (varchar 255)
updated_at (datetime)
created_at (datetime)
1 Answer
shezazr
8,275 PointsTry adding this:
$insert->save()
after
$insert->keyword
because you need to explicitly tell it to save
Shane McC
3,005 PointsShane McC
3,005 PointsWow, can't believe I forgot the save method. Thanks shez azr