php Laravel在数据库中搜索字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel在数据库中搜索字符串相关的知识,希望对你有一定的参考价值。
https://stackoverflow.com/questions/13386774/using-eloquent-orm-in-laravel-to-perform-search-of-database-using-like
<?php
// ###################### IN YOUR CONTROLLER #########################
public function searchTask() {
$value = Input::get('search_task');
$tasks = Task::where('task', 'LIKE', '%' . $value . '%')->limit(25)->get();
return view('task.search', compact('value', 'tasks') ) ;
}
// ###### IN YOUR LIST TEMPLATE WHERE THE SEARCH BOX IS LOCATED: #############
@extends('layout')
@section('content')
<div class="row">
<div class="col-md-6">
<h1>ALL TASKS</h1>
</div>
<div class="col-md-6">
<form action="{{ route('task.search') }}" class="navbar-form" role="search" method="GET">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search Task..." name="search_task">
<span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search">
<span class="sr-only">Search...</span>
</span>
</button>
</span>
</div>
</form>
</div>
</div>
@if ( !$tasks->isEmpty() )
@foreach ( $tasks as $task)
<div class="task_list"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> {{ $task->task }}
<br>
<a href="{{ route('task.delete', ['id' => $task->id]) }}" class="btn btn-danger"> x </a>
<a href="{{ route('task.edit', ['id' => $task->id]) }}" class="btn btn-primary"> edit </a>
@if ( $task->priority == 0 )
<span class="label label-info">Priority: Normal</span>
@else
<span class="label label-danger">Priority: High</span>
@endif
<!-- <a href="{{ route('task.list', ['id' => $task->project_id]) }}" class="btn btn-primary"> list </a> -->
@if ( !$task->completed )
<a href="{{ route('task.completed', ['id' => $task->id]) }}" class="btn btn-warning"> Mark as completed</a>
@else
<span class="label label-success">Completed</span>
@endif
<hr>
</div>
@endforeach
@else
<p><em>There are no tasks yet</em></p>
@endif
@stop
//######################## IN YOUR SEARCH TEMPLATE ###########################
@extends('layout')
@section('content')
<h1>Displaying Results for: "{{ $value }}" </h1>
@if ( !$tasks->isEmpty() )
@foreach ( $tasks as $task)
<div class="task_list"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> {{ $task->task }}
<br>
<a href="{{ route('task.delete', ['id' => $task->id]) }}" class="btn btn-danger"> x </a>
<a href="{{ route('task.edit', ['id' => $task->id]) }}" class="btn btn-primary"> edit </a>
@if ( $task->priority == 0 )
<span class="label label-info">Priority: Normal</span>
@else
<span class="label label-danger">Priority: High </span>
@endif
@if ( !$task->completed )
<a href="{{ route('task.completed', ['id' => $task->id]) }}" class="btn btn-warning"> Mark as completed</a>
@else
<span class="label label-success">Completed</span>
@endif
<hr>
</div>
@endforeach
@else
<p><em>There are no tasks that match {{ $value }}</em></p>
@endif
<div class="btn-group">
<a class="btn btn-default" href="{{ redirect()->getUrlGenerator()->previous() }}">Go Back</a>
</div>
@stop
// ##################### IN YOUR WEB ROUTES ##############################
<?php
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function(){
// ===================== PROJECTS ======================
Route::get('/projects', 'ProjectController@index')->name('project.show') ;
Route::get('/projects/create', 'ProjectController@create')->name('project.create') ;
// Store the new project from the form posted with the view Above
Route::post('/projects/store', 'ProjectController@store')->name('project.store');
// ==================== TASKS =======================
Route::get('/tasks','TaskController@index')->name('task.show') ;
// Display the Create Task View form
Route::get('/tasks/create', 'TaskController@create')->name('task.create');
// Store the new task from the form posted with the view Above
Route::post('/tasks/store', 'TaskController@store')->name('task.store');
// Search view
Route::get('/tasks/search', 'TaskController@searchTask')->name('task.search');
Route::get('/tasks/edit/{id}','TaskController@edit')->name('task.edit');
Route::get('/tasks/list/{projectid}','TaskController@list')->name('task.list');
Route::get('/tasks/delete/{id}', 'TaskController@destroy')->name('task.delete') ;
Route::post('/tasks/update/{id}', 'TaskController@update')->name('task.update') ;
Route::get('/tasks/completed/{id}','TaskController@completed')->name('task.completed');
// ===================================================
});
Auth::routes();
以上是关于php Laravel在数据库中搜索字符串的主要内容,如果未能解决你的问题,请参考以下文章