'Create tags in laravel post publishing
Currently when I want to add tags in my posts I have to go to my tags page
and create them then go to my post create
page and add them to my post by multiple input field and so far everything is working fine.
But what if I want to make my tag system works like WordPress? means I write my tags in my multiple field in my post create page
and if that tag already exist just will be add and if not as new tag will be save in database
and in that post as well?
Laravel version: 5.5
Update:
PostController
note: my posts name foods as my app requires (just different naming)
Create method
public function create()
{
$ingredients = Ingredient::all();
$vaghts = Vaght::all();
$categories = Category::all();
$user = Auth::user();
return view('panel.foods.create', compact('ingredients', 'vaghts', 'user','categories'));
}
Store methode
public function store(Request $request)
{
//Validating title and body field
$this->validate($request, array(
'title'=>'required|max:225',
'slug' =>'required|max:255',
'user_id' =>'required|numeric',
'image' =>'sometimes|image',
'description' => 'required|max:100000',
'category_id' => 'required|numeric',
'status' => 'required|numeric',
));
$food = new Food;
$food->title = $request->input('title');
$food->slug = $request->input('slug');
$food->user_id = $request->input('user_id');
$food->description = $request->input('description');
$food->category_id = $request->input('category_id');
$food->status = $request->input('status');
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/');
$request->file('image')->move($location, $filename);
$food->image = $filename;
}
$food->save();
$food->vaghts()->sync($request->vaghts, false);
$food->ingredients()->sync($request->ingredients, false);
//Display a successful message upon save
Session::flash('flash_message', 'Food, '. $food->title.' created');
return redirect()->route('foods.index');
}
Tags method: note: I've two different type of tags and they are the same so if I can get only one i'll do the next one myself
Create method
public function create()
{
return view('panel.vaghts.create');
}
Store method
public function store(Request $request)
{
//Validating title and body field
$this->validate($request, array(
'title'=>'required|max:225',
));
$vaght = new Vaght;
$vaght->title = $request->input('title');
$vaght->save();
//Display a successful message upon save
Session::flash('flash_message', 'Timing, '. $vaght->title.' created');
return redirect()->route('timing.index');
}
Solution 1:[1]
You can first try searching which tags are already existing and which are new and then create the new tags. After that attach/sync all the tags that have been received from user.
//get old vaghts title
$existingVaghts = $food->vaghts->pluck('title')->toArray();
//find new vaghts
foreach($request->vaghts as $vaght) {
if(! ( in_array($vaght, $existingVaghts) ) ) {
//create a new vaght
$newVaght = new App\Vaght;
$newVaght->title = $vaght;
$newVaght->save();
}
}
$attachableVaghts = [];
foreach($request->vaghts as $vaght) {
$attachableVaghts[] = App\Vaght::where('title', $vaght)->pluck('id')->first();
}
$food->vaghts()->sync($attachableVaghts);
That should do it I think. Hope it helps. Check the following link for more https://codeshare.io/5Ppygd
Also for the blade template check the following link https://codeshare.io/axeN7R
Solution 2:[2]
Everything you require is handled with this package
https://github.com/rtconner/laravel-tagging
It makes setting up a tagging system as a piece of cake! just follow the docs
Solution 3:[3]
It's 2022, and maybe I got the simpler solution.
$attachableTags = [];
foreach ($request->tags as $tag) {
$attachableTags[] = Tag::firstOrCreate([
'name' => $tag
])->id;
}
$post->tags()->sync($attachableTags);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Sletheren |
Solution 3 | Karl Hill |