当我尝试销毁 post 时,方法 App\Post::__toString() 不得抛出异常错误
Method App\Post::__toString() must not throw an exception error when I try to destroy a post
我目前正在使用 Laravel 5.2。
当我在 Post 控制器上调用 destroy 方法时,我收到以下消息:
我看了一下这个问题,因为它看起来和我的很相似,虽然答案没有帮助:
答案无法提供帮助,因为我无法在我的项目中找到任何名为 Entrust 的文件夹,编辑 config/auth 中的行只会给我一个不同的错误,即无法找到 "App\Models\User".
我不太确定还能在哪里寻找这个问题,非常感谢您能提供的任何帮助或建议。
这是我的销毁方法和 Post 控制器:
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');
}
您正在从 $tags = $post->tags();
获取查询构建器实例,而只需执行 $tags = $post->tags;
即可获取集合。
其次,像这样将一个 id 数组传递给分离方法:
// find the post and tags
$post = Post::find($id);
$ids = $post->tags->pluck('id');
// update post_tag relationship
if(count($ids))
{
$post->tags()->detach($ids);
}
// delete the post
$post->delete();
我目前正在使用 Laravel 5.2。
当我在 Post 控制器上调用 destroy 方法时,我收到以下消息:
我看了一下这个问题,因为它看起来和我的很相似,虽然答案没有帮助:
答案无法提供帮助,因为我无法在我的项目中找到任何名为 Entrust 的文件夹,编辑 config/auth 中的行只会给我一个不同的错误,即无法找到 "App\Models\User".
我不太确定还能在哪里寻找这个问题,非常感谢您能提供的任何帮助或建议。
这是我的销毁方法和 Post 控制器:
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');
}
您正在从 $tags = $post->tags();
获取查询构建器实例,而只需执行 $tags = $post->tags;
即可获取集合。
其次,像这样将一个 id 数组传递给分离方法:
// find the post and tags
$post = Post::find($id);
$ids = $post->tags->pluck('id');
// update post_tag relationship
if(count($ids))
{
$post->tags()->detach($ids);
}
// delete the post
$post->delete();