I am using laravel 5.3 I have created a view to create posts and am trying to add data to the table with save method but it is giving me SQL error.
SQL Error: QueryException in Connection.php line 770: SQLSTATE[HY000] [2002] No such file or directory
(SQL: insert into posts (title, body, updated_at, created_at) values (Test, sdfhgfj, 2017-02-06 07:43:54, 2017-02-06 07:43:54))
view/posts.create.blade.php:
<div class="row">
<div class="col-md-8 offset-2">
<h1>Create New Post</h1>
<hr>
{!! Form::open(['route' => 'posts.store']) !!}
<div class="form-group">
{{ Form::label('title', 'Title:', ['class' => 'control-label']) }}
{{ Form::text('title', null, ['class' => 'form-control']) }}
{{ Form::label('body', 'Body:', ['class' => 'control-label']) }}
{{ Form::textarea('body', null, ['class' => 'form-control']) }}
</div>
{{Form::submit('Create',array('class'=>'form-submit btn btn-success btn-block btn-lg'))}}
{!! Form::close() !!}
</div>
</div>
web.php(route file):
Route::resource('posts','PostController');
app/http/Post.php(Model):
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
PostController.php
public function store(Request $request)
{
//validate
$this->validate($request,array(
'title'=>'required|max:255',
'body'=>'required'
));
//Store
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
//redirect
return redirect()->route('posts.show', $post->id);
}
I have also added use App/Post
, in top after namespaces in PostController.php
but code does not runs after save method. Also, php artisan migrate
command runs successfully and tables are created in database, but still facing issue with save.