Laravel 4最喜欢的评论数据透视表

时间:2014-07-21 13:38:32

标签: php laravel laravel-4 eloquent

我尝试向网站添加功能,以便用户收藏评论。

添加/显示评论工作正常。我正在努力解决这个问题。

用户模型:     

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface
{
    protected $table = 'user';
    protected $hidden = ['password'];
    protected $fillable = array('username', 'password', 'email');



    //Attach comment to user
    public function addComment($id)
    {

        if (is_numeric($id)) {
            $this->comments()->attach($id);

            return true;
        }

        throw new UnexpectedValueException;
    }

    //Retrieve user comments
    public function comments()
    {
        return $this->belongsToMany('Comment');
    }

    //Attach favourite comment to user
    public function addFavouriteComment($id)
    {

        if (is_numeric($id)) {
            $this->favouriteComment()->attach($id);

            return true;
        }

        throw new UnexpectedValueException;
    }

    //Retrieve user favourite comments
    public function favouriteComments()
    {
        return $this->belongsToMany('FavouriteComment');
    }

}

用户表:

table name = user: 
username             
password         
email        
remember_token
created_at
updated_at  

评论模型:

<?php

    class Comment extends Eloquent
    {
        protected $fillable = array('user_id', 'content');

        public function user()
        {
            return $this->belongsToMany('User', 'comment_user');
        }
    }

评论表:

table name = comments: 
id
content
created_at
updated_At

table name = comment_user: 
user_id
comment_id

收藏的型号:

<?php

    class FavouriteComment extends Eloquent
    {
        protected $fillable = array('user_id', 'comment_id');

        public function user()
        {
            return $this->belongsToMany('User', 'comment_user');
        }
    }

最喜欢的评论表:

table name = favourite_comment_user: 
user_id
favourite_comment_id

将最喜欢的评论附加到用户可以正常工作。

$user = Auth::user();

$user->addFavouriteComment($id);

但是当我尝试从用户那里获得最喜欢的评论时

$user->favouriteComments()->get()

我收到此错误:

Base table or view not found: 1146 Table 'pcc_archive.favourite_comments' doesn't exist 

0 个答案:

没有答案