Laravel 5.4未检测到提交按钮,并且给出了MassAssignmentException _token错误

时间:2017-08-16 07:15:44

标签: php laravel

查看:

@extends('admin.layout.admin')
@section('content')
<div class="navbar">
    <a class="navbar-brand" href="#">Categories=></a>
    <ul class="nav navbar-nav">
    @if(!empty($categories))
        @forelse($categories as $category)
    <li>
        <a href="{{route('category.show',$category->id)}}">{{$category->name}}</a>
    </li>
        @empty
    <li>No Data</li>
        @endforelse
    @endif
    </ul>
    <a class="btn btn-primary" data-toggle="modal" href="#modal-id">Add Category</a>
    <div class="modal fade" id="modal-id">
        <div class="modal-dialog">
            {!! Form::open(['route' => 'category.store', 'method' => 'post']) !!}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Add Category</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        {{Form::label('name','Name') }}
                        {{Form::text('name',null,array('class' => 'form-control')) }}
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="f btn btn-primary" >Save changes</button>
                    <button type="button" class="f data-dismiss="modal">Close</button>
                    <div class="clr"></div>
                </div>
            </div>
            {!! Form::close() !!}
        </div>
    </div>
</div>
@endsection

控制器:

<?php
namespace App\Http\Controllers;

use App\Category;
use Illuminate\Http\Request;

class CategoriesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categories=Category::all();
        return view('admin.category.index',compact('categories'));
        }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Category::create($request->all());
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
当我使用Php Storm时,

“提交”按钮显示为灰色而不是绿色。任何可以帮助我的问题。提前致谢

3 个答案:

答案 0 :(得分:2)

至于说“MassAssignmentException”,这意味着您尝试添加新数据,但尚未准备好正确的属性$ fillable

protected $guarded = ['price'];

https://laravel.com/docs/5.4/eloquent#mass-assignment

你也可以使用$ guared属性,只是为了保护你的数据。

<Image
    Grid.Column="0" Grid.Row="0"
    Source="add.png"
    HorizontalOptions="EndAndExpand"
    VerticalOptions="EndAndExpand"
    Margin="0,0,30,30"
    HeightRequest="45"
    WidthRequest="45">
    <Image.GestureRecognizers>
        <TapGestureRecognizer 
            Command="{Binding clickNewCard}"
            CommandParameter="3"
            Tapped="TapGestureRecognizer_Tapped"/>          
    </Image.GestureRecognizers>
</Image>

<Entry x:Name="StoreField" InputTransparent="True" IsVisible="False"/>

答案 1 :(得分:1)

您应该在表单中添加csrf令牌字段以防止令牌不匹配异常:

在表单中使用此帮助程序:

{{ csrf_field() }}

<input type="hidden" name="_token" value="{{ csrf_token() }}">

此外,您的提交按钮的课程为btn-primary,这不是绿色!

答案 2 :(得分:0)

你添加

{{ csrf_field() }}

使用普通表单时不是集合HTML。在您的模型中,您必须传递输入文件的名称

   // pass all the name in your form in your case just name
protected $fillable = ['name'];

或者您可以使用

protected $guarded // here you pass what is not going to add