我使用laravel 5.2创建了一个视图页面,在该页面中创建了一个表并从mysql数据库中获取数据。并为编辑和删除提供动态下拉按钮。当我点击删除它将从表中删除相应的行。我完成并正常工作。我想做编辑部分。当我单击编辑选项时,它将重定向到一个表单,我需要从相应的行获取所有细节到表单文本字段,我们可以编辑的东西,当我们点击保存按钮它将更新数据到mysql表。 在这里,我无法将相应行的详细信息提取到表单文本字段中。谁能帮助我做到这一点。 我的浏览页面" userAdmin.blade.php"是
@extends('app')
@section('content')
<div class="templatemo-content-wrapper" xmlns="http://www.w3.org/1999/html">
<ol class="breadcrumb">
<li><a href="{{ url("/") }}"><font color="green">Home</font></a></li>
<li class="active">user information</li>
</ol>
<div class="templatemo-content">
<h1>View/Edit user information</h1>
<div>
<div>
<div>
<table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc">
<h3>Select User :</h3>
<thead>
<tr>
<th>User ID</th>
<th>User Desc</th>
<th>Contact Name</th>
<th>Contact Email</th>
<th>Time Zone</th>
<th>Active</th>
<th>Last Login (GMT+05:30)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{{--{{ UserController::getIndex() }}--}}
@foreach($name as $nam)
<tr>
<td>{{ $nam->userID }}</td>
<td>{{ $nam->description }}</td>
<td>{{ $nam->contactName }}</td>
<td>{{ $nam->contactPhone }}</td>
<td>{{ $nam->timeZone }}</td>
<td>
@if($nam->isActive == '1')
Yes
@else
No
@endif
</td>
<td>{{ date('Y/m/d H:i:s',($nam->lastLoginTime)) }}</td>
<td>
{{--@if ( in_array($nam->isActive, array('Yes','No')) )--}}
<div class="btn-group">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
{{--@if ($nam->isActive == 'Yes')--}}
<li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $nam->userID }}"><a href="{{ url('user/add/'.$nam->userID) }}">View/ Edit</a>
</li>
{{--@endif--}}
<li><a href="{{ url('/user/delete/'.$nam->userID)}}">Delete</a></li>
</ul>
</div>
{{--@endif--}}
</td>
</tr>
@endforeach
</tbody>
</table>
{{$name->links()}}
</div>
</div>
</div>
</div>
</div>
<a href="{{ url('user/add') }}"> <input type="submit" id="add" name="add" value="Edit" class="button"></a>
</br>
<h4>Create a new User</h4>
<form role="form" method="POST" action="{{ url('userAdmin') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6 margin-bottom-15">
<input type="text" class="form-control" name="userID" value="{{ old('userID') }}" placeholder="Enter User ID">
</div>
<div class="row templatemo-form-buttons">
<div class="submit-button">
<button type="submit" class="btn btn-primary">New</button>
</div>
</div>
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
@endsection
和Controller页面&#34; userController.php&#34;是
<?php
/**
* Created by PhpStorm.
* User: Rahul vp
* Date: 12/05/16
* Time: 11:06 AM
*/
namespace App\Http\Controllers;
use Mail;
use Illuminate\Support\Facades\DB;
use Faker\Provider\DateTime;
use App\User;
use App\Http\Requests\createUserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\Request;
class UserController extends Controller
{
public $type = 'User';
public function getIndex()
{
$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);
}
public function getData()
{
$name = DB::table('user');
return view('user.add')->with('name', $name);
}
public function userInsert()
{
$postUser = Input::all();
//insert data into mysql table
$data = array('userID'=> $postUser['userID']
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('user')->Insert($data);
//echo "Record Added Successfully!";
$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);
}
public function delete($id)
{
DB::table('user')->where('userID', '=', $id)->delete();
return redirect('userAdmin');
}
}
然后是编辑页面,我需要获取数据以进行更新&#34; add.blade.php&#34;
@extends('app')
@section('content')
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><a href="{{ url("/") }}"><font color="green">Home</font></a></li>
<li class="active">View/Edit User</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">View/Edit User Information</div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('accountAdmin') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">User ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="userID" value="{{ old('userID')}}" placeholder="Enter User ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Active</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('isActive') }}" name="isActive" >
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">User Description</label>
<div class="col-md-6">
<input type="text" class="form-control" name="description" value="{{ old('description') }}" placeholder="Enter the description">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="contactName" value="{{ old('contactName') }}" placeholder="Enter Contact Name">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Phone</label>
<div class="col-md-6">
<input type="text" class="form-control" name="contactPhone" value="{{ old('contactPhone') }}" placeholder="Enter Mobile Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Email</label>
<div class="col-md-6">
<input type="email" class="form-control" name="contactEmail" value="{{ old('contactEmail') }}" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Notify Email</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ old('notifyEmail') }}" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Time Zone</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('timeZone') }}" name="timeZone" >
<option value="0">GMT+05:30</option>
<option value="Inactive">xyz</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Authorized Group</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('distanceUnits') }}" name="distanceUnits" >
<option value="0">all</option>
<option value="1">Km</option>
<option value="2">Nm</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">First Login page</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('firstLoginPageID') }}" name="firstLoginPageID" >
<option value="0">Main Menu</option>
<option value="1">Liter</option>
<option value="2">IG</option>
<option value="3">ft^3</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Maximum Access Level</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('maxAccessLevel') }}" name="maxAccessLevel" >
<option value="3">New/Delete</option>
<option value="1">Read/View</option>
<option value="2">Write/Edit</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Save
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
然后是route.php
Route::any('userAdmin', 'UserController@getIndex');
Route::any('user/add', 'UserController@getData');
Route::any('user/delete/{id}', 'UserController@delete');
Route::post('userAdmin', 'UserController@userInsert');
Route::any('user/add', function()
{
return view('user/add');
});
任何人都可以帮我这样做。回应很明显。
答案 0 :(得分:0)
您的laravel代码并不遵循常规做法或使用laravel的正确方法。
请查看资源控制器 https://laravel.com/docs/5.2/controllers#restful-resource-controllers
也是雄辩的模特 https://laravel.com/docs/5.2/eloquent
现在让我们尝试解决您的问题,将这些问题添加到您的路线
//to show edit form and fetch passed user id info from db
Route::get('user/edit/{id}', 'UserController@edit');
//to get the edited info and save it to db
Route::get('user/update/{id}', 'UserController@update');
现在通过从项目目录运行此命令来创建一个名为user的模型:
php artisan make:model User
现在使用其他用法语句在控制器中添加这些
use App\User; /* User model */
use Illuminate\Database\Eloquent\ModelNotFoundException; /* find or fail error exception class */
现在将这些添加到您的控制器
// Show Edit page.
public function edit($user_id)
{
try{
//Find the user object from model if it exists
$user= User::findOrFail($user_id);
//Redirect to edit user form with the user info found above.
return view('add',['user'=>$user]);
}
catch(ModelNotFoundException $err){
//redirect to your error page
}
}
// Update user
public function update(Request $request, $user_id)
{
try{
//Find the user object from model if it exists
$user= User::findOrFail($user_id);
//Set user object attributes
//the $request index should match your form field ids!!!!!
//you can exclude any field you want.
$user->description = $request['description'];
$user->contactName = $request['contactName'];
$user->contactPhone = $request['contactPhone'];
$user->timeZone = $request['timeZone'];
//Save/update user.
$user->save();
//redirect to somewhere?
}
catch(ModelNotFoundException $err){
//Show error page
}
}
在编辑视图中,您可以通过
访问检索到的用户$user->description //$user->[attribute name]
由于你没有使用刀片,我相信你可以做到这一点,无论你在哪里(...)用旧的替换它(...)|| $用户&GT; ... 除了选择标记
示例:
value="{{ old('contactName') || $user->contactName }}"
对于选择标签我会给你一个例子,你需要自己做其余的
<select class="form-control" value="{{ old('timeZone') || $user->timeZone}}" name="timeZone" >
<option value="0" {{ $user->timeZone == "0"? 'Selected':''}}>GMT+05:30</option>
<option value="Inactive" {{ $user->timeZone == "Inactive"? 'Selected':''}}>xyz</option>
</select>
祝你好运。
Upvotes将不胜感激!