working with Laravel and I have both Topic and Product Tables. Product table contains relate to the Topic.
Topic.php
class Topic extends Model
{
public function products()
{
return $this->hasMany('App\Product');
}
}
Product.php
class Product extends Model
{
public function Topic()
{
return $this->belongsTo('App\Topic');
}
}
and I have the following controller store function in ProductController
public function store(Request $request)
{
$this->validate($request, array(
'name'=> 'required|max:225',
'menu'=> 'required|max:225',
'slug' => 'required|max:225',
'description'=> 'required|max:225',
));
$product = new Product;
$product->name = $request->name;
$product->menu = $request->menu;
$product->slug = $request->slug;
$product->description = $request->description;
$product->link = $request->link;
$product->topic_id = $request->topic_id;
$product->save();
}
web.php
Route::resource('product','ProductController');
and Product/create.blade.php
<div class="card-body">
<form method="post" action="{{action('ProductController@store')}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="exampleFormControlInput1">Product Name</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Product Name">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Menu</label>
<input type="text" name="menu" id="menu" class="form-control" placeholder="Menu">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Slug</label>
<input type="text" name="slug" id="slug" class="form-control" placeholder="Slug">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Body</label>
<input type="text" name="description" id="description" class="form-control" placeholder="Body">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Upload Product Image</label>
<input type="file" name="image" id="image" class="form-control" placeholder="Product Image">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Link</label>
<input type="text" name="link" id="link" class="form-control" placeholder="Link">
</div>
<button type="submit" class="btn btn-success btn-lg btn-block" style="margin-top:10px">Create New Product</button>
</form>
Then I need to store topic_id in the Product Table. how could I pass topic_id to the ProductController store function?