第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將表連接到 laravel 中的表

如何將表連接到 laravel 中的表

PHP
陪伴而非守候 2024-01-19 10:05:53
我已經(jīng)處理這個(gè)問(wèn)題很長(zhǎng)時(shí)間了,但還沒(méi)有取得任何結(jié)果。所以我決定尋求你的幫助。我有 3 個(gè)表 =>文章、類(lèi)別和國(guó)家/地區(qū),我想連接這些表。每個(gè)類(lèi)別可以有多篇文章,但每篇文章僅與一個(gè)類(lèi)別相關(guān)。每個(gè)國(guó)家/地區(qū)可以有多篇文章,但每篇文章僅與一個(gè)國(guó)家/地區(qū)相關(guān)。問(wèn)題在于 ArticleController 部分,該部分僅適用于將一個(gè)表連接到文章,但將兩個(gè)表連接到它時(shí),我收到此錯(cuò)誤: SQLSTATE[HY000]: General error: 1364 Field 'country_id' does not have a default value and我還將country_id和category_id作為文章表中的外鍵。下面是我的表格:文章型號(hào):public function countries(){    return $this->belongsTo('App\country');}public function categories(){    return $this->belongsTo('App\category');}國(guó)家模式public function articles(){    return $this->hasMany('App\Article');}類(lèi)別模型public function articles(){    return $this->belongsToMany('App\Article');}ArticleController - 以及主要部分 = 問(wèn)題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 回答

?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2024-01-19
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

SQLSTATE[HY000]:一般錯(cuò)誤:1364 字段“country_id”沒(méi)有默認(rèn)值

上述錯(cuò)誤意味著每當(dāng)您將數(shù)據(jù)插入數(shù)據(jù)庫(kù)時(shí),您都沒(méi)有傳遞country_id的值和數(shù)據(jù)庫(kù)搜索默認(rèn)值,并且您沒(méi)有在數(shù)據(jù)庫(kù)表中為country_id分配默認(rèn)值。

試試這個(gè),我在保存文章之前已經(jīng)為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);


}


查看完整回答
反對(duì) 回復(fù) 2024-01-19
  • 2 回答
  • 0 關(guān)注
  • 141 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)