Laravel Search function - php

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') }}" ..>

Related

404 not found error, can't redirect to another page/view

I am having a problem with the routing.
Firstly, I am on the page (rate product)/{$id}
Next, when clicking the button, I want to go to another view (addReview/{$productID}/{$clientID}) But it gives me an error.
-web.php file
Route::get('rateProduct/{id}', 'CatalogController#getProduct')
->name('rateProduct');
Route::post('./addReview/{$productID}/{$clientID}','ReviewController#addReview')
->name('rateProduct.addReview');
add_review.blade.php
<div class="mb-3">
<form method="post" action="{{url('rateProduct.addReview', [$product->id, 10]) }} value="{{ csrf_token() }}"">
{{--{{ csrf_field() }}--}}
<label for="exampleFormControlTextarea1" class="form-label">Share your thoughts!</label>
<textarea class="form-control rounded" id="exampleFormControlTextarea1" rows="3"
placeholder="Tell us about your experiecnce in a couple of sentences" name="comment"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
ReviewController.php
class ReviewController extends Controller
{
public function addReview($productID, $clientID)
{
if(isset( $_POST['submit'])) {
$comment = $_POST["comment"];
}
$date = date_create();
$reviewID = DB::table('review')->max('id') + 1;
$data = array(
'id'=>$reviewID,
"comment"=>$comment,
"review_date"=>date_timestamp_get($date),
"rating"=>1,
"client_id"=>$clientID
);
DB::table('review')->insert($data);
$data2 = array('product_id'=>$productID,"review_id"=>$reviewID);
DB::table('product_review')->insert($data2);
return view('pages/product-page');
}
}
Review model
class Review extends Model
{
use HasFactory;
public $timestamps = false;
protected $table = 'Review';
public function owner() {
return $this->belongsTo('App\Models\Product');
}
}
You have error in routing .Remove dot from the beginning(./addReview/{$productID}/{$clientID}) of string in post method.
Route::post('addReview/{$productID}/{$clientID}','ReviewController#addReview')->name('rateProduct.addReview');
Also you are using url method for named routing in form action it should be
<form method="post" action="{{route('rateProduct.addReview', [$product->id, 10]) }}" >
#csrf
Firstly you have a random . at the start of your route and $ symbols in your parameters, they needs to go.
Route::post('/addReview/{productID}/{clientID}','ReviewController#addReview')
->name('rateProduct.addReview');
Next you need to fix the invalid markup of your add_review blade file, you have mismatched opening/closing elements. You are also using a named route but using the url helper which works with paths rather than route names. You also want to add back in the #csrf token back in otherwise you'll get a 419 error.
<div class="mb-3">
<form method="post"
action="{{ route('rateProduct.addReview', [$product->id, 10]) }}">
#csrf
<label for="exampleFormControlTextarea1" class="form-label">
Share your thoughts!
</label>
<textarea class="form-control rounded"
id="exampleFormControlTextarea1"
rows="3"
placeholder="Tell us about your experiecnce in a couple of sentences"
name="comment">
</textarea>
<button type="submit" class="btn btn-primary">Send</button>
</form>
</div>
You can see a working example here.

laravel compact data wont read in home.blade.php

here is my code form my controller
i9ts seem to Image::get() not working when I use image::all() then its work
public function index(){
$images = Image::get();
return view('home', compact('images'));
}
here is my home.blade.php
<div class="container">
<div class="row justify-content-center">
<form action="{{route('ablum.store')}}" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="image" class="form-control">
<button class="btn btn-primary" type="submit">submit</button>
</form>
#foreach($images as $image)
<p> this is image name {{$image->name}}</p>
#endforeach
</div>
</div>
when I navigate to the home view I get this error
$images is undefined
Make the variable optional in the blade template. Replace {{ $images }} with {{ $images ?? '' }}
how can I solve this ?
You can try and rewrite your controller code like this:
return view('home')->with(compact('images'));
Official Laravel docs here does not say anything about any second parameters to view() helper.
with() should actually append the data and $images should be available in blade now.
Also, for simplicity, just use Image::all(). If this again is not working, you can try and ditch the compact method and in with() send an array like:
return view('home')->with(['images' => $images]);

The PUT method is not supported for this route. Supported methods: GET, HEAD, POST, DELETE. (note I am using model biding)

