【问题标题】:Laravel polymorphic voting system with namespaces带有命名空间的 Laravel 多态投票系统
【发布时间】:2014-12-23 07:05:53
【问题描述】:

我正在尝试将我希望的简单投票模块添加到帖子和 cmets。 “连接”是我的应用程序中的一种帖子。用户可以对连接或评论投赞成票或反对票。

我遇到的问题是当我尝试对连接进行投票时。我收到此错误:Class name must be a valid object or a string

这是有问题的代码行:

$voteToCast = $vote->voteable()->associate($voteable);

我确定 $voteable var 是 Ardent/Eloquent 模型的一个实例,所以我只能假设错误在于我为模型命名空间的方式,或者是一些我太盲目看不到的可悲错字。任何帮助将不胜感激。

谢谢!

连接模型(帖子类型):

...

public function votes()
{
    return $this->morphMany('Acme\Votes\Vote', 'voteable');
}

还有投票模型:

/* Votes Model */

namespace Acme\Votes;

use Illuminate\Database\Eloquent\Model;
use LaravelBook\Ardent\Ardent;

class Vote extends Ardent {

    protected $table = 'votes';

    protected $fillable = [
        'value',
        'votable_id',
        'voteable_type'
    ];

    /**
     * Establish the polymorphic relationship
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function voteable()
    {
        return $this->morphTo();
    }

    public function users()
    {
        return $this->belongsTo('Acme\\Users\\User');
    }

    /**
     * Vote the item up
     *
     * @param Model $voteable
     * @return mixed
     */
    public static function up(Model $voteable)
    {
        return (new static)->cast($voteable, 1);
    }

    /**
     * Vote the item down
     *
     * @param Model $voteable
     * @return mixed
     */
    public static function down(Model $voteable)
    {
        return (new static)->cast($voteable, -1);
    }

    /**
     * Execute the vote
     *
     * @param Model $voteable
     * @param int $value
     * @return bool
     */
    protected function cast(Model $voteable, $value = 1)
    {
        if (!$voteable->exists) return false;

        $vote = new static;
        $vote->value = $value;

        $voteToCast = $vote->voteable()->associate($voteable);
        $voteToCast->save();
    }

    /**
     * Restrict the votes so the absolute value is 1
     *
     * @param $value
     */
    public function setValueAttribute($value)
    {
        $this->attributes['value'] = ($value == -1) ? -1 : 1;
    }
}

投票控制器:

...

public function cast($connection)
{

    $voteable = Connection::findOrFail($connection);

    if (Input::get('value' < 1)){
        return Vote::down($voteable);
    }

    return Vote::up($voteable);

}

【问题讨论】:

  • 您是否尝试过 var_dump 或 print_r 来验证您的变量是否包含您认为它们所做的事情?
  • 嗨@SimonSvensson,感谢您的回复。是的,我一直在检查几乎所有我能想到的使用 Xdebug 的相关点。我认为据我所知,这些碎片已经到位。您是否建议我验证某个特定变量?
  • 我刚刚在 laravel 的语言环境实例上使用您的所有代码对其进行了测试,并且一切正常。堆栈跟踪是否包含任何有用的信息?
  • @jakeharris 使用堆栈跟踪并检查错误发生的位置..
  • 感谢大家入住!不幸的是,堆栈跟踪只记录了立即关闭:1. Symfony\Component\Debug\Exception\FatalErrorException …/­vendor/­laravelbook/­ardent/­src/­LaravelBook/­Ardent/­Ardent.php382 0. Illuminate\Exception\Handler handleShutdown &lt;#unknown&gt;0

标签: laravel laravel-4 eloquent


【解决方案1】:

在进行了更多故障排除后,这似乎是 Ardent 处理关系的方式存在问题。我能够在我的投票模型上使用 Eloquent 而不是 Ardent,并且投票机制现在可以完美运行。

【讨论】:

    猜你喜欢
    • 2015-01-08
    • 1970-01-01
    • 2015-04-25
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 2015-03-08
    • 1970-01-01
    相关资源
    最近更新 更多