如何删除Laravel中的文章

时间:2015-09-15 20:50:58

标签: laravel laravel-5.1

当我尝试删除模型时,我收到此错误:

  

Action App \ Http \ Controllers \ action('ArticlesController @ destroy')未定义。

这样做的正确方法是什么?

代码示例: 形式:

{!! Form::open(['method' => 'DELETE', 'action' => "action('ArticlesController@destroy ')"]) !!}
<div class="form-group">
    {!! Form::submit(['class ' => 'btn btn-danger'] ) !!}
</div>
{!! Form::close() !!}

控制器方法:

 public function destroy(Article $article)
{
    $article->delete();
    return redirect('articles');
}

路线:

Route::get('/','ArticlesController@index');
Route::resource('articles','ArticlesController');

ArticlesController:

namespace App\Http\Controllers;

use App\Article;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Requests\ArticleRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
   class ArticlesController extends Controller
{
    public function index()
    {
        $articles = Article::all();
        return view('articles.index', compact('articles'));
    }

    public function create()
    {
        return view('articles.create');
    }

    public function store(ArticleRequest $request)
    {
        if ($request->hasFile('file')) {

            $file = Input::file('file');
            $imgTitle = $request->title;
            $imagePath = 'uploads/' . $imgTitle . '.jpg';
            $request->image_path = $imagePath;
            $request->published_at = Carbon::now();
            Article::create(array('title' => $request->title,
                'body' => $request->body,
                'image_path' => $imagePath, 'published_at' => $request->published_at));

            Image::make($file)->resize(300, 200)->save($imagePath);

            return redirect('articles');
        } else {
            $url = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=1&q=' . $request->title;
            $url = file_get_contents($url);
            $file = json_decode($url);
            $file = $file->responseData->results[0]->url;
            $imgTitle = $request->title;
            $imagePath = 'uploads/' . $imgTitle . '.jpg';
            $request->image_path = $imagePath;
            $request->published_at = Carbon::now();
            Article::create(array('title' => $request->title,
                'body' => $request->body,
                'image_path' => $imagePath, 'published_at' => $request->published_at));

            Image::make($file)->resize(300, 200)->save($imagePath);

            return redirect('articles');
        }

    }

    public function edit(Article $article)
    {
        return view('articles.edit', compact('article'));
    }

    public function update(Article $article, ArticleRequest $request)
    {
        $article->update($request->all());
        return redirect('articles');
    }

//    public function destroy(Article $article,ArticleRequest $request)
//    {
//        $article->delete($request->all());
//        return redirect('articles');
//    }
    public function destroy($id) {

        $article = Article::findOrFail($id);
        $article->delete();
        return redirect('articles');
    }

public function show(Article $article)
{
    return view('articles.show', compact('article'));
}

}

3 个答案:

答案 0 :(得分:1)

在表单中,您需要发送要删除的文章的ID

例如

这是一个使用您创建操作的网址的表单:

Member.all.sort_by { |member| @executive.index(member.executive_position) }

控制器和删除操作:

 {!! Form::open(array('url' => 'articles' . $article->id)) !!}
                            {!! Form::hidden('_method', 'DELETE') !!}
                            {!! Form::submit('Delete article', array('class' => 'btn btn-danger')) !!}
{!! Form::close() !!}

答案 1 :(得分:0)

您需要使用findfindOrFail来获取要删除的特定文章。我更喜欢findOrFail,因为如果文章ID不存在,它将失败并返回404。

<强> ArticlesController.php

public function destroy($id) {

    $article = Article::findOrFail($id);
    $article->delete();
}

查看

{!! Form::open(['action' => ['ArticlesController@destroy', $article->id], 'method' => 'delete']) !!}
    {!! Form::submit('Delete', ['class'=> 'btn btn-danger']) !!}
{!! Form::close() !!}

注意:

只是记录在案,您的网址不应反映公共目录。目前您有localhost/Blog_Laravel/public/articles。您的网址应为localhost/articles/1,其中1对应于数据库中的article.id.这取决于您的路线设置方式。

答案 2 :(得分:0)

我不知道你从哪里获得这种语法:

'action' => "action('ArticlesController@destroy ')"

但应该是:

'action' => 'ArticlesController@destroy'