使用模态对话框laravel创建编辑功能

时间:2015-11-01 09:48:34

标签: laravel modal-dialog laravel-5.1

我设法使用模态弹出视图使我的创建功能工作,现在我想对编辑功能做同样的事情,但我似乎无法弄清楚如何做到这一点。有人可以帮我解决问题吗?我的create方法如下所示。

routes.php(我指定了这两个路由)

Route::get('manage_accounts', 'ManageAccountsController@index');
Route::post('manage_accounts', 'ManageAccountsController@register');

blade.php

...

<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#register">Register New User</button>class="glyphicon glyphicon-edit"></i></button>
<button class="btn btn-sm btn-warning" type="button" data-toggle="modal" data-target="#update">Edit&nbsp;<i class="glyphicon glyphicon-edit"></i></button>

 <!-- Modal -->
    <div id="register" class="modal fade" role="dialog">
 ...

Controller.php这样

class ManageAccountsController extends Controller
{
    public $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function index() 
    {
        $users = User::orderBy('name')->get();
        $roles = Role::all();

        return view('manage_accounts', compact('users', 'roles'));
    }

    public function register(StoreNewUserRequest $request)
    {
        // process the form here
        $this->userRepository->upsert($request);
        Session::flash('flash_message', 'User successfully added!');

        return redirect()->back();
    }


}

class UserRepository {

    public function upsert($data)
    {

            // Now we can separate this upsert function here
        $user = new User;
        $user->name     = $data['name'];
        $user->email    = $data['email'];
        $user->password = Hash::make($data['password']);
        $user->mobile   = $data['mobile'];
        $user->role_id  = $data['role_id'];

            // save our user
        $user->save();

        return $user;
    }
}

StoreNewUserRequest.php(验证)

<?php

namespace App\Http\Requests;

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;

use App\Http\Requests\Request;

class StoreNewUserRequest 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()
    {
        // create the validation rules ------------------------

        return [
        'name'             => 'required',                        // just a normal required validation
        'email'            => 'required|email|unique:users',     // required and must be unique in the user table
        'password'         => 'required|min:8|alpha_num',
        'password_confirm' => 'required|same:password',           // required and has to match the password field
        'mobile'           => 'required', 
        'role_id'          => 'required'
        ];
    }
}

0 个答案:

没有答案