我尝试销毁帖子时,方法App \ Post :: __ toString()不得抛出异常错误

时间:2018-02-10 00:27:15

标签: laravel eloquent laravel-5.2

我目前正在使用Laravel 5.2。

当我在Post控制器上调用destroy方法时,我收到以下消息:

enter image description here

我已经看过这个问题,因为它看起来与我的非常相似,虽然答案无法提供帮助:Exception being thrown when trying to delete model in laravel 5.2

答案无法提供帮助,因为我无法在我的项目中找到任何名为Entrust的文件夹,并且编辑config / auth中的行只会给我一个不能找到“App \ Models \”的错误用户”。

我不确定在哪里可以寻找这个问题,非常感谢你能给予的任何帮助或建议。

这是我的destroy方法和Post控制器:

后控制器

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table    = 'posts';

    /**
     * Define relationship between posts and categories.
     *
     * @return eloquent relationship
     */
    public function category()
    {
        return $this->belongsTo('App\Category', 'category_id');
    }

    /**
     * Define relationship between posts and tags.
     *
     * @return eloquent relationship
     */
    public function tags()
    {
        return $this->belongsToMany('App\Tag', 'post_tag', 'post_id', 'tag_id');
    }
}

销毁方法

public function destroy($id)
{
    //  find the post and tags
    $post = Post::find($id);
    $tags = $post->tags();

    //  update post_tag relationship
    if($tags != null)
    {
        $post->tags()->detach($tags);    
    }

    //  delete the post
    $post->delete();

    //  redirect with flash data to posts.index
    Session::flash('success', 'The blog post was deleted successfully!');
    return redirect()->route('posts.index');
}

1 个答案:

答案 0 :(得分:2)

您正在从 select b.* from (SELECT ID, max(COALESCE(header__timestamp)) max_modified,MAX(CAST(header__change_seq AS DECIMAL(38,0))) max_sequence FROM table_name group by ID) a join table_name b on (a.id=b.id and a.max_modified=b.header__timestamp and a.max_sequence=b.header__change_seq) 获取查询构建器实例,而只是执行$tags = $post->tags();来获取集合。

其次,将一组id传递给detach方法,如下所示:

$tags = $post->tags;