【发布时间】:2019-05-13 19:48:14
【问题描述】:
我有一个包含数据的表,我想对其实现搜索功能。例如,我会在搜索栏中键入一个查询,它会查看表格列中的相似术语,然后显示包含匹配项的行。
这是我不断遇到的错误。
我只是按照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');
编辑:这是我的模型:
【问题讨论】:
-
可以展示一下门票型号吗?
-
在哪里可以找到门票型号?抱歉,我是 Laravel 的新手
-
你创建的模型,在他创建的教程中我认为发布了模型
-
我现在添加了模型。谢谢
-
尝试添加受保护的 $primaryKey='ticketID';或者您的数据库中的主键是什么
标签: php mysql laravel laravel-5