Laravel search & filter - php

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'));
}

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');

Laravel Form displaying correct data but storing incorrect values in DB

I have come across a bug on my website and I am baffled as to how to fix it.. basically I have a view to create courses, I type a course title and secondly assign an instructor to that course and click save. But when i return to my index page.. the instructor i assigned is completely different. It appears there must be an issue with my select but I am not sure how to go about fixing it.. I am new to laravel so any help is greatly appreciated.
I have added a picture to try to further explain my issue, here i have selected an instructor -
And when i return to my index page, the newly created course 'test' has the admin user assigned... I am very confused -
create.blade.php
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group {{ $errors->has('instructors') ? 'has-error' : '' }}">
<label class="required" for="name">Instructors</label>
<select class="form-control select2" name="instructors[]" id="instructors" multiple>
#foreach($instructors as $id => $instructors)
<option value="{{ $id }}" {{ in_array($id, old('instructors', [])) ? 'selected' : '' }}>{{ $instructors }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</div>
</form>
</div>
</div>
#endsection
index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<p>
#can('create_courses')
<button type="button" class="btn btn-success">Create Course</button>
#endcan('create_courses')
</p>
<div class="card">
<div class="card-header">Courses</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Course Title</th>
<th>Instructor</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($course as $course)
<tr>
<th scope="row">{{ $course->id }}</th>
<td>{{ $course->title}}</td>
<td>{{ implode (', ', $course->instructors()->pluck('name')->toArray()) }}</td>
<td>
#can('edit_courses')
<a class="btn btn-xs btn-secondary" href="{{ route('admin.modules.index', $course->id) }}">
Modules
</a>
#endcan
</td>
<td>
#can('edit_courses')
<a class="btn btn-xs btn-primary" href="{{ route('admin.courses.edit', $course->id) }}">
Edit
</a>
#endcan
</td>
<td>
#can('delete_courses')
<form action="{{ route('admin.courses.destroy', $course->id) }}" method="POST" onsubmit="return confirm('Confirm delete?');" style="display: inline-block;">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-danger" value="Delete">
</form>
#endcan
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- <div class="col-md-2 col-lg-2">
<div class="list-unstyled">
Courses
Modules
</div>
</div> -->
</div>
</div>
#endsection
CoursesController;
<?php
namespace App\Http\Controllers\Admin;
use Gate;
use App\User;
use App\Course;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class CoursesController extends Controller
{
public function __construct()
{
//calling auth middleware to check whether user is logged in, if no logged in user they will be redirected to login page
$this->middleware('auth');
}
public function index()
{
if(Gate::denies('manage_courses')){
return redirect(route('home'));
}
$courses = Course::all();
return view('admin.course.index')->with('course', $courses); //pass data down to view
}
public function create()
{
if(Gate::denies('create_courses')){
return redirect(route('home'));
}
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->get()->pluck('name'); //defining instructor variable to call in create.blade.php. Followed by specifying that only users with role_id:2 can be viewed in the select form by looping through the pivot table to check each role_id
return view('admin.course.create', compact('instructors')); //passing instructor to view
}
public function store(Request $request)
{
$course = Course::create($request->all()); //request all the data fields to store in DB
$course->instructors()->sync($request->input('instructors', [])); //input method retrieves all of the input values as an array
if($course->save()){
$request->session()->flash('success', 'The course ' . $course->title . ' has been created successfully.');
}else{
$request->session()->flash('error', 'There was an error creating the course');
}
return redirect()->route ('admin.courses.index');
}
public function destroy(Course $course)
{
if(Gate::denies('delete_courses'))
{
return redirect (route('admin.course.index'));
}
$course->delete();
return redirect()->route('admin.courses.index');
}
public function edit(Course $course)
{
if(Gate::denies('edit_courses'))
{
return redirect (route('admin.courses.index'));
}
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->get()->pluck('name');
//return view('admin.course.edit', compact('instructors'));
return view('admin.course.edit', compact('instructors'))->with([
'course' => $course
]);
}
public function update(Request $request, Course $course)
{
$course->update($request->all());
if ($course->save()){
$request->session()->flash('success', $course->title . ' has been updated successfully.');
}else{
$request->session()->flash('error', 'There was an error updating ' . $course->title);
}
return redirect()->route('admin.courses.index');
}
public function show(Course $course)
{
return view('admin.course.show', compact('course'));
}
}
Referring to latest comment -
you are updating data with key not id
try this
in controller
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->select('id','name')->get();
in blade
you will need instructors->id to update right value
#foreach($instructors as $id => $instructor)
<option value="{{ $instructor->id }}" {{ in_array($id, old('instructors', [])) ? 'selected' : '' }}>{{ $instructor->name }}
</option>
#endforeach

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();
}

Search in Laravel using routes instead of controllers

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');

Categories