亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將表連接到 laravel 中的表

如何將表連接到 laravel 中的表

PHP
陪伴而非守候 2024-01-19 10:05:53
我已經處理這個問題很長時間了,但還沒有取得任何結果。所以我決定尋求你的幫助。我有 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;    }如果有人提供幫助,我將非常感激。
查看完整描述

2 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

您不需要加載整個模型來保存文章,您只需要一個 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();


查看完整回答
反對 回復 2024-01-19
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

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


}


查看完整回答
反對 回復 2024-01-19
  • 2 回答
  • 0 關注
  • 151 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號