How to implement search function in Laravel 5.4
I'm new to Laravel and have some understanding problems how can I implement a search function to my app.
What I have tried so far.
I created a model that gets the data from the db (this is working)
class Post extends Model
{
public static function scopeSearch($query, $searchTerm)
{
return $query->where('city', 'like', '%' .$searchTerm. '%')
->orWhere('name', 'like', '%' .$searchTerm. '%');
}
}
My controller looks like this:
use AppPost;
class PostsController extends Controller
{
public function index(Request $request)
{
$searchTerm = $request->input('searchTerm');
$posts = Post::all()
->search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
}
public function show($id)
{
$post = Post::find($id);
return view('posts.show', compact('post'));
}
}
With php artisan tinker the model is working. I'm getting the db query.
On the front-end I have a simple input field with a submit button:
<div class="container">
<div class="col-sm-8">
<form action="posts.index" method="GET">
{{csrf_field()}}
<div class="input-group">
<input type="text" class="form-control" name="searchTerm" placeholder="Search for..." value="{{ isset($searchTerm) ? $searchTerm : '' }}">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Search</button>
</span>
</div>
</form>
</div>
</div>
But, now, how can I retrieve the data from the controller if I input something in the search input field? The code is only working when I get all the posts. I need to include somehow the serachTerm
from the controller
@extends('layouts.master')
@section('content')
<div class="col-sm-8 blog-main">
@foreach($posts as $post)
<div class="card" style="margin-bottom: 10px">
<div class="card-block">
<h3 class="card-title">{{ $post->Name1 }}</h3>
<small>Created at: {{ $post->created_at->toFormattedDateString() }}</small>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<a href="{{$post->id}}" class="btn btn-primary">Details</a>
</div>
</div>
@endforeach
</div>
@endsection
php laravel laravel-5.4
add a comment |
I'm new to Laravel and have some understanding problems how can I implement a search function to my app.
What I have tried so far.
I created a model that gets the data from the db (this is working)
class Post extends Model
{
public static function scopeSearch($query, $searchTerm)
{
return $query->where('city', 'like', '%' .$searchTerm. '%')
->orWhere('name', 'like', '%' .$searchTerm. '%');
}
}
My controller looks like this:
use AppPost;
class PostsController extends Controller
{
public function index(Request $request)
{
$searchTerm = $request->input('searchTerm');
$posts = Post::all()
->search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
}
public function show($id)
{
$post = Post::find($id);
return view('posts.show', compact('post'));
}
}
With php artisan tinker the model is working. I'm getting the db query.
On the front-end I have a simple input field with a submit button:
<div class="container">
<div class="col-sm-8">
<form action="posts.index" method="GET">
{{csrf_field()}}
<div class="input-group">
<input type="text" class="form-control" name="searchTerm" placeholder="Search for..." value="{{ isset($searchTerm) ? $searchTerm : '' }}">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Search</button>
</span>
</div>
</form>
</div>
</div>
But, now, how can I retrieve the data from the controller if I input something in the search input field? The code is only working when I get all the posts. I need to include somehow the serachTerm
from the controller
@extends('layouts.master')
@section('content')
<div class="col-sm-8 blog-main">
@foreach($posts as $post)
<div class="card" style="margin-bottom: 10px">
<div class="card-block">
<h3 class="card-title">{{ $post->Name1 }}</h3>
<small>Created at: {{ $post->created_at->toFormattedDateString() }}</small>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<a href="{{$post->id}}" class="btn btn-primary">Details</a>
</div>
</div>
@endforeach
</div>
@endsection
php laravel laravel-5.4
add a comment |
I'm new to Laravel and have some understanding problems how can I implement a search function to my app.
What I have tried so far.
I created a model that gets the data from the db (this is working)
class Post extends Model
{
public static function scopeSearch($query, $searchTerm)
{
return $query->where('city', 'like', '%' .$searchTerm. '%')
->orWhere('name', 'like', '%' .$searchTerm. '%');
}
}
My controller looks like this:
use AppPost;
class PostsController extends Controller
{
public function index(Request $request)
{
$searchTerm = $request->input('searchTerm');
$posts = Post::all()
->search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
}
public function show($id)
{
$post = Post::find($id);
return view('posts.show', compact('post'));
}
}
With php artisan tinker the model is working. I'm getting the db query.
On the front-end I have a simple input field with a submit button:
<div class="container">
<div class="col-sm-8">
<form action="posts.index" method="GET">
{{csrf_field()}}
<div class="input-group">
<input type="text" class="form-control" name="searchTerm" placeholder="Search for..." value="{{ isset($searchTerm) ? $searchTerm : '' }}">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Search</button>
</span>
</div>
</form>
</div>
</div>
But, now, how can I retrieve the data from the controller if I input something in the search input field? The code is only working when I get all the posts. I need to include somehow the serachTerm
from the controller
@extends('layouts.master')
@section('content')
<div class="col-sm-8 blog-main">
@foreach($posts as $post)
<div class="card" style="margin-bottom: 10px">
<div class="card-block">
<h3 class="card-title">{{ $post->Name1 }}</h3>
<small>Created at: {{ $post->created_at->toFormattedDateString() }}</small>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<a href="{{$post->id}}" class="btn btn-primary">Details</a>
</div>
</div>
@endforeach
</div>
@endsection
php laravel laravel-5.4
I'm new to Laravel and have some understanding problems how can I implement a search function to my app.
What I have tried so far.
I created a model that gets the data from the db (this is working)
class Post extends Model
{
public static function scopeSearch($query, $searchTerm)
{
return $query->where('city', 'like', '%' .$searchTerm. '%')
->orWhere('name', 'like', '%' .$searchTerm. '%');
}
}
My controller looks like this:
use AppPost;
class PostsController extends Controller
{
public function index(Request $request)
{
$searchTerm = $request->input('searchTerm');
$posts = Post::all()
->search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
}
public function show($id)
{
$post = Post::find($id);
return view('posts.show', compact('post'));
}
}
With php artisan tinker the model is working. I'm getting the db query.
On the front-end I have a simple input field with a submit button:
<div class="container">
<div class="col-sm-8">
<form action="posts.index" method="GET">
{{csrf_field()}}
<div class="input-group">
<input type="text" class="form-control" name="searchTerm" placeholder="Search for..." value="{{ isset($searchTerm) ? $searchTerm : '' }}">
<span class="input-group-btn">
<button class="btn btn-secondary" type="submit">Search</button>
</span>
</div>
</form>
</div>
</div>
But, now, how can I retrieve the data from the controller if I input something in the search input field? The code is only working when I get all the posts. I need to include somehow the serachTerm
from the controller
@extends('layouts.master')
@section('content')
<div class="col-sm-8 blog-main">
@foreach($posts as $post)
<div class="card" style="margin-bottom: 10px">
<div class="card-block">
<h3 class="card-title">{{ $post->Name1 }}</h3>
<small>Created at: {{ $post->created_at->toFormattedDateString() }}</small>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<a href="{{$post->id}}" class="btn btn-primary">Details</a>
</div>
</div>
@endforeach
</div>
@endsection
php laravel laravel-5.4
php laravel laravel-5.4
edited Nov 12 at 16:25
Greenonline
1,01821424
1,01821424
asked Aug 22 '17 at 10:24
Greg Ostry
403319
403319
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Check this:
$posts = Post::search($searchTerm)->get();
Just to expand on this when you doPost::all()
it executes the query immediately so the search query scope doesn't get applied. So apply the scope like in the answer here then you have to callget()
to actually execute the query and get the results.
– jfadich
Aug 22 '17 at 22:11
Make sure to add larqavel/scout to your project and add the trait to the model before using search()
– Athul Raj
Dec 22 '17 at 13:37
add a comment |
Firstly you need to install laravel scout
Use: composer require laravel/scout
Next you need to publish the Scout configuration using the vendor:publish
command. This command will publish the scout.php
to your config directory.
Here is the command: php artisan vendor:publish --provider="LaravelScoutScoutServiceProvider"
Last step would be LaravelScoutSearchable
trait to the model you would like to make searchable.
Then its simple for use as: NameOfModel::search($searchText)->get();
add a comment |
You are trying to get all record and then applying your scope function, rather then get all records just try:
$searchTerm = $request->input('searchTerm');
$posts = Post::search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
add a comment |
Laravel makes it really easy to do searches, as well as custom searches like live results, and etc. Just make sure to throttle your HTTP requests on the front end.
/**
* Get a breed by name (returns similar results)
* @param $term
* @return IlluminateHttpJsonResponse
*/
public function getBreedByName($term)
{
// Find an Breed by name
$matchedTerm = Breed::where('text', 'LIKE', '%' . $term . '%')->get();
return response()->json($matchedTerm);
}
Then create a reference to that controller and method in your routes. In my case, I used the following in /routes/api.php
Route::get('/breeds/search/{term}', 'BreedsController@getBreedByName');
I hope this is helpful! Let me know if you have any questions or if this didn't cover your question.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45815095%2fhow-to-implement-search-function-in-laravel-5-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check this:
$posts = Post::search($searchTerm)->get();
Just to expand on this when you doPost::all()
it executes the query immediately so the search query scope doesn't get applied. So apply the scope like in the answer here then you have to callget()
to actually execute the query and get the results.
– jfadich
Aug 22 '17 at 22:11
Make sure to add larqavel/scout to your project and add the trait to the model before using search()
– Athul Raj
Dec 22 '17 at 13:37
add a comment |
Check this:
$posts = Post::search($searchTerm)->get();
Just to expand on this when you doPost::all()
it executes the query immediately so the search query scope doesn't get applied. So apply the scope like in the answer here then you have to callget()
to actually execute the query and get the results.
– jfadich
Aug 22 '17 at 22:11
Make sure to add larqavel/scout to your project and add the trait to the model before using search()
– Athul Raj
Dec 22 '17 at 13:37
add a comment |
Check this:
$posts = Post::search($searchTerm)->get();
Check this:
$posts = Post::search($searchTerm)->get();
answered Aug 22 '17 at 10:35
wolacinio
728
728
Just to expand on this when you doPost::all()
it executes the query immediately so the search query scope doesn't get applied. So apply the scope like in the answer here then you have to callget()
to actually execute the query and get the results.
– jfadich
Aug 22 '17 at 22:11
Make sure to add larqavel/scout to your project and add the trait to the model before using search()
– Athul Raj
Dec 22 '17 at 13:37
add a comment |
Just to expand on this when you doPost::all()
it executes the query immediately so the search query scope doesn't get applied. So apply the scope like in the answer here then you have to callget()
to actually execute the query and get the results.
– jfadich
Aug 22 '17 at 22:11
Make sure to add larqavel/scout to your project and add the trait to the model before using search()
– Athul Raj
Dec 22 '17 at 13:37
Just to expand on this when you do
Post::all()
it executes the query immediately so the search query scope doesn't get applied. So apply the scope like in the answer here then you have to call get()
to actually execute the query and get the results.– jfadich
Aug 22 '17 at 22:11
Just to expand on this when you do
Post::all()
it executes the query immediately so the search query scope doesn't get applied. So apply the scope like in the answer here then you have to call get()
to actually execute the query and get the results.– jfadich
Aug 22 '17 at 22:11
Make sure to add larqavel/scout to your project and add the trait to the model before using search()
– Athul Raj
Dec 22 '17 at 13:37
Make sure to add larqavel/scout to your project and add the trait to the model before using search()
– Athul Raj
Dec 22 '17 at 13:37
add a comment |
Firstly you need to install laravel scout
Use: composer require laravel/scout
Next you need to publish the Scout configuration using the vendor:publish
command. This command will publish the scout.php
to your config directory.
Here is the command: php artisan vendor:publish --provider="LaravelScoutScoutServiceProvider"
Last step would be LaravelScoutSearchable
trait to the model you would like to make searchable.
Then its simple for use as: NameOfModel::search($searchText)->get();
add a comment |
Firstly you need to install laravel scout
Use: composer require laravel/scout
Next you need to publish the Scout configuration using the vendor:publish
command. This command will publish the scout.php
to your config directory.
Here is the command: php artisan vendor:publish --provider="LaravelScoutScoutServiceProvider"
Last step would be LaravelScoutSearchable
trait to the model you would like to make searchable.
Then its simple for use as: NameOfModel::search($searchText)->get();
add a comment |
Firstly you need to install laravel scout
Use: composer require laravel/scout
Next you need to publish the Scout configuration using the vendor:publish
command. This command will publish the scout.php
to your config directory.
Here is the command: php artisan vendor:publish --provider="LaravelScoutScoutServiceProvider"
Last step would be LaravelScoutSearchable
trait to the model you would like to make searchable.
Then its simple for use as: NameOfModel::search($searchText)->get();
Firstly you need to install laravel scout
Use: composer require laravel/scout
Next you need to publish the Scout configuration using the vendor:publish
command. This command will publish the scout.php
to your config directory.
Here is the command: php artisan vendor:publish --provider="LaravelScoutScoutServiceProvider"
Last step would be LaravelScoutSearchable
trait to the model you would like to make searchable.
Then its simple for use as: NameOfModel::search($searchText)->get();
answered Jul 3 at 15:23
raBne
1,68512032
1,68512032
add a comment |
add a comment |
You are trying to get all record and then applying your scope function, rather then get all records just try:
$searchTerm = $request->input('searchTerm');
$posts = Post::search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
add a comment |
You are trying to get all record and then applying your scope function, rather then get all records just try:
$searchTerm = $request->input('searchTerm');
$posts = Post::search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
add a comment |
You are trying to get all record and then applying your scope function, rather then get all records just try:
$searchTerm = $request->input('searchTerm');
$posts = Post::search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
You are trying to get all record and then applying your scope function, rather then get all records just try:
$searchTerm = $request->input('searchTerm');
$posts = Post::search($searchTerm);
return view('posts.index', compact('posts', 'searchTerm'));
edited Aug 22 '17 at 13:28
MisaGH
537417
537417
answered Aug 22 '17 at 12:49
Henz Jason
184
184
add a comment |
add a comment |
Laravel makes it really easy to do searches, as well as custom searches like live results, and etc. Just make sure to throttle your HTTP requests on the front end.
/**
* Get a breed by name (returns similar results)
* @param $term
* @return IlluminateHttpJsonResponse
*/
public function getBreedByName($term)
{
// Find an Breed by name
$matchedTerm = Breed::where('text', 'LIKE', '%' . $term . '%')->get();
return response()->json($matchedTerm);
}
Then create a reference to that controller and method in your routes. In my case, I used the following in /routes/api.php
Route::get('/breeds/search/{term}', 'BreedsController@getBreedByName');
I hope this is helpful! Let me know if you have any questions or if this didn't cover your question.
add a comment |
Laravel makes it really easy to do searches, as well as custom searches like live results, and etc. Just make sure to throttle your HTTP requests on the front end.
/**
* Get a breed by name (returns similar results)
* @param $term
* @return IlluminateHttpJsonResponse
*/
public function getBreedByName($term)
{
// Find an Breed by name
$matchedTerm = Breed::where('text', 'LIKE', '%' . $term . '%')->get();
return response()->json($matchedTerm);
}
Then create a reference to that controller and method in your routes. In my case, I used the following in /routes/api.php
Route::get('/breeds/search/{term}', 'BreedsController@getBreedByName');
I hope this is helpful! Let me know if you have any questions or if this didn't cover your question.
add a comment |
Laravel makes it really easy to do searches, as well as custom searches like live results, and etc. Just make sure to throttle your HTTP requests on the front end.
/**
* Get a breed by name (returns similar results)
* @param $term
* @return IlluminateHttpJsonResponse
*/
public function getBreedByName($term)
{
// Find an Breed by name
$matchedTerm = Breed::where('text', 'LIKE', '%' . $term . '%')->get();
return response()->json($matchedTerm);
}
Then create a reference to that controller and method in your routes. In my case, I used the following in /routes/api.php
Route::get('/breeds/search/{term}', 'BreedsController@getBreedByName');
I hope this is helpful! Let me know if you have any questions or if this didn't cover your question.
Laravel makes it really easy to do searches, as well as custom searches like live results, and etc. Just make sure to throttle your HTTP requests on the front end.
/**
* Get a breed by name (returns similar results)
* @param $term
* @return IlluminateHttpJsonResponse
*/
public function getBreedByName($term)
{
// Find an Breed by name
$matchedTerm = Breed::where('text', 'LIKE', '%' . $term . '%')->get();
return response()->json($matchedTerm);
}
Then create a reference to that controller and method in your routes. In my case, I used the following in /routes/api.php
Route::get('/breeds/search/{term}', 'BreedsController@getBreedByName');
I hope this is helpful! Let me know if you have any questions or if this didn't cover your question.
answered Aug 22 '17 at 22:04
Adriano Michael
564
564
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45815095%2fhow-to-implement-search-function-in-laravel-5-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown