Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:单击按钮后更新数据库时出现问题_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel:单击按钮后更新数据库时出现问题

Php Laravel:单击按钮后更新数据库时出现问题,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个问题:我构建了一个应用程序,我的数据库中有两个表:习惯和用户。用户有一个名为“点”的字段,单击“添加点”按钮后,我想增加该值。我在HabitController中添加了一个名为addPoints和button的方法,但收到一条错误消息:MethodNotAllowedHttpException无消息。这是我的密码 HabitsController.php <?php namespace App\Http\Controllers; use Illuminate\Http\R

我有一个问题:我构建了一个应用程序,我的数据库中有两个表:习惯和用户。用户有一个名为“点”的字段,单击“添加点”按钮后,我想增加该值。我在HabitController中添加了一个名为addPoints和button的方法,但收到一条错误消息: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');
    }
}
如有任何帮助,我将不胜感激。:)

变化

{{Form::hidden('_method', 'POST')}}


现在没有错误,但我的方法不增加点值。你知道控制器方法addPoint有什么问题吗?你想增加什么@Morgan当前用户的my users db表中名为points的值。如果该值为points,则应更改HabitsController@update到HabitsController@addPointnow我收到错误“Action App\Http\Controller”\HabitsController@addPoint没有定义。(视图:C:\xampp\htdocs\happ\resources\views\habits\show.blade.php)您必须更改方法以放置。
Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about');

Route::resource('habits', 'HabitsController');
Auth::routes();

Route::get('/dashboard', 'DashboardController@index');
{{Form::hidden('_method', 'POST')}}
{{Form::hidden('_method', 'PUT')}}