使用模态视图laravel一起存储和更新方法

时间:2015-11-02 08:04:45

标签: laravel modal-dialog laravel-5.1

我有一个商店和更新方法,我想使用相同的模态框来提示,但是我注意到store方法采用

的语法
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>

而我的更新方法,

<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
          <input type="hidden" name="_method" value="PUT">

有没有办法可以指定使用哪种方法,具体取决于所选的选项,我创建了两个按钮,分别为每个。

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

有没有办法可以使用相同的模态框单独调用,或者我必须创建两个重复的模态框,一个用于存储,另一个用于更新?

我的部分代码如下所示..

blade.php

<div class="well col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
  <div class="row user-row">
    <div class="col-xs-2 col-sm-3 col-md-4 col-lg-4">
      <h5 style="font-weight: bold">{{ $user->name }}</h5>
    </div>
    <div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 dropdown-user" data-for=".{{ $user->id }}">
      <h5 class="glyphicon glyphicon-chevron-down text-muted pull-right">&nbsp;</h5>
    </div>
  </div>
  <div class="row user-infos {{ $user->id }}">
    <div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
      <div class="panel panel-info">
        <div class="panel-heading">
          <h2 class="panel-title">User Information</h2>
        </div>
        <div class="panel-body">
          <div class="row">
            <div class=" col-md-10 col-lg-10 hidden-xs hidden-sm">
            <div class="col-xs-5">User level:</div><div class="col-xs-5"> {{ $user->role->role_description }}</div>
            <div class="col-xs-5">Email:</div> <div class="col-xs-5"> {{ $user->email }}</div>
            <div class="col-xs-5">Phone number: </div> <div class="col-xs-5"> {{ $user->mobile }} </div>
            <div class="col-xs-5">Office extension: </div> <div class="col-xs-5"> [ TO IMPLEMENT ]</div>
          </div>
        </div>
      </div>
      <div class="panel-footer">
        <button class="btn btn-sm btn-warning btn--edit" type="button" 
        data-toggle="modal" data-target="#form">Edit&nbsp;<i class="glyphicon glyphicon-edit"></i></button>
        <span class="pull-right">
          <button class="btn btn-sm btn-danger" type="button">Inactive&nbsp;<i class="glyphicon glyphicon-remove"></i></button>
        </span>
     </div>
    </div>
  </div>
  </div> 
  <input type="hidden" name="user_id" value="{{ $user->id }}" />
  @endforeach               
</div> 

@if(Session::has('flash_message'))
<div class="alert alert-success col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
  {{ Session::get('flash_message') }}
</div>
@endif

<div class="col-sm-offset-1 col-sm-2">

  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>

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

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">User Information</h4>
        </div>

        <div class="modal-body">
          <form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
          <input type="hidden" name="_method" value="PUT">
          <input type="hidden" name="_token" value="{{ csrf_token() }}">

          <div class="form-group">
            <label class="control-label col-sm-3" for="name">Username:</label>
            <div class="col-sm-5 @if ($errors->has('name')) has-error @endif"> 
            <input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
              @if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> @endif
            </div>
          </div>

          <div class="form-group">
            <label class="control-label col-sm-3" for="password">Password:</label>
            <div class="col-sm-5 @if ($errors->has('password')) has-error @endif"> 
            <input type="password" class="form-control" type="hidden" id="password" name="password" placeholder="Enter login password">
              @if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> @endif
            </div>
          </div>
...

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 store(StoreUserRequest $request)
    {
      // validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
        $this->userRepository->upsert($request);

        Session::flash('flash_message', 'User successfully added!');

        return redirect()->back();

    }

    public function update(StoreUserRequest $request, $id)
    {
        // validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
        $this->userRepository->upsert($request, $id);

        Session::flash('flash_message', 'User successfully updated!');

        return redirect()->back();

    }
}

class UserRepository {

    public function upsert($data, $id)
    {

            // You will also need something like this
            if(isset($id))
            {
                $user = User::find($id);
            }
            else {
                $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;
        }
    }

0 个答案:

没有答案