I am using model biding, but I am basically trying to create a form that can edit and update comments. It runs perfectly and even prompts me to edit the form. However, every time I try to update it the POST method is not supported for the root. which is bogus as I did the spoofing correctly, someone help me please.
Here us my CommentsController methods
public function edit($id)
{
$comment = Commenting::find($id);
return view('comments.edit', compact('comment'));
}
public function update(Request $request, $id, $posts)
{
$comment = Commenting::find($id);
$comment->update($request->all());
$comment->posts_id = $posts;
return view('post.show', compact('comment'));
}
Here is the routing in my web.php
Route::get('/posts/comment/{comment}', 'CommentsController#edit')->name('comments.edit')->middleware('auth');
Route::put('/posts/comment/{comment}', 'CommentsController#update')->name('comments.update')->middleware('auth');
Here's what I call the link to edit the form in my show.blade
Edit
lastly this is my edit.blade file
#extends ('layouts.home')
#section ('content')
<div class="card">
<h1> Edit Comment </h1>
<div class="card-block">
<form method="POST" action="{{route('comments.update', ['comments' => $comment])}}">
#csrf
#method('PUT')
<div class="form-group">
<textarea name="body" placeholder="Enter you comment here..." class="form-control"> {{$comment->body}}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#include ('layouts.errors')
</div>
</div>
#endsection
Take a look your update method
public function update(Request $request, $id, $posts)
Try to delete the $posts variable on update function
Try
{{method_field('put')}}
or
<input type="hidden" name="_method" value="PUT">
add patch method in blade file:
#extends ('layouts.home')
#section ('content')
<div class="card">
<h1> Edit Comment </h1>
<div class="card-block">
<form method="POST" action="{{route('comments.update', ['comments' => $comment])}}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
<div class="form-group">
<textarea name="body" placeholder="Enter you comment here..." class="form-control"> {{$comment->body}}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#include ('layouts.errors')
</div>
</div>
#endsection
write post method in route file :
Route::post('/posts/comment/{comment}', 'CommentsController#update')->name('comments.update')->middleware('auth');
Your route binding is named comment not comments. This will not generate a URL to your comments.update route:
route('comments.update', ['comments' => $comment])
This will generate a URL to /posts/comment?comments=3 not /posts/comment/3. You want to use the same name as the route parameter so it knows to replace that route parameter:
route('comments.update', ['comment' => $comment])
Which would generate a URL to /posts/comment/3.
Also your controller method
public function update(Request $request, $id, $posts)
has a $posts variable but there is no parameter for that defined for the route, so where does that come from?
If you actually want to use Model Binding, which it would appear you are not, you could use Implicit Bindings by type-hinting a $comment argument on that controller method:
public function update(Request $request, Comment $comment)
as the route pararemeter is named comment not id.
Laravel 5.8 - Routing - Route Model Binding - Implicit Binding

Laravel: Two Actions in a Form - one with "on-change" one with a button?

in my Laravel App I have a Blade "Insert Project" where I want to dynamic load html data from Database when a DropDown changes. To be more concrete: I have several Project Categories in the Table "Cats" (id, name, code) - Each Category needs different fields to be added, therefore I have put the html code in the Table "Cats". I want to achieve the following:
If a user selects a Project Category the html code which is attached to the category should be shown.
My Code so far:
<form method="POST" action="{{ route('project-insert') }}">
#csrf
<form action="{{ route('filter') }}" method="post">
<label for="Cat">Kategorie</label>
<select class="form-control" name="cat_id" id="cat_id" data-parsley-required="true" onchange='this.form.submit()'>
#foreach ($cats as $sn)
{
<option value="{{ $sn->id }}">{{ $sn->name }}</option>
}
#endforeach
</select>
</form>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Add Project') }}
</button>
</div>
</div>
</form>
In my Controller I have add:
public function insertProject(Request $data) {
return $data['cat_id'];
}
public function filter(Request $request)
{
$cat_id = $request->input("cat_id");
return $cat_id;
$code = DB::table('cats')->where('id', '=', $cat_id);
var_dump($code->code);
}
When I change the DropDown, then the function "insertProject" will be called and not the desired function "filter".
How can I achieve it to call the correct function, or how can I achieve it to call "insertProject" only when the button has been pressed?
Thank you for your advice and help.
Stefan

Search and Pagination Laravel 4

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.

Categories