Search and Pagination Laravel 4 - php

I have a problem, I want to use the form to do a search and use pagination. The problem is when I change the page, an error occurs:
No query results for model [Aluno].
My controller
Route::post(
'alunos/busca',
array(
'as' => 'alunos.busca',
'uses' => 'AlunosController#busca'
)
);
My Controller
public function busca()
{
$search = Input::get('q');
$alunos = Aluno::where('curso', $search)->paginate(1);
return View::make('alunos.index', compact('alunos', 'search'));
}
My view:
<form method="post" action="{{ URL::to('alunos/busca') }}">
<input class="input-xxlarge" name="q" type="text" placeholder="Search...">
<div class="control-group">
<div class="controls">
<input type="submit" class="btn" id="submit" value="Submit" />
</div>
</div>
<div class="divPagination">
{{ $alunos->links() }}
</div>
I need help..
Sorry my English

I think the best solution is the next one:
Copy and paste this form wherever you want it:
{{Form::open(array('url' => 'alunos/busca','method' => 'get'))}}
{{Form::text('q', null , array('placeholder' => 'Search...','class' => 'input-xxlarge'))}}
{{Form::submit('SEARCH',array('class' => 'btn','id' => 'submit'))}}
{{Form::close()}}
In routes.php :
Route::get('alunos/busca', function() {
$search = Input::get('q');
$alunos = Aluno::where('curso', 'LIKE', '%'.$search.'%')->paginate(15);
return View::make('alunos.index')
->with('alunos', $alunos);
});
Now, in your view app/views/alunos/index.blade.php :
#if(!$alunos->isEmpty())
#foreach($alunos as $aluno)
{{$aluno->subject}}
#endforeach
{{$alunos->links()}}
#else
<h1> No results </h1>
#endif
Change it as you wish and enjoy

The problem is when you click on the next page it has no memory of the search param you just entered, essentially $q = null; which will cause the error.

Related

Update article laravel

Controller article
public function update(Request $request, Article $article){
$article->update($request->except('slug', 'image_path'));
if ($request->hasFile('image_path')) {
$image = $request->file('image_path');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$article->image_path = $new_name;
$article->save();
}
$article->categories()->detach();
if ($request->has('categories')) {
$article->categories()->attach($request->input('categories'));
}
$user=auth()->user();
return redirect()->back()->with('message', 'Raksts atjaunots!');
}
public function edit(Article $article){
$user=auth()->user();
return view('edit',[
'article' => $article,
'categories' => Category::with('children')->where('parent_id',0)->get(),
'delimiter' => ''
]);
}
edit blade
<form class="form-horizontal" action="{{route('update', $article)}}" method="post" enctype="multipart/form-data" style="display: flex;justify-content: center;flex-direction: column;">
<input type="hidden" name="_method" value="put">
{{ csrf_field() }}
{{-- Form include --}}
<img src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
#include('partials.form')
<input type="hidden" name="modified_by" value="{{Auth::id()}}">
</form>
link to the form
<tbody>
#foreach ($articles_suggest_user as $article)
<a class="btn btn-default" href="{{route('edit', $article)}}"><i class="fa fa-edit"></i></a>
<?php } ?>
#endforeach
</tbody>
web.php
Route::get('/home/edit/{id}','Controller_Article_parents#edit', function () {
return view('/home/edit');
})->name('edit');
Route::get('/home/update/','Controller_Article_parents#update', function () {
return view('/home');
})->name('update');
When i clicked the link, i move to for example this url http://127.0.0.1:8000/home/edit/190
But my form is empty... input empty. How I can do when I open form it's display me input information ?
When click my link its display me form, but form is empty.
form example
<div class="rtq">
<label for="parent_id">Cat</label>
<span class="required">*</span>
<select
id="parent_id"
class="form-control"
name="categories[]"
multiple=""
required
>
#include('admin.categories.partials.categories',
['categories' => $categories,
'current' => $article,
'delimiter' => $delimiter])
</select>
</div>
<div class="rtq">
<label for="">Descript</label>
<textarea
name="description_short"
class="form-control"
id="description_short"
>
{{ $article->description_short ?? "" }}
</textarea>
</div>
It my form . Mini example
If you want to redirect with form input, you can use the withInput() function
return back()->withInput();
https://laravel.com/docs/7.x/redirects#creating-redirects
Now, this function is designed to be used with form validation and may not be what you want.
What I have done in the past when combining create & edit views into a single template is something like this:
{!! Form::text('name', isset($model) ? $model->name : null, [
'class' => 'form-control',
'placeholder' => 'Please fill out this field &hellips;',
'required' => true
]) !!}
This uses the Laravel Collective HTML library. However the principle is the same if you are using raw hmtl like so:
<input type="text" value="{{ isset($model) ? $model->name : null }}">
Round 2 Electric Boogaloo
You aren't returning a variable called $article to your edit.blade.php.
Round 3 Apple Tree
Originally, you appeared to be calling both a controller action AND also a callback function. Stick to the controller action only like so:
Route::get('/home/edit/{id}','Controller_Article_parents#edit')->name('edit');
Then within your edit() function on the Controller you will want to do this:
public function edit ($id) {
return view('/home/edit', [
'article' => Article::find($id)
]);
}

