如何将表连接到 laravel 中的表

我已经处理这个问题很长时间了,但还没有取得任何结果。所以我决定寻求你的帮助。我有 3 个表 =>文章、类别和国家/地区,我想连接这些表。每个类别可以有多篇文章,但每篇文章仅与一个类别相关。每个国家/地区可以有多篇文章,但每篇文章仅与一个国家/地区相关。问题在于 ArticleController 部分,该部分仅适用于将一个表连接到文章,但将两个表连接到它时,我收到此错误: SQLSTATE[HY000]: General error: 1364 Field 'country_id' does not have a default value and我还将country_id和category_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;

    }


如果有人提供帮助,我将非常感激。


陪伴而非守候
浏览 40回答 2
2回答

呼啦一阵风

您不需要加载整个模型来保存文章,您只需要一个 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的值和数据库搜索默认值,并且您没有在数据库表中为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);}
打开App,查看更多内容
随时随地看视频慕课网APP