Gagal menampilkan post title pada halaman tags di laravel 5.2.

Ini pesan Errornya, Kak. Mohon pencerahannya, Kak. :)

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bloglaravel.name_of_talbe' doesn't exist (SQL: select `posts`.*, `name_of_talbe`.`post_tag` as `pivot_post_tag`, `name_of_talbe`.`post_id` as `pivot_post_id` from `posts` inner join `name_of_talbe` on `posts`.`id` = `name_of_talbe`.`post_id` where `name_of_talbe`.`post_tag` = 3) (View: /var/www/html/blog/resources/views/tags/show.blade.php)

Sepertinya gagal pada bagian @foreach


    <div class="row">
      <div class="col-md-12">
        <table class="table">
          <thead>
            <tr>
              <th>#</th>
              <th>Title</th>
              <th>Tags</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            @foreach($tag->posts as $post)
            <tr>
              <th>{{ $post->id }}</th>
              <td>{{ $post->title }}</td>
              <td>
                @foreach ($post->tags as $tag)
                  <span class="label label-default">{{ $tag->name }}</span>
                @endforeach
              </td>
            </tr>
           @endforeach
          </tbody>
        </table>
      </div>
    </div>

avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Diperbarui 6 tahun yang lalu

9 Jawaban:

tampilin modelnya juga seperti apa, dan relasinya seperti apa belum dijelasin. Kalo dari errornya aja "Base table or view not found: 1146 Table 'bloglaravel.name_of_talbe' doesn't .. " dia make nama table yang ngga ada di database

avatar hilmanski
@hilmanski

2672 Kontribusi 2132 Poin

Dipost 6 tahun yang lalu

Ini file routes.php, Mas Hilman



// Categories
  Route::resource('categories', 'CategoryController', ['except' => ['create']]);
  Route::resource('tags', 'TagController', ['except' => ['create']]);

avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

MODEL

Post.php

 namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function category()
    {
      return $this->belongsTo('App\Category');
    }

    public function tags()
    {
      return $this->belongsToMany('App\Tag');
    }
}
avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

MODEL

Tag.php

 <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
  public function posts()
  {
    return $this->belongsToMany('App\Post', 'name_of_table');
  }
}

avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

CONTROLLER

PostController.php

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Post;

use App\Tag;

use App\Category;

use Session;

class PostController extends Controller
{

    public function __construct()
    {
      $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
      // create a variable and store all the blog posts in it from the database
      $post = Post::orderBy('id', 'desc')->paginate(5);

      // return a view and pass in the above variable
      return view('posts.index')->withPosts($post);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
      $categories = Category::all();
      $tags = Tag::all();
      return view('posts.create')->withCategories($categories)->withTags($tags);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      // validate the data
      $this->validate($request, array(
        'title'       => 'required|max:30',
        'slug'        => 'required|alpha_dash|min:5|max:225',
        'category_id' => 'required|integer',
        'body'        => 'required'
      ));

      // store in the database
      $post = new Post;

      $post->title = $request->title;
      $post->slug  = $request->slug;
      $post->category_id = $request->category_id;
      $post->body  = $request->body;

      $post->save();

      $post->tags()->sync($request->tags, false);

      Session::flash('success', 'The blog post was succesfully save!');

      // redirect to another page
      return redirect()->route('posts.show', $post->id);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      $post = Post::find($id);

      return view('posts.show')->withPost($post);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
      // find the post in the database and save as a variable
      $post = Post::find($id);
      $categories = Category::all();
      $cats = array();
      foreach($categories as $category) {
        $cats[$category->id] = $category->name;
      }

      $tags = Tag::all();
      $tags2 = array();
      foreach ($tags as $tag) {
        $tags2[$tag->id] = $tag->name;
      }

      // return the view and pass in the var we previously created
      return view('posts.edit')->withPost($post)->withCategories($cats)->withTags($tags2);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
      // validate the data
      $post = Post::find($id);
      if ($request->input('slug') == $post->slug) {
        $this->validate($request, array(
          'title'       => 'required|max:225',
          'category_id' => 'required|integer',
          'body'        => 'required'
        ));
      } else {
          $this->validate($request, array(
            'title'       => 'required|max:10',
            'slug'        => 'required|alpha_dash|min:5|max:225|unique:posts',
            'category_id' => 'required|integer',
            'body'        => 'required'
          ));
        }

      // save the data to the database
      $post = Post::find($id);

      $post->title = $request->input('title');
      $post->slug  = $request->input('slug');
      $post->category_id = $request->input('category_id');
      $post->body  = $request->input('body');

      $post->save();

      if (isset($request->tags)) {
        $post->tags()->sync($request->tags);
      } else {
          $post->tags()->sync(array());
      }

      // set flash data with success message
      Session::flash('success', 'This post was successfully seved.');

      // redirect with flash data to posts.show
      return redirect()->route('posts.show', $post->id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
      $post = Post::find($id);

      $post->delete();

      Session::flash('success', 'The post was succesfully deleted.');

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

avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

CONTROLLER

TagController.php



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Tag;

use Session;

class TagController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tags = Tag::all();
        return view('tags.index')->withTags($tags);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, array(
          'name' => 'required|max:225'
        ));

        $tag = new Tag;
        $tag->name = $request->name;

        $tag->save();

        Session::flash('success', 'New Tag was successfully created!');

        return redirect()->route('tags.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      $tag = Tag::find($id);

      return view('tags.show')->withTag($tag);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

Database

avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

Berhasil Mas Hilman, ternyata salah pada file model Post.php, saya kurang teliti. 'name_of_table', ternyata itu perintah untuk memilih table pada database.

 return $this->belongsToMany('App\Post', 'name_of_table');

Dan seharusnya seperti ini.

 return $this->belongsToMany('App\Post', 'post_tag');
avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

Terimakasih banyak, Mas Hilman. :)

avatar gugunguntoro
@gugunguntoro

250 Kontribusi 10 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban