【问题标题】:Adding data into the database not working in laravel在laravel中将数据添加到数据库中不起作用
【发布时间】:2017-06-23 02:33:28
【问题描述】:

我正在使用 laravel 5.3 我创建了一个视图来创建帖子,并尝试使用 save 方法将数据添加到表中,但它给了我 SQL 错误。

SQL 错误: Connection.php 第 770 行中的 QueryException:SQLSTATE[HY000] [2002] 没有这样的文件或目录 (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))

查看/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::resource('posts','PostController');

app/http/Post.php(模型):

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);
}

我还在PostController.php 的命名空间之后的顶部添加了use App/Post,但代码在保存方法后不运行。此外,php artisan migrate 命令运行成功,并且在数据库中创建了表,但仍然面临保存问题。

【问题讨论】:

  • SQL 错误:Connection.php 第 770 行中的 QueryException:SQLSTATE[HY000] [2002] 没有这样的文件或目录(SQL:插入到poststitlebody,@987654334 @,created_at) 值 (测试, sdfhgfj, 2017-02-06 07:43:54, 2017-02-06 07:43:54))
  • 包含 Post 类的命名空间并检查数据库表 'posts' 是否存在。
  • @malutki5200 'posts' 表存在于数据库中,我还添加了'use App\Post;'在 PostController.php 中
  • 我们可以看看你的数据库配置吗?
  • @VaheGalstyan 谢谢我在 .env 以及 config/database.php 中将主机从“localhost”更改为“127.0.0.1”,然后重新启动服务器,服务器现在在“127.0.0.1:8000”上运行" 而不是 "localhost:8000"

标签: php laravel laravel-5.3


【解决方案1】:

我在 .env 以及 config/database.php 中将主机从“localhost”更改为“127.0.0.1”,然后重新启动服务器,服务器现在在“http://127.0.0.1:8000”而不是“http://localhost:8000”上运行

而且效果很好!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多