0

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.

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
vivek321
  • 169
  • 3
  • 18
  • 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)) – vivek321 Feb 06 '17 at 08:03
  • include namespace for class Post and check if the db table 'posts' exist. – malutki5200 Feb 06 '17 at 08:05
  • @malutki5200 table for 'posts' exists in database and I have also added 'use App\Post;' in PostController.php – vivek321 Feb 06 '17 at 08:13
  • Can we see your database configuration. – Ohgodwhy Feb 06 '17 at 08:22
  • DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=blog DB_USERNAME=root DB_PASSWORD= – vivek321 Feb 06 '17 at 08:35
  • Here is a solution http://stackoverflow.com/questions/20723803/pdoexception-sqlstatehy000-2002-no-such-file-or-directory – Vahe Galstyan Feb 06 '17 at 08:44
  • Whats the configuration for your `.env` file – Narendrasingh Sisodia Feb 06 '17 at 09:51
  • 1
    @VaheGalstyan thanks man I have changed host from "localhost" to "127.0.0.1" in .env as well as config/database.php and then restarted server and server now runs on "http://127.0.0.1:8000" instead of "http://localhost:8000" – vivek321 Feb 06 '17 at 10:46

1 Answers1

0

I have changed host from "localhost" to "127.0.0.1" in .env as well as config/database.php and then restarted server and server now runs on "http://127.0.0.1:8000" instead of "http://localhost:8000"

And it works well!!!

vivek321
  • 169
  • 3
  • 18