Beruflich Dokumente
Kultur Dokumente
Laravel5SimpleSiteSearchBar| TutorialEdge.net
Laravel5SimpleSiteSearchBar
SomethingthatIvebeenworkingonrecentlyonthesite
isthesearchbarfunctionalitywhichallowspeopleto
effectivelysearcheverythrougheveryarticleonthesite
tofindonethatwillhelpthemthemost.InthistutorialIll
bedocumentingafewofthewaysthatyoucan
implementasimplefullsitesearchusingLaravel5.
Requirements
Inordertocompletethistutorialyou'llhavetosetup
yourlaravel5projecttousetheformfacade.Addthisto
yourcomposer.jsonfile:
"require":{
"laravelcollective/html":"~5.0"
}
Andthenrunthefollowingcomposercommand.
composerupdate
Finallywehavetoaddittoourboththealiasesarray
andtheprovidersarrayfoundinconfig/app.php
https://tutorialedge.net/laravel5simplesitesearchbar
1/7
10/23/2016
Laravel5SimpleSiteSearchBar| TutorialEdge.net
'providers'=>[
....
'Collective\Html\HtmlServiceProvider',
....
],
'aliases'=>[
....
'Form'=>'Collective\Html\FormFacade',
'Html'=>'Collective\Html\HtmlFacade',
....
],
CreatingtheForm
Togetthingsstartedwearegoingtoneedtocreatea
formthatwillallowustoinputoursearches.
{!!Form::open(array('route'=>'queries.search,'class'=>'formnavbarformna
{!!Form::text('search',null,
array('required',
'class'=>'formcontrol',
'placeholder'=>'Searchforatutorial...'))!!}
{!!Form::submit('Search',
array('class'=>'btnbtndefault'))!!}
{!!Form::close()!!}
CreatingourRoutes:
ThewayIveimplementedthesearchfunctionroutein
myprojectistouseaRouteresourceasImgoingtobe
storingthequeriesfurtherdownthelineinordertoget
https://tutorialedge.net/laravel5simplesitesearchbar
2/7
10/23/2016
Laravel5SimpleSiteSearchBar| TutorialEdge.net
suggestionsfornewarticles.Openupyourroutes.php
fileandaddthefollowingroute:
Route::resource('queries','QueryController');
AddingSearchFunctionalitytoourQueryController
Sonowwevecreatedaformandupdatedour
routes.phpfilewecanaddthesearchfunctionalitytoour
controller.CreateanewfunctionthattakesinaRequest
andcallitsearch,likeso:
publicfunctionsearch(Request$request)
{
TutorialEdge.net
//Getsthequerystringfromourformsubmission
Search
Courses
Books
Blog
Tutorials
Register/SignInWithGithub
$query=Request::input('search');
//Returnsanarrayofarticlesthathavethequerystringlocatedsomewher
//ourarticlestitles.Paginatesthemsowecanbreakuplotsofsearchre
$articles=DB::table('articles')>where('title','LIKE','%'.$query.'%
//returnsaviewandpassestheviewthelistofarticlesandtheoriginal
returnview('page.search',compact('articles','query'));
}
Thatsallweneedforverysimplisticsearchfunctionality
butnowweneedtobeabletoseetheseresults.
CreatingourSearchbladeTemplate
Wevealreadysetupourroutessobutcurrentlyit
doesnthaveatemplateitcanusesowellimplement
thatnow.Createanewfolderwithinyour
resources/viewsfoldercalledpageandthencreate
https://tutorialedge.net/laravel5simplesitesearchbar
3/7
10/23/2016
Laravel5SimpleSiteSearchBar| TutorialEdge.net
search.blade.phpwithinthatfolder.Thiswillbethe
templatethatwillbeusedwheneverwewanttolistour
results.Imgoingtoassumeyouvegotan
app.blade.phpalreadyimplementedwithsectionsfor
content.
@if(count($articles)===0)
...htmlshowingnoarticlesfound
@elseif(count($articles)>=1)
...printoutresults
@foreach($articlesas$article)
printarticle
@endforeach
@endif
TutorialEdge.net
Search
Courses
Conclusion
Books
Blog
Tutorials
Register/SignInWithGithub
Thatshouldbeeverythingyouneedforasimplefullsite
searchwithlaravel5.IfyoufeelIvemissedanythingor
requirefurtherexplanationsthenpleaseletmeknowin
thecommentssectionbelow!
https://tutorialedge.net/laravel5simplesitesearchbar
4/7
10/23/2016
Laravel5SimpleSiteSearchBar| TutorialEdge.net
6Comments
tutorialedge.net
Share
Recommend 2
Login
SortbyBest
Jointhediscussion
TasminAfrozTannee9daysago
publicfunctionsearch(Request$request)
whydidyoudothis?Imeanyoudidnotuse$requestfarther.
Reply Share
ElliotForbes
Goodspot,Iguessit'sunnecessary.It'sredundantifyouwillbe
usingtheRequest::inputfunction.
TutorialEdge.net
Reply Share
Search
Courses
afonte
15daysago Books
Blog
Tutorials
Register/SignInWithGithub
Hello..Havetherebeenanyupdatestothis?Itriedusingthisbutcouldnot
getittowork.Anyhelpwouldbemuchappreciated.
Reply Share
ElliotForbes
canIaskwhatwasn'tworking?Bearinmindthisarticlewas
writtenusingLaravel5.2atthetime.
Reply Share
afonte>ElliotForbes 12daysago
Iamusingthelaravelcollectiveformyform.Iamalsousing
RouteResource...whenIaddedthe
Route::resource('queries','QueryController')itdidnot
recognizeit.ButIdidn'tmakeaseparateControllerfor
"QueryController".IwillmakeaQueryControllerandtestit
outagain.IonlyhaveonetablesoIwasoriginallyjusttrying
tocreateasearchfunctionbutcouldnotgetthattowork.I
amusingLaravel5.2.
Reply Share
ElliotForbes
Ok,soifyouareusingQueryControllerinyour
routesfilethenyouabsolutelyhavetocreatedthe
QueryControllerusingphpartisanmake:controller
QueryController.
https://tutorialedge.net/laravel5simplesitesearchbar
5/7
10/23/2016
Laravel5SimpleSiteSearchBar| TutorialEdge.net
Isuspectthisisthereasonyourcodeisfailing.
PostInfo:
Author:ElliotForbes
DatePosted:23Aug,2015
Comments:0
Views:7273
Tags:
TutorialEdge.net
Laravel5.2
ShareThisPost:
Search
Courses
Books
Blog
Tutorials
Register/SignInWithGithub
RegisterNow:
Registernowforfreequicklyusingyourpreferredsocial
mediaplatformandreceivethelatesttutorialsandupdates
straighttoyourinbox.
Github
ElliotForbes
SoftwareEngineer
23YearoldSoftwareEngineerwholovesallmannerof
frontendprograming.
FollowMe
https://tutorialedge.net/laravel5simplesitesearchbar
6/7
10/23/2016
Laravel5SimpleSiteSearchBar| TutorialEdge.net
Twitter
Facebook
Github
Youtube
TutorialEdge.net
TutorialEdge.net
Thissiteisdedicatedtobringingyouthehighestqualitycoursesdesigned
Search
Courses
Books
Blog
Tutorials
Register/SignInWithGithub
toteachyoutheinsandoutsofprogrammingwithvariousframeworks
andprogramminglanguages.
Links
AllTutorials
Books
Contact
Courses
Blog
About
TutorialEdge2016Copyright
https://tutorialedge.net/laravel5simplesitesearchbar
7/7