Laravel 4由id删除

时间:2013-08-04 22:05:06

标签: php laravel laravel-4

我找不到删除新闻的方法。它返回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)。我有相同的功能来删除我的帐户,它的工作原理。

1 个答案:

答案 0 :(得分: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