Laravel5.8: получение данных из таблицы - PullRequest
0 голосов
/ 26 сентября 2019

Я создаю информационную страницу о событии.

В таблице моих сообщений есть post_id и category_id.

Теперь я могу получить информацию о сообщении (изображение, органайзер, название, место, карту, дату, описание) из таблицы сообщений.

Но я не могу получить название категории.Кроме того, мой пост имеет теги.И мне нужно получить имя тега.

Я понятия не имею, как получить эти данные и показать в моих сообщениях / show.blade.php.

Я рад, если кто-то поможетя вышел.PostsController.php

 public function store(CreatePostsRequest $request)
{

    $image = $request->image->store('posts');

    //create the posts
    $post = Post::create([
        'image' => $image,
        'category_id' => $request->category,
        'organizer' => $request->organizer,
        'title' => $request->title,
        'place' => $request->place,
        'map' => $request->map,
        'date' => $request->date,
        'published_at' => $request->published_at,
        'description' => $request->description
    ]);

    if($request->tags) {
        $post->tags()->attach($request->tags);
    }

    return redirect(route('posts.index'));
}

post.php

 public function category() 
{
    return $this->belongsTo(Category::class);
}

public function tags() 
{
    return $this->belongsToMany(Tag::class);
}

таблица сообщений

Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('image');
            $table->integer('category_id');
            $table->string('organizer');
            $table->string('title');
            $table->string('place');
            $table->string('map');
            $table->date('date');
            $table->timestamp('published_at')->nullable();
            $table->text('description');
            $table->timestamps();
        });

таблица категорий

Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });

таблица тегов

Schema::create('tags', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });

ResultsController.php

public function show($id,Post $post)
    {
        $post= Post::find($id);
        $category = Category::find($id);
        $tag = Tag::find($id);
        return view('posts.show',compact('post'));
    }

show.blade.php

Category: "I want to show category name here !"

    <div class="tag-group">
        Tags:
      <div class="tags">
        "I want to show tag's name here !"
       </div>
      </div>

1 Ответ

1 голос
/ 26 сентября 2019
  1. Создание миграции с помощью сводной таблицы "post_tag" с именем таблицы;Подробности здесь
  2. В вашей миграции для таблицы "posts", пожалуйста, измените $ table-> unsignedBigInteger ('category_id');
  3. Настройте свой контроллер.Вы можете использовать теги $ post-> category или $ post->;
...