2

I'm new to Laravel 5. I viewed the tutorial "Laravel 5 Fundamentals" and I have an issue.

Article model

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

Category model

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

ArticleController

public function create()
{
    $categories = Category::lists('name', 'id');

    return view('admin.article.create', compact('categories'));
}

public function store(ArticleRequest $request)
{
    $article = new Article($request->all());
    $category = Category::find($request->input('categories'));
    $article->category()->associate($category);
    \Auth::user()->article()->save($article);

    return \Redirect::to('/admin/article');
}

public function edit(Article $article)
{
    $categories = Category::lists('name', 'id');

    return view('admin.article.edit', compact('article', 'categories'));
}

public function update(Article $article, ArticleRequest $request)
{
    $article->update($request->all());

    return redirect('admin/article');
}

How can I update the Article model and the related Category model?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Ihor Tkachuk
  • 1,088
  • 4
  • 15
  • 34

1 Answers1

0

You can update like this: (original source: http://laravel.com/docs/5.0/eloquent#insert-update-delete)

public function update(Article $article, ArticleRequest $request)
{
    $newValues = $request->except('_method', '_token');
    foreach($newValues as $key => $value)
    {
         $article[$key] = $value;
    }
    $article->save();

    return redirect('admin/article');
}

or

public function update(Article $article, ArticleRequest $request)
{
    $article->title = $request->input('title');
    //all article's table columns = $request->input('corresponding_input_name')
    $article->save();

    return redirect('admin/article');
}

As sugguested by @patricus:

public function update(Article $article, ArticleRequest $request)
{
    $article->fill($request->all());
    $article->save();
    return redirect('admin/article');
}
Bruno Lebtag
  • 420
  • 4
  • 18
  • I gen an error: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: insert into `articles` (`_method`, `_token`, `title`, `slug`, `category_id`, `body`, `updated_at`, `created_at`) values (PATCH, ***, First article, first-article, 2, lorem ipsum 11, 2015-06-01 17:39:19, 2015-06-01 17:39:19)) – Ihor Tkachuk Jun 01 '15 at 14:40
  • exclude _method or _token like this: $request->except('_method', '_token'); I wrote a foreach expecting $request->all() contains all your table columns. Just grab from $request the columns you need and they need to have the same name. – Bruno Lebtag Jun 01 '15 at 14:45
  • @brlebtag You should use the `fill()` method on the model. This will take into account any `fillable` or `guarded` restrictions on the model, and also automatically ignores any parameters that start with an underscore (such as _token and _method). – patricus Jun 01 '15 at 16:36
  • @patricus thx I didn't know that. I have to take a look and read all Eloquent model's method. – Bruno Lebtag Jun 01 '15 at 17:10