0

我找不到删除我的新闻的方法。它返回一个 404(未找到)错误。

我的模型(News.php):

    <?php

class News extends Eloquent {

    protected $table = 'aac_news';

    protected $fillable = array('author', 'title', 'content');
    public $timestamps = true;
}

我的控制器:

        <?php

    class AdminController extends BaseController {



    /**
     * News Repository
     *
     * @var News
     */
    protected $news;

    public function __construct(News $news)
    {
        $this->news = $news;
    }

/** ------------------------------------------
 *  News Functions
 *  ------------------------------------------
 */

    public function get_news()
    {

      return View::make('admin.news_managment')->with('newss', $this->news->all());
    }

    public function create_news()
    {

      return View::make('admin.create_news');
  }

  public function post_create_news()
  {
    $input = Input::all();

    $rules = array(
        'author' => 'required|min:4|max:255',
        'title' => 'required|unique:aac_news,title|min:4|max:255',
        'content' => 'required|unique:aac_news,content|min:10'
        );

    $validation = Validator::make($input, $rules);

    if ($validation->fails()) {

       return Redirect::back()->withErrors($validation);

   } else {

    News::create($input);
    return Redirect::to('news/index');
}
}

public function news_delete($newsId)
{
 $news = $this->news->findOrFail($newsId);
 $news->delete();
 return Redirect::back()->with('success', 'Your news post has been deleted.');
}
}

和我的路线:

    # News Management
Route::get('admin/dash/news', 'AdminController@get_news');
Route::get('admin/dash/news/add', 'AdminController@create_news');
Route::post('admin/dash/news/add', 'AdminController@post_create_news');
Route::get('admin/dash/news/{id}/delete/', 'AdminController@news_delete');

它只是返回我的自定义错误 404 页面。我的控制台(laravel 的 PHP)中没有错误。我具有删除帐户的相同功能,并且可以使用。

4

1 回答 1

2

您是否尝试过使用您的News班级查找项目?

public function news_delete($newsId)
{
    $news = News::find($newsId);
    $news->delete();
    return Redirect::back()->with('success', 'Your news post has been deleted.');
}

虽然你真的应该在告诉用户之前检查它是否被删除。该delete()方法返回受影响的行数,因此您应该检查它是否超过0返回时

于 2013-08-05T03:42:07.273 回答