【问题标题】:SQLSTATE[HY000] When trying to select and pass a value from a dropdownSQLSTATE[HY000] 尝试从下拉列表中选择和传递值时
【发布时间】:2020-03-05 22:43:37
【问题描述】:

我正在尝试在我的应用程序中添加一个选择框,以便用户选择一个类别来分配给他的帖子。

我的数据库中的 Categories 和 Post 表是分开的,Posts 中有一个 category_id 列,它是 Categories 表的外键。

我的帖子创建刀片是:

<div class="container">
    <form method="POST" action="/posts">
        {{csrf_field()}}
        <div>
            <label>Title</label>
            <input type="text" name="title" required>
        </div>
        <div>
            <label>Slug</label>
            <input type="text" name="slug" required>
        </div>
        <div>
            <label>Subtitle</label>
            <input type="text" name="subtitle" required>
        </div>
        <div>
            <label>Content</label>
            <input type="text" name="content" required>
        </div>
        <div class="form-group">
            <select class="form-control" name="categories_id">
                @foreach($categories as $category)
                    <option value="{{$category->title}}">{{$category->title}}</option>
                @endforeach
            </select>
        </div>
        <div  style="margin-top: 10px">
            <input type="submit" value="Make Post">
        </div>
    </form>
</div>

我的帖子控制器上的存储功能是:

public function store(Request $request)
{
    $post = new Post();

    $post->title = request('title');
    $post->slug = request('slug');
    $post->subtitle = request('subtitle');
    $post->content = request('content');
    $post->save();

    return redirect('/posts');
}

创建函数是

public function create()
{
    $categories = Category::all(['id','title']);
    return view('posts.create',compact('categories',$categories));
}

最后是我的模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public $table = "posts";
}

目前,当我尝试选择一个选项时,我得到一个

SQLSTATE[HY000]:一般错误:1364 字段 'category_id' 没有默认值。

我不希望它为空。并且每个帖子至少应与 1 个类别相关联

【问题讨论】:

  • 我认为您在保存帖子时没有“保存”categories_id 值($post->save())。不应该是 $post->categories_id = $request->input('categories_id') 吗?

标签: php laravel laravel-5.8


【解决方案1】:

在您的 select 中,使用类别 ID 而不是标题作为值。

<select class="form-control" name="category_id">
    @foreach($categories as $category)
        <option value="{{ $category->id }}">{{ $category->title }}</option>
    @endforeach
</select>

然后,在您的 store() 方法中,将该值传递给帖子 create() 方法(或者您可以使用 $post = new Post() 并手动分配它们,它也可以工作,但这有点短)。

这假设用户选择了一个类别 - 您似乎没有任何验证,因此您或许可以考虑使用验证器(请参阅 https://laravel.com/docs/6.x/validation)。

public function store(Request $request)
{
    $post = Post::create($request->only("title", "slug", "subtitle", "content", "category_id"));

    return redirect('/posts');
}

使用create()update() 方法时,您需要指定哪些字段可批量赋值。您可以通过将它们添加到模型的 $fillable 属性中来做到这一点。

class Post extends Model
{
    public $table = "posts";

    protected $fillable = ["title", "slug", "subtitle", "content", "category_id"];
}

【讨论】:

  • 当我按照你说的做时,我得到 Add [title] to fillable property 以允许在 [App\Post] 上进行批量分配。
  • 检查Post 模型中的$fillable 属性。它应该类似于protected $fillable = ["title", "slug", "subtitle", "content", "category_id"];
  • 我已经按照你说的更新了我的选择。并完成所有其他步骤。当我添加可填充时,我得到了同样的错误。字段 category_id 没有默认值
  • 那么您没有更新您的&lt;select&gt;store() 方法和/或name 属性。
  • 它还活着。非常非常感谢你!现在我必须找到一种方法让它接受多个类别。但你和@minatula 一起救了我的命。再次感谢您!
【解决方案2】:

store()方法中save()之前需要添加

$post->content = request('categories_id');

在新帖子中添加类别 ID。而且您需要确保该用户会选择类别

【讨论】:

  • @Qirel 是对的,您需要使用类别的 ID 而不是标题作为值
  • 我也试过你的方法。当我按 make post 时,我被重定向到索引页面,但未显示帖子。我的错。我忘了保存。但是当我按照你说的做时,我得到了默认值问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多