vendredi 11 septembre 2020

how to connect to tables to a table in laravel

It is so long that I have been dealing with this but no result have I achieved yet. So I decided to ask for your help. I have 3 tables => article, category and country and I want to connect these tables. Each category can have multiple articles but each article is related to only one category. Each country can have multiple articles but each article is related to only one country. The problem is with the ArticleController part which works for connecting only one table to article but for connecting both tables to it, I receive this error: SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value and also I have country_id and category_id as my foreign keys in articles table. Below are my tables:

article model:

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

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

country model

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

category model

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

ArticleController - and the main part = the problem

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

}

I will really appreciate if someone helps.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire