jeudi 26 septembre 2019

Laravel5.8 : Retrieving data from table

I am building event information page.

My posts table has post_id and category_id.

Now I could retrieve post information(image, organizer, title, place, map, date, description) from posts table.

But I cannot retrieve category name. Also, my post has tags. And I need to retrieve tag's name.

I have no idea how to get those data and show on my posts/show.blade.php.

I am glad if someone helps me out.

post.php

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

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

posts table

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

categories table

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

tags table

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>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire