我有一个包含数据的表,我想对它实现搜索功能。例如,我将在搜索栏中键入查询,它会在表列中浏览相似的术语,然后显示包含匹配项的行。
这是我不断遇到的错误。
我刚刚遵循this教程,而是使用了我的变量。我不知道我在做什么错。我按照它做了一切。
这是我的TicketController代码:
<?php
namespace App\Http\Controllers;
use App\Ticket;
use Illuminate\Http\Request;
use App\Http\Requests\TicketUpdateRequest;
class TicketController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$tickets= Ticket::latest()->paginate(10);
return view('tickets.index', compact('tickets'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('tickets.create');
}
public function delete(Ticket $ticket)
{
return view('tickets.delete', ['tickets' => $tickets]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
Ticket::create([
'summary'=>request('summary'),
'description'=>request('description'),
'status'=>request('status'),
'updated_at'=>request('updated_at'),
'name_client'=>request('name_client'),
'contactnum'=>request('contactnum'),
'location'=>request('location'),
'rtype'=>request('rtype'),
'personnel'=>request('personnel'),
'priority'=>request('priority'),
'notes'=>request('notes'),
]);
return redirect()->route('tickets.index');
}
/**
* Display the specified resource.
*
* @param \App\Ticket $ticket
* @return \Illuminate\Http\Response
*/
public function show(Ticket $ticket)
{
return view('tickets.show', compact('ticket'));
}
public function search(Request $request)
{
$search = $request->get('search');
$tickets = Ticket::latest('tickets')->orWhere('rtype', 'like', '%'.$search.'%')->paginate(10);
return view('tickets.index',['tickets' => $tickets]);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Ticket $ticket
* @return \Illuminate\Http\Response
*/
public function edit(Ticket $ticket)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Ticket $ticket
* @return \Illuminate\Http\Response
*/
public function update(TicketUpdateRequest $request, Ticket $ticket)
{
$ticket->summary = request('summary');
$ticket->description = request('description');
$ticket->status = request('status');
$ticket->name_client= request('name_client');
$ticket->contactnum= request('contactnum');
$ticket->location= request('location');
$ticket->rtype= request('rtype');
$ticket->personnel= request('personnel');
$ticket->priority= request('priority');
$ticket->notes= request('notes');
$ticket->save();
return redirect()->route('tickets.index');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Ticket $ticket
* @return \Illuminate\Http\Response
*/
public function destroy(Ticket $ticket)
{
$ticket->delete();
return redirect()->route('tickets.index');
}
}
以及导航栏的搜索部分:
<form action="/search" method="get">
<input class="form-control form-control-dark w-100" type="search" placeholder="Search" aria-label="Search"></form>
这也是我的索引页的表部分:
<table class="table table-striped table-sm">
<thead>
<tr>
<th>Ticket</th>
<th>Last Updated</th>
<th>Subject</th>
<th>Request Type</th>
<th>Request Details</th>
<th>Notes</th>
<th>Priority</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->updated_at}}</td>
<td>{{$ticket->summary}}</td>
<td>{{$ticket->rtype}}</td>
<td>{{str_limit($ticket->description, 40)}}</td>
<td>{{str_limit($ticket->notes, 40)}}</td>
<td>{{$ticket->priority}}</td>
<td>{{$ticket->status}}</td>
<td><a href="/tickets/{{$ticket->id}}" class="btn btn-primary">Update</a>
<a href="/tickets/delete/{{$ticket->id}}" class="btn btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
最后是我的路线:
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/tickets', 'TicketController@index')->name('tickets.index')->middleware('auth');
Route::get('/tickets/create','TicketController@create')->name('tickets.create')->middleware('auth');
Route::post('/tickets/create','TicketController@store')->name('tickets.store')->middleware('auth');
Route::post('/tickets/{ticket}','TicketController@update')->name('tickets.update')->middleware('auth');
Route::get('/tickets/delete/{ticket}','TicketController@delete')->name('tickets.delete')->middleware('auth');
Route::post('/tickets/delete/{ticket}','TicketController@destroy')->name('tickets.destroy')->middleware('auth');
Route::get('/tickets/{ticket}','TicketController@show')->name('tickets.show')->middleware('auth');
Route::get('/', 'TicketController@index');
Route::get('/search','TicketController@search');
Route::resource('tickets','TicketController');
Route::get('/clientrequest','ClientRequestController@create')->name('clientrequest');
Route::post('/clientrequest','ClientRequestController@store')->name('clientrequest');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('admin/routes', 'HomeController@admin')->middleware('admin');
编辑:这是我的模型:
答案 0 :(得分:0)
最新和最古老的方法使您可以轻松地按默认日期或日期对结果进行排序。
Ticket::latest('tickets')->orWhere('rtype', 'like', '%'.$search.'%')->paginate(10);
在雄辩的查询中,您要传递tickets
列用于orderBy(latest)查询。
虽然它在您的表中不存在。将其更改为按日期created_at
或任何其他列的顺序应该可以解决该错误。
Ticket::Where('rtype', 'like', '%'.$search.'%')->latest()->paginate(10);
答案 1 :(得分:0)
创建路线
Route::get('/user', 'UserMasterController@index')->name('user');
Route::get('/add', 'UserMasterController@add')->name('user.add');
Route::post('/add_record', 'UserMasterController@add_record')->name('user.add_record');
Route::get('/edit_user/{id}', 'UserMasterController@edit_user')->name('user.edit_user');
Route::post('/update_user/{id}', 'UserMasterController@update_user')->name('user.update_user');
Route::any('/delete/{id}', 'UserMasterController@delete')->name('user.delete');
创建控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\UserMaster;
use App\Role;
use Validator;
use File;
use DB;
class UserMasterController extends Controller
{
public function index(){
$roles = Role::all();
$users = DB::table('user_master')->simplePaginate(1);
return view('users.index',compact('users','roles'));
}
public function add(){
$roles = Role::all();
return view('users.create',compact('roles'));
}
public function add_record(Request $request){
$validatedData = $request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:user_master|email',
'phone' => 'required|numeric',
'role_id' => 'required'
]);
$customers = new UserMaster;
$customers->name = $request->input('name');
$customers->email = $request->input('email');
$customers->phone = $request->input('phone');
$customers->role_id = $request->input('role_id');
if($request->hasFile('image'))
{
$image = $request->file('image');
$filename = $request->image->getClientOriginalName();
$destinationPath = public_path('/uploads');
$image->move($destinationPath, $filename);
}
$customers->image = $filename;
$customers->save();
return redirect()->action('UserMasterController@index');
}
public function edit_user(Request $request,$id){
$roles = Role::all();
$users = UserMaster::find($id);
$selectedRole = UserMaster::first()->role_id;
return view('users.edit',compact('users','roles','selectedRole'));
}
public function update_user(Request $request,$id)
{
$validatedData = $request->validate([
'name' => 'required|max:30',
'email' => 'required|email',
'phone' => 'required|numeric',
'role_id' => 'required'
]);
$customers = UserMaster::find($id);
$customers->name = $request->input('name');
$customers->email = $request->input('email');
$customers->phone = $request->input('phone');
$customers->role_id = $request->input('role_id');
if($request->hasFile('image'))
{
$userimage = UserMaster::select('image')->where('id',$id)->first();
$image_name = basename($userimage->image);
$image_path = public_path('uploads/'.$image_name);
//dd($image_path);
if(File::exists($image_path)) {
File::delete($image_path);
}
$image = $request->file('image');
$filename = $request->image->getClientOriginalName();
$destinationPath = public_path('/uploads');
$image->move($destinationPath, $filename);
}
$customers->image = $filename;
$customers->save();
return redirect()->route('user')
->with('success','User updated successfully');
}
public function delete($id)
{
UserMaster::find($id)->delete($id);
return redirect()->route('user')
->with('success','User delete successfully');
}
}
创建模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Role;
class UserMaster extends Model
{
protected $table = 'user_master';
public function roles(){
return $this->hasOne('App\Role','user_id');
}
}
创建index.blade.php
@extends('layouts.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Users</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('user.add') }}"> Create New User</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Image</th>
<th>Role</th>
<th width="280">Action</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name}}</td>
<td>{{ $user->email}}</td>
<td>{{ $user->phone}}</td>
<td>{{ $user->roles->name}}</td>
<td><img height="50px" width="50px" class="user-pic" src="<?php echo asset("uploads/$user->image")?>"></td> </td>
<td>
<a class="btn btn-primary" href="{{ route('user.edit_user',$user->id) }}">Edit</a>
<form action="{{ route('user.delete', $user->id) }}" method="DELETE">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
<!-- <button class="deleteRecord" data-id="{{ $user->id }}" >Delete Record</button> -->
</td>
{{ $users->links() }}
</tr>
@endforeach
</table>
@endsection
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$(".deleteRecord").click(function(){
var id = $(this).data("id");
//var token = $("meta[name='csrf-token']").attr("content");
$.ajax(
{
url: "destroy/"+id,
type: 'Post',
data: { "id": id, "_token": "{{ csrf_token() }}",},
dataType: "JSON",
success: function (){
console.log("it Works");
}
});
});
});
</script>
创建表单create.blade.php
@extends('layouts.layout')
@section('content')
<div id="">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Add User</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
</div>
<div class="panel-body">
<form role="form" action="{{ route('user.add_record') }}" id="add_customer" method="post" enctype="multipart/form-data">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="row">
<div class="col-lg-4">
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Name">
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
</div>
<div class="col-lg-4">
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
<label for="name">Email</label>
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<div class="form-group {{ $errors->has('phone') ? 'has-error' : '' }}">
<label for="name">Phone</label>
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone">
<span class="text-danger">{{ $errors->first('phone') }}</span>
</div>
</div>
<div class="col-lg-4">
<div class="form-group {{ $errors->has('role_id') ? 'has-error' : '' }}">
<label for="name">Role</label>
<select name="role_id" class="form-control" id="role_id">
<option value="">-- Select Role --</option>
@foreach ($roles as $role)
<option value="{{ $role->id }}">{{ ucfirst($role->name) }}</option>
@endforeach
</select>
<span class="text-danger">{{ $errors->first('role_id') }}</span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<div class="form-group {{ $errors->has('image') ? 'has-error' : '' }}">
<label for="name">Image</label>
<input type="file" name="image" id="image" class="form-control" placeholder="Image">
<span class="text-danger">{{ $errors->first('image') }}</span>
</div>
</div>
</div>
<button type="submit" valur="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default">Cancel</button>
</form>
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
</div>
@endsection
创建编辑表单edit.blade.php
@extends('layouts.layout')
@section('content')
<div id="">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Add Role</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
</div>
<div class="panel-body">
<form role="form" action="{{ route('user.update_user',$users->id) }}" id="update_role" method="post" enctype="multipart/form-data">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="row">
<div class="col-lg-4">
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" value="<?php echo $users->name ?>" placeholder="Name">
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
</div>
<div class="col-lg-4">
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
<label for="name">Email</label>
<input type="text" name="email" id="email" class="form-control" value="<?php echo $users->email ?>" placeholder="Email">
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<div class="form-group {{ $errors->has('phone') ? 'has-error' : '' }}">
<label for="name">Phone</label>
<input type="text" name="phone" id="phone" class="form-control" value="<?php echo $users->phone ?>"placeholder="Phone">
<span class="text-danger">{{ $errors->first('phone') }}</span>
</div>
</div>
<div class="col-lg-4">
<div class="form-group {{ $errors->has('role_id') ? 'has-error' : '' }}">
<label for="name">Role</label>
<select name="role_id" class="form-control" id="role_id">
<option value="">-- Select Role --</option>
@foreach ($roles as $role)
<option value="{{ $role->id }}" {{ $selectedRole== $role->id ? 'selected="selected"' : '' }}>{{ $role->name }}</option>
@endforeach
</select>
<span class="text-danger">{{ $errors->first('role_id') }}</span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<div class="form-group {{ $errors->has('image') ? 'has-error' : '' }}">
<label for="name">Image</label>
<input type="file" name="image" id="image" class="form-control" placeholder="Image">
<span class="text-danger">{{ $errors->first('image') }}</span>
</div>
</div>
</div>
<button type="submit" valur="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default">Cancel</button>
</form>
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
</div>
@endsection
创建布局文件layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 5.5 CRUD Application</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>