Laravel 405(不允许使用方法)

时间:2018-07-22 02:27:23

标签: javascript php laravel

我想实现一个类似系统,但是我不知道一旦用户点击了一个a-tag的“类似”按钮,如何将请求发送给类似控制器。由于ArticleController已经具有存储的POST方法,因此我创建了一个名为LikeController的新控制器。

这是控制器的PHP代码

use App\Article;
use App\Like;
use App\Events\NewLike;

class LikeController extends Controller
{
  public function store(Request $request)
  {
      $id = $request['id'];
      $article = Article::findOrFail($id);
      $user_id = $request->user()->id;
      $like = Like::where(['likeable_id' => $article->id, 'user_id' => $user_id]);
      $like_type = $possibleLike->like_type;

      // check if article exists

      if(!$like) {
          $like = new Like([
              'user_id' => $user_id,
              'like_type' => 'like',
              'likeable_id' => $article->id,
              'likeable_type' => 'article'
          ]);
      } else {
          if($like->like_type == 'like') {
              $like->like_type == 'dislike';
          } else if($like->like_type == 'dislike') {
              $like->like_type == 'like';
          }
      }
      $like->save();

      event(new NewLike($like));


      return redirect('/articles'.$article->id);
  }
}

这是我在vue组件中的Javascript以启动请求:

export default {
  props: ['articleId'],
  methods: {
    activateLike (articleId) {
      axios({
        type: 'POST',
        url: '/likes',
        headers: {
          'X-CSRF-Token': $('meta[name=csrf-token]').attr('content')
        },
        id: articleId
      }).then(response => {
        console.log(response.data)
      }).catch(function(error) {
        console.log(error)
      })
    }
  }
}
</script>

如果有人需要更多信息,我将在下面发布。


路线:

Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about');
Route::get('/leaderboard', 'PagesController@leaderboard');

Route::resource('articles', 'ArticlesController');
Route::get('article/{category}', 'ArticlesController@display');

Route::post('/likes', 'LikeController@store');

Auth::routes();

Route::get('/dashboard', 'DashboardController@index')->name('dashboard');

0 个答案:

没有答案