我遇到问题:我构建了一个应用程序,我的数据库中有2个表:习惯和用户。 用户有一个名为 points 的字段,点击按钮'添加点'后我想增加该值。我在 HabitsController 中添加了一个名为 addPoints 的方法,并在视图中添加了按钮,但是我收到一条错误消息: MethodNotAllowedHttpException没有消息。有我的代码。
HabitsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Habit;
use App\User;
use DB;
use App\Quotation;
class HabitsController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//store in $habits all posts of current user
$habits = DB::table('habits')->where('user_id', auth()->id())->get();
return view('habits.index')->with('habits', $habits);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('habits.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name'=>'required',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = new Habit;
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->user_id = auth()->user()->id;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$single = Habit::find($id);
return view('habits.show')->with('single', $single);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$single = Habit::find($id);
return view('habits.edit')->with('single', $single);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'name'=>'required',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = Habit::find($id);
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$single = Habit::find($id);
$single->delete();
return redirect('/habits')->with('success', 'Habit deleted');
}
public function addPoint(Request $request, $id)
{
$habit = User::find($id);
$habit->points += 1;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
}
show.blade.php
@extends('layouts/app')
@section('content')
<div class="row single">
<div class="col-sm-8 col-sm-offset-2 showSingleHabit">
<h2>{{$single->name}}</h2>
<p>{{$single->description}}</p>
<p>{{$single->difficulty}}</p>
<p>{{$single->NumberOfCompleted}}</p>
<a href="/habits/{{$single->id}}/edit" class="btn btn-default">Edit</a>
{!!Form::open(['action' => ['HabitsController@destroy', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
{!!Form::open(['action' => ['HabitsController@update', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'POST')}}
{{Form::submit('Add point', ['class' => 'btn btn-primary'])}}
{!!Form::close()!!}
</div>
</div>
@endsection
路由/ web.php
Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about');
Route::resource('habits', 'HabitsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController@index');
我将不胜感激任何帮助。 :)
答案 0 :(得分:0)
更改
{{Form::hidden('_method', 'PUT')}}
要
{{1}}