Laravel Search function

i have a question about the Laravel search function, i had follow the guildeline online and i still fail to search the category, can someone guide me and tell me where i did wrongly ? Much appreciated
My category Controller php code:
public function search(Request $request)
{
$search = $request->get('search');
$posts = DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['posts' => $posts]);
}
My index.blade code
<div align="left">
<div class="col-md-4">
<h1>Policy</h1>
</div>
<div class="col-md-4">
<form action="/search" method="get" role="search">
{{ csrf_field() }}
<div class="input-group">
<input type="text" class="form-control" name="_method" placeholder="Search ID / Code"> <span class="input-group-btn">
<button type="submit" class="btn btn-primary">Search</button></span>
</div>
</form>
</div>
</div>
web.php
Route::get('/search','categoryController#search');
What error i get is here
Error image
interface
Database
You are sending $posts variable to your view. But the error says you are referencing a $category variable.
return view('category.index',['posts' => $posts]);
Maybe you might want to update view to use $posts. If you could post your full code (category/index.blade.php) we might be able to help you better.
__
Here is how I would do:
$categories= DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['categories' => $categories]); //you can also use compact return view('category.index', compact('categories') );
And to display:
#foreach( $categories as $category )
<div>{{ $category->id }}</div>
#endforeach
Another tip: you can name your routes like so
Route::get('search','categoryController#search')->name('search');
Then you can reference this route (in form or anywhere else you want) like so:
<form action="{{ route('search') }}" ..>

Parameter goes into array as the name of the field instead of the value

What am I trying to achieve?
I have "tasks" and each task can have multiple "notes", so when you select a Task and click notes, it takes you to a page with all the notes for the task in which you clicked.
Each note has a field called "task_id", so my problem is passing this task_id to the note.
I'm trying to pass it like this on the notes form:
<form method="POST" action="{{route('notes.store',$task)}}">
#include('notes.form')
</form>
And it goes into my controller
public function store(Request $r)
{
$validatedData = $r->validate([
'note' => 'required',
]);
$r['created_by'] = Auth::user()->user_id;
return $r;
/*
$note = Note::create($r->all());
return redirect('/notes')->with('store');
*/
}
But I return it to see how its going and I get this:
{"_token":"OmGrbYeQDl35oRnmewrVraCT0SHMC16wE4gD56nl","note":"363","created_by":4,"8":null}
That 8 at the end is actually the correct task id, but it appears as the name instead of the value.
What may be causing this?
This is my form view:
#csrf
<div class="col">
<div class="form-group">
<input type="text" class="form-control" name="note">
</div>
</div>
<div class="col-10">
<div class="form-group">
<button class="btn btn-success" type="submit">Add note</button>
<br><br>
</div>
</div>
These are my routes:
Route::get('/tasks/{task}/notes', ['as' => 'tasks.notes', 'uses' => 'NoteController#index']);
Route::get('/projects/{project}/tasks', ['as' => 'projects.tasks', 'uses' => 'ProjectController#seeTasks']);
Route::get('/projects/results','ProjectController#filter');
Route::get('/tasks/results','TaskController#filter');
Route::resource('projects','ProjectController');
Route::resource('clients','ClientController');
Route::resource('tasks','TaskController');
Route::resource('users','UserController');
Route::resource('notes','NoteController');
You are trying to pass the task_id as a route parameter, but your notes.store route has no route parameters.
Verb Path Action Route Name
POST /notes store notes.store
Adding the task_id as a hidden input should properly send it with the request:
<form method="POST" action="{{ route('notes.store') }}">
<input type="hidden" name="task_id" value="{{ $task->id }}">
#include('notes.form')
</form>

