saeed doozandeh
saeed doozandeh

Reputation: 33

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.

Upvotes: 0

Views: 311

Answers (3)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value

in this error you can easily see country_id doesn't have a default

it means whenever your are inserting data to data base it is not passing any data and database searching default value

so you code your be

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

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

Upvotes: 0

Alok p
Alok p

Reputation: 629

SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value

The above error means whenever your are inserting data to database,you are not passing value of country_id and database searching default value and you have not assigned default value for country_id in db table.

Try this, i have assigned value to country_id and category_id before saving the article

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

}

Upvotes: 1

Vladan
Vladan

Reputation: 1622

You don't need to load the whole model to save an article, you just need an id, therefore:

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

And finally

$article->save();

Upvotes: 1

Related Questions