Search in Laravel using routes instead of controllers - php

This is my Laravel route where I am getting value from as 'q' from a form to perform search from quiz table.
Route::get('/admin/Searchquizzes',function ($q) {
$quizzes = DB::table('quiz')
->leftjoin('category', 'quiz.category_id', '=', 'category.id')
->where('name','LIKE','$q')
->select('quiz.*', 'category.name As category_name')
->get();
$categories = DB::table('category')->select('id', 'name')->get();
return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);
)->middleware('auth')->name('admin.Searchquizzes');
This is code for my laravel Form to perform search ..
<div class="input-group">
<input type="text" name="q" id="q" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
This is not working I don't know why, help shall be appreciated.

Fixed it by my self :)
Route::get('/admin/Searchquizzes',function (Request $request) {
//dd($request->q);
$quizzes = DB::table('quiz')
->leftjoin('category', 'quiz.category_id', '=', 'category.id')
->where('quiz.name','LIKE',$request->q)
->select('quiz.*', 'category.name As category_name')
->get();
$categories = DB::table('category')->select('id', 'name')->get();
return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);
})->middleware('auth')->name('admin.Searchquizzes');
Here is my form , Now i can search quizes :)
<form method="get" action="{{route('admin.Searchquizzes')}}">
{{ csrf_field() }}
<div class="input-group">
<input type="text" name="q" id="q" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>

YOu won't get that form value. You are not using POST method
<form method="POST" action="{{route('admin.Searchquizzes')">
{{ csrf_field() }}
<div class="input-group">
<input type="text" name="q" id="q" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
Route :
use Illuminate\Http\Request;
Route::POST('/admin/Searchquizzes',function (Request $request) {
//dd($request->q);
$quizzes = DB::table('quiz')
->leftjoin('category', 'quiz.category_id', '=', 'category.id')
->where('name','LIKE','$q')
->select('quiz.*', 'category.name As category_name')
->get();
$categories = DB::table('category')->select('id', 'name')->get();
return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);
})->middleware('auth')->name('admin.Searchquizzes');

Related

Why my controller cannot get request from view?

I want to insert some data to database, I need id that I can get from my route and quantity. But my quantity keep define as null.
This is my view
<form method="POST" action="{{ url('beli/'.$produk->id) }}" enctype="multipart/form-data" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="exampleInputEmail1">Jumlah Beli :</label>
<input type="name" class="form-control" name ="quantity" id="quantity" placeholder="Jumlah">
</div>
<div class="mt-4">
<div class="btn btn-default btn-lg btn-flat">
<i class=" fa-lg mr-2"></i> <a href="{{url('/beli/'.$produk->id)}}">
Beli Produk </a>
</div>
</form>
and this is my Controller
public function tambah(Request $request, $id)
{
$idtoko = Store::select('id')->where('users_id',Auth::user()->id)->first();
$transaksi = new Transaction();
$transaksi->quantity = $request->get('quantity');
$transaksi->stores_id = $idtoko->id;
$transaksi->save();
}
And this is my route
Route::get('/beli/{id}','KeranjangController#tambah');

SQLSTATE[23502]: Not null violation: 7 ERROR when update comment in laravel

I want update a comment in Laravel.
<div class="edit-input" id="edit{{$comment->id}}">
<input type="text" name="edit_comment" class="form-control">
<div class="input-group-append">
OK
<button class="btn btn-danger" id="editCancel" type="button">Cancel</button>
</div>
</div>
This my route:
Route::get('review-edit/{id}', 'CommentController#editComment')->name('review-edit');
And CommentController:
public function editComment(Request $request, $id)
{
$updateComment = Comment::findOrFail($id);
$updateComment->user_id = Auth::id();
$updateComment->comment = $request->edit_comment;
$updateComment->save();
return back();
}
When I try update comment, I'm getting an error telling me
SQLSTATE[23502]: Not null violation: 7
dd($request->edit_comment) gives null also. What am I overlooking here?
Try this
you edit_comment should be inside form then only you can send data to controller
<form action="{{ route('review-edit', [ 'id' => $comment->id]) }}" method="get">
<div class="edit-input" id="edit{{$comment->id}}">
<input type="text" name="edit_comment" class="form-control">
<div class="input-group-append">
<button class="btn btn-info" type="submit">OK</button>
<button class="btn btn-danger" id="editCancel" type="button">Cancel</button>
</div>
</div>
</form>
try this and dd($request->edit_comment);
<form action="{{ route('review-edit', [ 'id' => $comment->id]) }}" method="get">
<div class="edit-input" id="edit{{$comment->id}}">
<input type="text" name="edit_comment" class="form-control">
<div class="input-group-append">
<button class="btn btn-info" type="submit">OK</button>
<button class="btn btn-danger" id="editCancel" type="button">Cancel</button>
</div>
</div>
</form>

