gpt4 book ai didi

php - laravel 5.4 显示、编辑和删除不起作用

转载 作者:行者123 更新时间:2023-11-29 18:11:17 26 4
gpt4 key购买 nike

Controller

namespace App\Http\Controllers;
use App\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Html\FormFacade;

class RoleController extends Controller
{
public function index()
{
$roles = Role::orderBy('name','asc')->get();
return view('role.index', ['roles' => $roles]);
}
public function create()
{
return view('role.create');
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|unique:roles|string|max:100',
]);
$role = Role::create(['name' => $request->name]);
$role->save();
return Redirect::route('roles.index');
}
public function show(Role $role)
{
$roles = Role::find($role);
return view('role.show', ['roles' => $roles]);
}
public function edit(Role $role)
{
$roles = Role::find($role);
//dd($roles);
return view('role.edit', compact('role'));
}
public function update(Request $request, Role $role)
{
$this->validate($request, [
'name' => 'required|string|max:40|unique:roles,name,' .$role
]);

$role = Role::find($role);
$role->update(['name' => $request->name]);
//dd($role->update(['name' => $request->name]));
return Redirect::route('roles.show', ['role' => $role]);
}
public function destroy(Role $role)
{
//$song = Song::where('slug', $slug)->delete();
$role = Role::where('id', $role)->delete();
dd($role);
//Role::destroy($role);
return Redirect::route('roles.index');
}
}

型号

namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = ['name'];
}

index.blade.php

@extends('base.master')
@section('title')
<title>Models</title>
@endsection
@section('content')
<div class="col-lg-12">
@if(Session::has('success_msg'))
<div class="alert alert-success">{{ Session::get('success_msg') }}</div>
@endif
<!-- Posts list -->
@if(!empty($roles))
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Roles List </h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('roles.create') }}"> Add New</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<table class="table table-striped task-table">
<!-- Table Headings -->
<thead>
<th width="25%">Name</th>
<th width="20%">Action</th>
</thead>

<!-- Table Body -->
<tbody>
@foreach($roles as $role)
<tr>
<td class="table-text">
<div>{{$role->name}}</div>
</td>

<td>
<a href="{{ route('roles.show', $role->id) }}" class="label label-success">Details</a>
<a href="{{ route('roles.edit', $role->id) }}" class="label label-warning">Edit</a>

<a href="{{ route('roles.destroy', $role->id) }}" class="label label-danger" onclick="return confirm('Are you sure to delete?')">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endif
</div>
@endsection
@section('scripts')
<script>
function ConfirmDelete()
{
var x = confirm("Are you sure you want to delete?");
return x;
}
</script>
@endsection

edit.blade.php

@extends('base.master')
@section('title')
<title>Edit Model</title>
@endsection
@section('content')
<h1>Edit Roles</h1>
<hr/>
<!--{{ $role }}-->
<form class="form" role="form" method="POST" action="{{ url('/roles/'. $role->id) }}">
{{ method_field('PATCH') }}
{{ csrf_field() }}
<!-- Role Name Form Input -->
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="name" value="{{ $role->name }}">
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">
Edit
</button>
</div>
</form>
@endsection

show.blade.php

    <div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Role Detalis</h2>
</div>
<div class="pull-right">
<a href="{{ route('roles.index') }}" class="label label-primary pull-right"> Back</a>
</div>
</div>
</div <div class="row">
@foreach ($roles as $role)
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<p>{{strtolower($role->name)}}</p>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Published On:</strong>
{{ $role->created_at->format('d M Y - H:i:s') }}
</div>
</div>
@endforeach
</div>

问题:1 删除后,它会转到详细信息页面。

问题:2 编辑后显示 ->

(2/2) QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name:"222"' in 'where clause' (SQL: select count(*) as aggregate from roles where name = user and name:"222" <> {"id":3 and created_at:"2017-11-17 10:50:13" = updated_at:"2017-11-17 10:50:13"})

问题:3 删除后,它会显示页面。

问题:4 未显示单个值的查找。

最佳答案

Laravel 使用 DELETE请求去删除功能。如果您尝试运行:php artisan route:list你可以发现roles.destroy仅在 DELETE 中调用路由要求。所以替换这一行 <a href="{{ route('roles.destroy', $role->id) }}" class="label label-danger" onclick="return confirm('Are you sure to delete?')">Delete</a>如下所示,

<form method="POST" action="{{ route('roles.destroy', $role->id) }}" accept-charset="UTF-8" id="deleteForm">
<input name="_method" type="hidden" value="DELETE">
{{ scrf_field() }}
<button type="submit" onclick="return confirm('Are you sure to delete?')" class="label label-danger">
</form>

关于php - laravel 5.4 显示、编辑和删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47385195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com