控制器
namespace App\Http\Controllers; use App\Http\Requests\UserAddressRequest; use App\Models\UserAddress; use Illuminate\Http\Request; class UserAddressesController extends Controller { // public function index(Request $request){ return view('user_addresses.index',['addresses'=>$request->user()->addresses]); } public function create(){ return view('user_addresses.create_and_edit',['address'=>new UserAddress()]); } public function edit(UserAddress $user_address){ return view('user_addresses.create_and_edit',['address'=>$user_address]); } public function update(UserAddress $user_address,UserAddressRequest $request){ $user_address->update($request->only([ 'province', 'city', 'district', 'address', 'zip', 'contact_name', 'contact_phone', ])); return redirect()->route('user_addresses.index'); } public function destroy(UserAddress $user_address){ $user_address->delete(); return redirect()->route('user_addresses.index'); } public function store(UserAddressRequest $request){ $request->user()->addresses()->create($request->only([ 'province', 'city', 'district', 'address', 'zip', 'contact_name', 'contact_phone', ])); return redirect()->route('user_addresses.index'); } }
路由
Auth::routes(['verify'=>true]); /*Route::get('/home', 'HomeController@index')->name('home');*/ Route::group(['middleware'=>['auth']],function(){ Route::get('user_address','UserAddressesController@index')->name('user_addresses.index');//收货地址列表 Route::get('user_address/create','UserAddressesController@create')->name('user_addresses.create');//收货地址列表 Route::post('user_address','UserAddressesController@store')->name('user_addresses.store');//收货地址保存 Route::get('user_address/{user_address}','UserAddressesController@edit')->name('user_addresses.edit');//收货地址保存 Route::put('user_address/{user_address}','UserAddressesController@update')->name('user_addresses.update');//收货地址保存 Route::delete('user_address/{user_address}','UserAddressesController@destroy')->name('user_addresses.destroy');//收货地址保存 }); Route::fallback(function () { return '好像没有您要的资源'; });
验证器
php artisan make:request Request php artisan make:request UserAddressRequest 此时继成Request的基类,注意验证都要改成通过true class UserAddressRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'province' => 'required', 'city' => 'required', 'district' => 'required', 'address' => 'required', 'zip' => 'required', 'contact_name' => 'required', 'contact_phone' => 'required', ]; } public function attributes() { return [ 'province' => '省', 'city' => '城市', 'district' => '地区', 'address' => '详细地址', 'zip' => '邮编', 'contact_name' => '姓名', 'contact_phone' => '电话', ]; } }
模板
@extends('layouts.app') @section('title','收货地址列表') @section('content') <div class="row"> <div class="col-md-10 offset-md-1"> <div class="card panel-default"> <div class="card-header">收货地址列表<a href="{{ route('user_addresses.create') }}" class="float-right">新增收货地址</a></div> <div class="card-body"> <table class="table table-bordered table-striped"> <thread> <tr> <th>收货人</th> <th>地址</th> <th>邮编</th> <th>电话</th> <th>操作</th> </tr> </thread> <tbody> @foreach($addresses as $address) <tr> <td>{{ $address->contact_name }}</td> <td>{{ $address->full_address }}</td> <td>{{ $address->zip }}</td> <td>{{ $address->contact_phone }}</td> <td> <a href="{{ route('user_addresses.edit',['user_address'=>$address->id]) }}" class="btn btn-primary">修改</a> <form action="{{ route('user_addresses.destroy',['user_address'=>$address->id]) }}" method="post" style="display: inline-block"> {{ csrf_field() }} {{ method_field('DELETE') }} <button class="btn btn-danger" type="submit">删除 </button> </form> </td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> @endsection