繁体   English   中英

如何将表连接到 Laravel 中的表

[英]how to connect to tables to a table in laravel

我一直在处理这个问题已经很长时间了,但我还没有取得任何结果。 所以我决定寻求你的帮助。 我有 3 个表 =>文章、类别和国家,我想连接这些表。 每个类别可以有多篇文章,但每篇文章只与一个类别相关。 每个国家可以有多篇文章,但每篇文章只与一个国家相关。 问题出在 ArticleController 部分,它仅用于将一个表连接到文章,但为了将两个表都连接到它,我收到此错误: SQLSTATE[HY000]:一般错误:1364 字段 'country_id' 没有默认值,并且我也有country_idcategory_id作为我在文章表中的外键。 以下是我的表格:

物品型号:

public function countries(){
    return $this->belongsTo('App\country');
}

public function categories(){
    return $this->belongsTo('App\category');
}

国家模式

public function articles(){
    return $this->hasMany('App\Article');
}

类别模型

public function articles(){
    return $this->belongsToMany('App\Article');
}

ArticleController - 和主要部分 = 问题

public function store(Request $request)
{
    $article = new article(
        [
            'title' => $request->input('title'),
            'top_content' => $request->input('top_content'),
            'quote' => $request->input('quote'),
            'left_content' => $request->input('left_content'),
            'right_content' => $request->input('right_content'),
        ]
    );
    if ($request->hasFile('article_slider_image')) {
        $file = time() . '_' . $request->file('article_slider_image')->getClientOriginalName();
        $destination = base_path() . '/public/images/articleSliderImages';
        $request->file('article_slider_image')->move($destination, $file);
        $article->article_slider_image = $file;
    }
    if ($request->hasFile('left_image')) {
        $file = time() . '_' . $request->file('left_image')->getClientOriginalName();
        $destination = base_path() . '/public/images/articleLeftImages';
        $request->file('left_image')->move($destination, $file);
        $article->left_image = $file;
    }
    if ($request->hasFile('right_image')) {
        $file = time() . '_' . $request->file('right_image')->getClientOriginalName();
        $destination = base_path() . '/public/images/articleRightImages';
        $request->file('right_image')->move($destination, $file);
        $article->right_image = $file;
    }
    $country = country::where('name',$request->input('country'))->first();
    $category = category::where('name',$request->input('category'))->first();

  //$article->category_id = 1; //not commenting this part works fine
    $country->articles()->save($article);
    $category->articles()->save($article);// but when I use this one, it gives me error
  //dd($category->id);

    $article->save();
    return redirect()->route('article.index')->with('success', 'article created successfully' . $request->title);

}

如果有人帮助,我将不胜感激。

SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值

上述错误意味着每当您将数据插入数据库时​​,您没有传递 country_id 的值和数据库搜索默认值,并且您没有为 db 表中的 country_id 分配默认值。

试试这个,我在保存文章之前已经为 country_id 和 category_id 赋值了

public function store(Request $request)
{
    $article = new article(
        [
            'title' => $request->input('title'),
            'top_content' => $request->input('top_content'),
            'quote' => $request->input('quote'),
            'left_content' => $request->input('left_content'),
            'right_content' => $request->input('right_content'),
        ]
    );
    if ($request->hasFile('article_slider_image')) {
        $file = time() . '_' . $request->file('article_slider_image')->getClientOriginalName();
        $destination = base_path() . '/public/images/articleSliderImages';
        $request->file('article_slider_image')->move($destination, $file);
        $article->article_slider_image = $file;
    }
    if ($request->hasFile('left_image')) {
        $file = time() . '_' . $request->file('left_image')->getClientOriginalName();
        $destination = base_path() . '/public/images/articleLeftImages';
        $request->file('left_image')->move($destination, $file);
        $article->left_image = $file;
    }
    if ($request->hasFile('right_image')) {
        $file = time() . '_' . $request->file('right_image')->getClientOriginalName();
        $destination = base_path() . '/public/images/articleRightImages';
        $request->file('right_image')->move($destination, $file);
        $article->right_image = $file;
    }
    $country = country::where('name',$request->input('country'))->first();
    $category = category::where('name',$request->input('category'))->first();

    $article->country_id = $country->id;
    $article->category_id = $category->id;

    $article->save();

    return redirect()->route('article.index')->with('success', 'article created successfully' . $request->title);

}

你不需要加载整个模型来保存一篇文章,你只需要一个 id,因此:

$article->country_id = country::where('name',$request->input('country'))->pluck('id')->first();
$article->category_id = category::where('name',$request->input('category'))->pluck('id')->first();

最后

$article->save();

SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值

在此错误中,您可以轻松看到country_id doesn't have a default

这意味着每当您将数据插入数据库时​​,它都不会传递任何数据和database搜索默认值

所以你编码你的

if(!$country){
   return redirect()->route('article.index')->with('error', 'country not found.');
}

$article->country_id = $country->id;
$article->category_id = $category->id;
$article->save();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM