i am working on Laravel project but little bit confused how to post tag-it multiple values to controller and how to save it separately in DB ?? these values will be saved in multiple rows(records) against single foreign key..
<div class="form-group wish-tags">
<label class="col-lg-3 control-label">Wish to visit:</label>
<div class="col-lg-8">
<input type="text" value="" data-role="tagsinput" id="tags" name="wish-tags" class="form-control">
</div>
</div>
<div class="form-group wish-tags">
<label class="col-lg-3 control-label">Already visited:</label>
<div class="col-lg-8">
<input type="text" value="" data-role="tagsinput" id="tags" name="visited-tags" class="form-control">
</div>
</div>
Visitedlist Table
CREATE TABLE `visitedlist` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(10) UNSIGNED NOT NULL,
`locations` VARCHAR(100) NOT NULL,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `FK1_vistedlist_list` (`user_id`),
CONSTRAINT `FK1_vistedlist_list` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE)
WishList Table
CREATE TABLE `wishlist` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(10) UNSIGNED NOT NULL,
`locations` VARCHAR(50) NOT NULL,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `FK1_wishList` (`user_id`),
CONSTRAINT `FK1_wishList` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE)
Depending on how the data is formatted, when you receive it in your controller method, you need to split it into individual items, loop over and save each of them.
So, let‘s you receive the items in your method as "item1,item2,item3", your code could look like this:
$locations = Request::input('wish-tags');
foreach (explode(",", $locations) as $location)
{
$tag = new Tag();
$tag->location = $location;
$tag->user()->associate($theUser);
$tag->save();
}
Hope that helps! If not feel free to leave a comment!