How to implement search functionality in laravel with multiple input fields in laravel

I am making a online ticketing service using laravel and vue.js. I want to implement search functionality in my website with multiple fields. eg: flight destination, arrival, date. I dont know how to implement it in laravel. I've watch several videos but it doesnt work for me. Can someone know what should i do?
Heres my output
index.blade.php
<h2 class="text-4 mb-3">Book Domestic and International Flights</h2>
<form id="bookingFlight" action="{{url("/flightSearch")}}">
{{ csrf_field() }}
<div class="form-row">
<div class="col-md-8 col-lg-3 form-group">
<input type="text" class="form-control" id="flightFrom" required placeholder="From">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span> </div>
<div class="col-md-8 col-lg-3 form-group">
<input type="text" class="form-control" id="flightTo" required placeholder="To">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span> </div>
<div class="col-md-8 col-lg-3 form-group">
<input id="flightDepart" type="text" class="form-control" required placeholder="Depart Date">
<span class="icon-inside"><i class="far fa-calendar-alt"></i></span> </div>
<div class="col-md-8 col-lg-3 form-group">
<input id="flightReturn" type="text" class="form-control" required placeholder="Return Date">
<span class="icon-inside"><i class="far fa-calendar-alt"></i></span> </div>
<div class="col-md-12 form-group">
<button class="btn btn-primary btn-block" type="submit">Search</button>
</div>
</div>
</form>
</div>
FlightsController.php
namespace App\Http\Controllers;
use App\Flights;
use Illuminate\Http\Request;
class FlightsController extends Controller
{
public function searchFlights(Request $request){
return $request->all();
}
}
web.php
Route::get('/flightSearch', 'FlightsController#searchFlights')->name('flightSearch');
Flights.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flights extends Model
{
protected $fillable = [
'flightFrom', 'flightTo', 'flightDepart', 'flightReturn',
];
}
first you need to add the method to the form and names to the inputs
<form method="GET" id="bookingFlight" action="{{url("/flightSearch")}}">
{{ csrf_field() }}
<input type="text" name="flightFrom" class="form-control" id="flightFrom" required placeholder="From">
<input type="text" name="flightTo" class="form-control" id="flightTo" required placeholder="To">
<input name="flightDepart" id="flightDepart" type="text" class="form-control" required placeholder="Depart Date">
<input name="flightReturn" id="flightReturn" type="text" class="form-control" required placeholder="Return Date">
<button class="btn btn-primary btn-block" type="submit">Search</button>
</form>
and then you can perform the query based on the request data:
public function searchFlights(Request $request){
return Flights::where('flightFrom', 'like', '%' . $request->flightFrom . '%')
->where('flightTo', 'like', '%' . $request->flightTo . '%')
->where('flightDepart', 'like', '%' . $request->flightDepart . '%')
->where('flightReturn', 'like', '%' . $request->flightReturn . '%')
->get();
}
based on #RioHilmy comment, note you can use inputs type date for flightDepart and flightReturn:
<form method="GET" id="bookingFlight" action="{{url("/flightSearch")}}">
{{ csrf_field() }}
<input type="text" name="flightFrom" id="flightFrom" required placeholder="From">
<input type="text" name="flightTo" id="flightTo" required placeholder="To">
<input name="flightDepart" type="date" id="flightDepart" required>
<input name="flightReturn" type="date" id="flightReturn" required>
<button class="btn btn-primary btn-block" type="submit">Search</button>
</form>
then you can perform the query with the dates values:
public function searchFlights(Request $request){
return Flights::where('flightFrom', 'like', '%' . $request->flightFrom . '%')
->where('flightTo', 'like', '%' . $request->flightTo . '%')
->whereDate('flightDepart', $request->flightDepart)
->whereDate('flightReturn', $request->flightReturn)
->get();
}

