php 在Laravel Post Publishing中创建标签

ih99xse1  于 2023-05-16  发布在  PHP
关注(0)|答案(3)|浏览(88)

目前,当我想在我的帖子中添加标签时,我必须转到我的tags page并创建它们,然后转到我的post create页面并通过多个输入字段将它们添加到我的帖子中,到目前为止,一切正常。
但是如果我想让我的标签系统像WordPress一样工作呢?意思是我写我的标签在我的多个领域在我的post create page,如果该标签已经存在,只是将被添加,如果不是作为新的标签将save in database,并在该职位以及?
Laravel版本:5.5
更新:
PostController注:我的帖子根据我的应用程序要求命名食物(只是不同的命名)
创建方法

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'));
    }

存储方法

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');
    }

标签方法:注:我有两个不同类型的标签,他们是相同的,所以如果我只能得到一个,我会做下一个自己
创建方法

public function create()
    {
      return view('panel.vaghts.create');
    }

存储方法

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');
    }
xxb16uws

xxb16uws1#

也许我有更简单的解决办法。

$attachableTags = [];
foreach ($request->tags as $tag) {
    $attachableTags[] = Tag::firstOrCreate([
        'name' => $tag
    ])->id;
}
$post->tags()->sync($attachableTags);
hwamh0ep

hwamh0ep2#

您可以先尝试搜索哪些标记已经存在,哪些是新的,然后创建新标记。之后,附加/同步已从用户接收到的所有标签。

//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);

我想应该可以了。希望能帮上忙。查看以下链接了解更多https://codeshare.io/5Ppygd
对于刀片式服务器模板,请检查以下链接https://codeshare.io/axeN7R

mqkwyuun

mqkwyuun3#

你所需要的一切都用这个包裹处理
https://github.com/rtconner/laravel-tagging
它使得建立一个标记系统成为一件容易的事!照文件上说的做

相关问题