$request->get() not getting the form value when updating data in laravel 5.4

I am trying to update just one column from'business_account' table. I tried something like bellow, When trying to get the form value to my 'packPurchasedMembers' controller function I am getting null value. What should be the right code to getting expected result.
Route::group(['prefix' => 'super', 'middleware' => 'super', 'as' => 'super.'], function () {
Route::post('verify-account', array('as' => 'verify-account', 'uses' => 'packPurchasedMembers#postVerifyAccount'));});
My Controller-
public function postVerifyAccount(Request $request){
$uid = $request->get('userid');
$verfiy = $request->get('verification');
DB::table('business_account')
->where('user_id', $uid)
->update(['verified' => $verfiy]);}
My Form -
<div class="pull-left">
<h4>Verify Account</h4>
#foreach ($verification as $verify)
<form action="{{ url('super/verify-account') }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="userid" value="<?php echo $uid; ?>" />
<input type="radio" id="reinv1" name="verification" value="0"
<?php if ($verify->verified == '0') echo 'checked' ?> >
<label for="reinv1"> Not Verified</label>
<input type="radio" id="reinv2" name="verification" value="1"
<?php if ($verify->verified == '1') echo 'checked' ?> >
<label for="reinv2"> Verified</label>
<button type="submit" class="btn btn-success" value="Submit">Submit</button>
</form>
#endforeach
</div>
Please use the following function to retrieve input value
public function postVerifyAccount(Request $request){
$uid = $request->input('userid');
$verfiy = $request->input('verification');
DB::table('business_account')
->where('user_id', $uid)
->update(['verified' => $verfiy]);
}
Just in case if anyone lands up to this question. I would like to address the solution for the above question.
So the problem was he had not used the proper HTTP request. Instead of using POST request he was using GET request.
In routes/web.php
The request method is changed from GET -> POST.
Hope it may help someone.

Adding input query to route value for search function Laravel Scout

Trying to set up basic search functionality for products. I am having trouble sorting the route parameter variable and passing the query string to the search function.
Route::get('/search/{query?}', 'ProductController#searchable');
This works and returns a query when I input the query manually.
Controller
public function searchable($query)
{
// search database, with result, list on page, with links to products,
$products = Product::search($query)->get();
return view('search.index', compact('products'));
}
However, I would like it to come from the URL /search?test.
My form shows:
{{ Form::open(array('action' => 'ProductController#searchable', 'method' => 'get', 'files' => 'false')) }}
<input type="search" name="search" placeholder="type keyword(s) here" />
<button type="submit" class="btn btn-primary">Search</button>
{{ Form::close() }}`
I am new to Laravel and need a little help. I am using Laravel Scout and TNTSearch.
You don't need to user {wildcard} for searching. We have Request for that
Route::get('search', 'ProductController#searchable');
Pass the url instead.
{{ Form::open(array('url' => 'search', 'method' => 'GET', 'files' => 'false')) }}
<input type="search" name="search" placeholder="type keyword(s) here" />
<button type="submit" class="btn btn-primary">Search</button>
{{ Form::close() }}
In Controller simple fetch $request->search
public function searchable(Request $request)
{
// search database, with result, list on page, with links to products,
$products = Product::search($request->search)->get();
return view('search.index', compact('products'));
}

Categories