如何在同一视图页面上使用laravel更新数据库?

时间:2019-06-20 14:19:13

标签: php laravel

我想从具有索引的数据库中获取客户信息。在同一页面上,我想更新此信息而不创建编辑页面。 每当编辑数据并按下按钮时,都会出现“ 419 | Page Expired”错误。你能给我个主意吗?谢谢你。

我的index.blade.php是

 <div class="widget-body clearfix">
     @foreach($posts as $post)
      <form method="POST" action="{{ route('front.home.index') }}" enctype="multipart/form-data">                         
        <div class="form-group row customerinfo--area">
        <div class="col-md-4">
          <label class="col-form-label" for="l0">Name</label>
          <input value="{{$post->customername}}" class="form-control" name="customername" type="text">
        </div>  
        <div class="col-md-4">
          <label class="col-form-label" for="l0">Email</label>
          <input value="{{$post->email}}" class="form-control" name="email" type="text">
        </div>  		    							                               
       <div class="form-actions">
          <div class="form-group row">
          <div class="col-md-12 ml-md-auto btn-list">
          <button class="btn btn-primary btn-rounded" type="submit">Save</button>
          </div>
          </div>
       </div>
     </form>
     @endforeach
     </div>
我的indexController是

<?php

namespace App\Http\Controllers\front\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Customers;

class indexController extends Controller
{
    public function index()
    {
	$posts=DB::select("select * from `customers` where id=1");
        return view('front.home.index',['posts'=>$posts]);
    }
    public function edit(Request $request)
    {
        $all = $request->except('_token');
        $data = Customers::where('id','1')->get();
        $update = Customers::where('id','1')->update($all);
        
	if($update)
        {
            return redirect()->back()->with('status','Customer was edited');
        }
        else
        {
            return redirect()->back()->with('status','Customer was not edited');
        }
    }
 }
我的web.php / routes是

Route::group(['namespace'=>'front','prefix'=>'front','as'=>'front.'],function(){
	  Route::group(['namespace'=>'home','as'=>'home.'],function(){
           Route::get('/','indexController@index')->name('index');
	   Route::post('/','indexController@edit')->name('edit');
    });
});

1 个答案:

答案 0 :(得分:2)

这是因为Laravel表单需要一个csrf令牌,而您却丢失了它。将其添加到您的表单下方:

<form action="" method="post">
    @csrf
    ...

在此处了解更多信息:https://laravel.com/docs/5.7/csrf