Laravel search & filter

This will display the current url page and I want to filter the contents from the table of view page... any idea how to do it in controller?
Here students is current view page
Where did I make a mistake? My table's fields are name, country, age.
<form method='get' style="display:inline-block;width: 260px;" action='{{Request::url()}}'>
<div class="input-group">
<input type="text" name="q" value="{{ Request::get('q') }}" class="form-control input-sm pull-{{ trans('crudbooster.right') }}" placeholder="{{trans('crudbooster.filter_search')}}"/>
{!! CRUDBooster::getUrlParameters(['q']) !!}
<div class="input-group-btn">
#if(Request::get('q'))
<?php
$parameters = Request::all();
unset($parameters['q']);
$build_query = urldecode(http_build_query($parameters));
$build_query = ($build_query)?"?".$build_query:"";
$build_query = (Request::all())?$build_query:"";
?>
<button type='button' onclick='location.href="{{ CRUDBooster::mainpath().$build_query}}"' title="{{trans('crudbooster.button_reset')}}" class='btn btn-sm btn-warning'><i class='fa fa-ban'></i></button>
#endif
<button type='submit' class="btn btn-sm btn-default"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
this is my controller code.
public function getstudents(Request $request)
{
$students =DB::table('users')
->orderBy('created_at',desc)
->get();
$searchby =$request->get('q');
$students = DB::table('students')
->where('name', 'like', '%' . $searchby . '%')
->get();
return view('students',compact('students'));
}

Pass Data from input form to ajax in Laravel

I Have a List of projects and three type of payments for any project I Want to give all type of payments and send payment id to ajax But I give just id=1 for all request my Code is here:
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<input type="hidden"id="payment_id"value="{{$payment->id}}">
<input type="text" class="form-control" id="paymentCode" name="paymentC">
{{csrf_field()}}
<button id="AddCode" type=submit class="btn btn-info generate-label AddCode">enter</button>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" name="paymentC" id="paymentCode">
{{csrf_field()}}
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.AddCode').click (function (event) {
var payment_id = $('#payment_id').val();
var paymentCode = $('#paymentCode').val();
console.log(payment_id);
});
How I Can fix my problem that When click in button of payment print this id?
You're not targetting an unique id with var payment_id = $('#payment_id').val();
Change this line of html
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
Into
<button value="{{$payment->id}}" type="submit" class="btn btn-info generate-label AddCode">enter</button>
JQuery
$('.AddCode').on("click", function(event){
var payment_id = $(this).val(); // payment id
});
Try this
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<div>
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" id="paymentCode" name="paymentC">
{{csrf_field()}}
<button id="AddCode" class="btn btn-info generate-label AddCode">enter</button>
</div>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<div>
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" name="paymentC" id="paymentCode">
{{csrf_field()}}
<button id="AddCode" class="btn btn-info generate-label AddCode">enter</button>
</div>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.AddCode').click (function (event) {
var payment_id = $(this).closest('div').find("#payment_id").val();
var paymentCode = $(this).closest('div').find("#paymentCode").val();
console.log(payment_id);
});
ID should be unique for individual HTML entity.
Code Update:
{{csrf_field()}}
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<input type="hidden"id="payment-id-1"value="{{$payment->id}}">
<input type="text" class="form-control" id="payment-code-1" name="payment_code">
<button type=submit class="btn btn-info generate-label add-code" data-payment=1>enter</button>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<input type="hidden" id="payment_id_2" value="{{$payment->id}}">
<input type="text" class="form-control" name="payment_code" id="paymentCode">
<button type="submit" class="btn btn-info generate-label add-code" data-payment=1>enter</button>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.add-code').on("click", function(event){
var payment_type = $(this).data("payment");
var payment_id = $('#payment-id-'+payment_type).val();
var paymentCode = $('#payment-code-'+payment_type).val();
});
Try this out. Not tested.

Categories