Reputation: 35
I have two tables, basic stuff, categories, and articles. Categories:
Articles:
Relationships
Category model:
public function articles()
{
return $this->hasMany('Article');
}
Articles model:
public function category()
{
return $this->belongsTo('Category');
}
edit and upadte methods in controller:
public function getEdit($id)
{
$categories = Category::get();
$articles = Article::with('category')->find($id);
return View::make('articles.edit', compact('articles', 'categories'), array('title' => 'Promena clanka'));
}
public function updateEdit($id)
{
$validation = Validator::make(Input::all(), Article::$rules);
if($validation->passes()) {
$article = Article::find($id);
$article->title = Input::get('title');
$article->slug = Slug::make(Input::get('title'));
$article->body = Input::get('body');
$article->category_id = Input::get('category_id');
$article->tag = Input::get('tag');
$article->visibility = Input::get('visibility');
$article->update();
return Redirect::route('dashboard')->with('message', 'Uspesno promenjen clanak');
}
return Redirect::route('dashboard/article/edit/' . $id )->withErrors($validation);
}
How can I dispay only checked checkbox that has category_id value. So far i get all checked.
@if(count($categories))
@foreach($categories as $category)
<div class="checkbox">
{{Form::checkbox('category_id', $category->id ) }} <span>{{Str::Title($category->name) }}</span>
</div>
@endforeach
@endif
How should I solve this?
Upvotes: 0
Views: 2818
Reputation: 1960
The Form::checkbox
takes an optional third parameter to set the checkbox to checked. You could use isset
on $category->id
which will return a boolean (true if $category->id
is set and not null).
So within your loop you could do something like this:
{{Form::checkbox('category_id', $category->id, isset($category->id)}}
Upvotes: 2