Laravel 5.4 Destroy POST功能出错了

时间:2017-03-15 09:52:57

标签: php laravel laravel-5.4

我正致力于laravel 5.4上的新闻主题。我的问题是,每当用户想要删除帖子时,如果有一个帖子与他有相同的id,它将删除它而不是我最初想要销毁的帖子。 但如果没有,他可以删除他的帖子没有问题。例如,我的用户的ID为4,并且想要删除ID为12的帖子。我第一次点击删除链接时,会删除id 4的帖子。当这个不再存在时,他可以使用id 12来销毁帖子。

控制器中的功能

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\StartUpPost  $startUpPost
 * @return \Illuminate\Http\Response
 */
public function destroy(StartUpPost $startUpPost)
{
    $post = StartUpPost::find($startUpPost);
    $post->delete();

    return redirect('startup/home') -> with('success', 'Post deleted !');
}

路线

Route::get('/StartUpPost/{startUpPost}/delete',[
    'uses' => 'StartUpPostController@destroy',
    'as'=>'startUpPost.delete'
]);

HTML中的链接

<a href="{{route('startUpPost.delete', ['startUpPost' => $startuppost->id])}}">Delete</a>

模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class StartUpPost extends Model
{
    protected $table = 'start_up_posts';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'content','startup_id','image',
    ];

    public function startup() {
        return $this->belongsTo('App\Startup');
    }

    public function startUpComments() {
        return $this->hasMany('App\StartUpComment');
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的控制器功能应如下所示:(无需再次搜索该项目,您已在功能参数中使用此功能StartUpPost $startUpPost

public function destroy(StartUpPost $startUpPost)
{
    $startUpPost->delete();
    return redirect('startup/home') -> with('success', 'Post deleted !');
}

点击它之前检查按钮,错误必须在您的操作链接中删除帖子,检查您如何加载每个按钮

你有$startuppost->id。那个帖子呢?我想它需要第一个。问题可能出在您在删除之前选择$startuppost的查询