首页 文章

将数据添加到不在laravel中工作的数据库中

提问于
浏览
0

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

SQL错误:Connection.php第770行中的QueryException:SQLSTATE [HY000] [2002]没有这样的文件或目录(SQL:插入帖子(title,body,updated_at,created_at)值(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);
}

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

1 回答

  • 0

    我已将主机从"localhost"更改为.env中的"127.0.0.1"以及config / database.php然后重新启动服务器和服务器现在运行在“http://127.0.0.1:8000 " instead of " http://localhost:8000

    它运作良好!

相关问题