Laravel 5 simple search box using GET method - laravel-5.8

I want to search code for enter an admin.
web.php
Route::get('/document', 'DocumentController#document')->name('document');
DocumentController.php
public function document()
{
$keyword = request('code');
$documents = Document::document($keyword)->latest()->get();
return view('Home.content.documents', compact('documents'));
}
Document.php
public function scopeDocument($query, $keywords)
{
$keywords = explode(' ',$keywords);
foreach ($keywords as $keyword) {
$query->where('code' , 'LIKE' , '%' . $keyword . '%');
}
return $query;
}
documents.blade,php
<form action="{{ route('document') }}" method="get">
<div class="form-group">
<label for="code">Code</label>
<input type="text" class="form-control col-sm-4" id="code" name="code">
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
<div class="text-center">
#foreach($documents as $document)
<h1 class="m-3">{{ $document->first_name }} {{ $document->last_name }}</h1>
<img src="images/documents/{{ $document->image }}" class="img-fluid">
#endforeach
</div>
Why this code is not working?

Related

select2 not showing options within livewire component

I am working with laravel livewire.There is a component to update brands and categories of a product. category and brand select option is a select2 option. dropdown showing as a select2, but does not show the options.
This is my component
<?php
namespace App\Http\Livewire\Components\MemberArea\Product;
use App\Models\ProductBrand;
use App\Models\ProductCategory;
use App\Models\Product;
use Illuminate\Support\Facades\Session;
use Livewire\Component;
class UpdateProductBrand extends Component
{
public $product_id;
public $product_category_id;
public $product_brand_id, $categories, $brands, $product;
public function render()
{
return view('pages.products.components.update-product-brand');
}
public function mount()
{
$this->product = Product::get($this->product_id);
$this->categories = ProductCategory::all();
$this->brands = ProductBrand::all();
$this->product_category_id = $this->product->product_category_id;
}
public function submit()
{
$data = $this->validate();
$product = Product::get($this->product_id);
Product::update($product, $data);
session()->flash('message', 'Brand Details Updated Successfully.');
}
}
This is My Blade
<div class="card-body">
<form wire:submit.prevent="submit">
<div class="row">
<div class="form-group col-lg-6">
<label class="h5">Product Category <sup class="text-danger">*</sup>|<button type="button"
id="new-category" class="btn btn-light ">+</button></label>
<select required class="form-control" wire:model="product_category_id" id="categories_edit">
<option value=""></option>
#foreach ($categories as $category)
<option {{ $product->product_category_id === $category->id ? 'selected' :'' }}
value="{{ $category->id }}">{{
$category->title }}</option>
#endforeach
</select>
</div>
#error('product_brand_id')
<span class="text-danger error">{{ $message }}</span>
#enderror
<div class="form-group col-lg-6">
<label class="h5">Product Brand <sup class="text-danger">*</sup>| <button type="button" id="new-brand"
class="btn btn-light">+</button></label>
<select class="form-control" wire:model="product_brand_id" id="brands_edit">
#foreach ($brands as $brand)
<option {{ $product->product_brand_id === $brand->id ? 'selected' :'' }} value="{{ $brand->id }}">
{{ $brand->title }}
</option>
#endforeach
</select>
</div>
#error('product_category_id')
<span class="text-danger error">{{ $message }}</span>
#enderror
<div class="form-group col-lg-12 text-center">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</form>
</div>
#push('js')
<script>
document.addEventListener("livewire:load", function (event) {
window.livewire.hook('afterDomUpdate', () => {
$('#categories_edit, #brands_edit').select2({
placeholder: 'Select an option',
});
});
});
$(document).ready(function () {
$('#categories_edit').select2();
$('#brands_edit').select2();
});
//set category on select
$('#categories_edit').on('change', function (e) {
var category_select = $('#categories_edit').select2("val");
console.log("category when select= " + category_select);
#this.product_category_id = category_select;
});
//set brand on select
$('#brands_edit').on('change', function (e) {
var brand_select = $('#brands_edit').select2("val");
console.log("brand when select= " + brand_select);
#this.product_brand_id = brand_select;
});
</script>
#endpush
this is how show it
when use wire:ignore it also not working. show only dropdown as select2 , but it does not show the options

production.ERROR: Undefined variable: id Laravel blade

In my Laravel-5.8 I have this code in the controller:
public function appraisal(Request $request)
{
try {
$userCompany = Auth::user()->company_id;
$userId = Auth::user()->id;
$company = OrgCompany::where('id', $userCompany)->first();
$identities = Identity::where('company_id', $userCompany)->pluck('appraisal_name','id');
$goals = AppraisalGoal::where('employee_id', $userId)
->where(function ($query) {
$query->where('is_approved', 3)->orWhere('is_visible', 0);
})
->whereNull('deleted_at')
->orderBy('goal_type_id');
$method = $request->method();
if ($request->isMethod('post')) {
$id = $request->input('identity_id');
if ($request->has('search')) {
// select search
if ($request->input('identity_id')) {
$goals->where('identity_id',$request->input('identity_id'));
}
$goals = $goals->get();
return view('appraisal-default',['goals' => $goals, 'identities' => $identities]);
} elseif ($request->has('viewIdentity')) {
// select goal identity
if ($request->input('identity_id')) {
return view('performance_dashboard', $id);
}
}
} else {
//select current year
$goals = $goals->get();
return view('appraisal-default',['goals' => $goals, 'identities' => $identities]);
}
} catch (Exception $exception) {
Log::error($exception);
Session::flash('error', 'Action failed! Please try again');
return back();
}
}
view blade: appraisal-default
<div class="row" style="margin-bottom: 10px">
<label class="control-label"> Performance Period:<span style="color:red;">*</span></label>
<div class="col-sm-8">
<select id="identity" name="identity_id" class="form-control">
<option value="">--- Select Performance Period ---</option>
#foreach ($identities as $ids => $name)
<option value="{{ $ids }}" {{ request( 'identity_id')==$ids ? 'selected' : ''}}>{{ $name }}</option>
#endforeach
</select>
</div>
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-sm" name="search">Search</button>
<form action="{{ route('performance_dashboard', $id->id) }}" method="get" target="_blank" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<button type="submit" class="btn btn-secondary btn-sm" name="viewIdentity"><i class="fas fa-file-pdf"></i> View Identity</button>
</form>
</div>
</div>
In the view, I have two buttons with names: search and viewIdentity
When the
<select id="identity" name="identity_id" class="form-control">
is selected.
If search button is submitted, it displays the result on the same page. This is working.
However, where I'm having problem is when viewIdentity button is submitted. At this point it suppose to take the id of the dropdown as the parameter in:
<form action="{{ route('performance_dashboard', $id->id) }}" method="get" target="_blank" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<button type="submit" class="btn btn-secondary btn-sm" name="viewIdentity"><i class="fas fa-file-pdf"></i> View Identity</button>
</form>
and display the result in another page: performance_dashboard
I got this error in the view:
production.ERROR: Undefined variable: id
It points to this line in the view:
<form action="{{ route('performance_dashboard', $id->id) }}" method="get" target="_blank" class="form-horizontal" enctype="multipart/form-data">
How do I get it resolved?
Thanks

search by multiple values using checkbox in laravel

I have job_sector table in which sector_id and job_id fields are there. I just want to search job_id by the sectors which I have selected through checkbox. One may select multiple sectors.
My model :
public function scopeSelectedOptions($query, $input = [])
{
if(!empty($input)) {
if(array_key_exists('sector_id', $input)) {
$query->whereHas('sector_id', function($q) use ($input) {
return $q->whereIn('sector_id', $input['sector_id']);
});
}
}
return $query;
}
Controller :
public function jobsFilter(Request $request)
{
$jobs = JobSector::SelectedOptions(request()->all())->get();
return view('front.pages.job.jobfilter')->with(['title'=>'Job Filter', 'jobs' => $jobs]);
}
Form from where I am selecting multiple sectors :
<form action="{{ route('job.jobfilter') }}" method="GET" class="mb-4">
{{csrf_field()}}
#foreach(get_sectors() as $k=>$s)
<input type="checkbox" name="input[]" value="{{ $k }}">{{ $k }}<br>
#endforeach
<input type="submit" value="Search" />
</form>
Query showing the output :
#foreach($jobs as $c)
{{ $c->job_id }} <br>
#endforeach
It shows me all the job_id in the table.
Please help me out,
You are giving the wrong array to your scope
it would look like this :
['input' => ['12' => true]]
try this
<form action="{{ route('job.jobfilter') }}" method="GET" class="mb-4">
{{csrf_field()}}
#foreach(get_sectors() as $k=>$s)
<input type="checkbox" name="sector_id[{{ $k }}]">{{ $k }}<br>
#endforeach
<input type="submit" value="Search" />
</form>
public function jobsFilter(Request $request)
{
$jobs = JobSector::whereIn('sector_id', array_keys(request()->sector_id))->get();
return view('front.pages.job.jobfilter')->with(['title'=>'Job Filter', 'jobs' => $jobs]);
}
(I just ignore your scope to be more readable)

why do i get this problem when i want to filter project

I want to add the translate feature to my website. but I got this problem
Missing required parameters for [Route: filterdata] [URI: {locale}/project/{filter}]
this is the app web route:
Route::get('/', function () {
return redirect(app()->getLocale());
});
Route::get('/project', function () {
return redirect(app()->getLocale());
});
Route::group([
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale',
], function(){
Route::get('/', function(){
return view('pages.home');
})->name('home');
Route::get('/project/{filter}', 'ProjectController#filterProject')->name('filterdata');
});
this is the blade project
<a class="btn btn-default" href="{{ url(app()->getLocale().'/project/Branding') }}" role="button">Branding</a>
this is the controller:
public function filterProject($locale, $filter){
$locale = $locale;
$filter = str_replace('-',' ',$filter);
$url = Lang::get('proyek.project');
$url2 = json_encode($url);
$data = json_decode($url2, true);
$data = array_filter($data);
if(collect($data)->where('tag1',"{$filter}")->all() == true){
$project = collect($data)->where('tag1',"{$filter}")->all();
}elseif(collect($data)->where('tag2',"{$filter}")->all() == true){
$project = collect($data)->where('tag2',"{$filter}")->all();
}elseif(collect($data)->where('tag3',"{$filter}")->all() == true){
$project = collect($data)->where('tag3',"{$filter}")->all();
}else{
$project = collect($data)->all();
}
return view ('pages/projects', compact('project','locale','filter'));
}
and this is the buttons I use for language switch:
<div class="form-check d-flex align-items-center">
<input type="radio" class="form-check-input form-control" name="language" id="languageen" value="{{ route(\Illuminate\Support\Facades\Route::currentRouteName(), 'en') }}" onchange="location = this.value;" #if(app()->getLocale() == 'en') checked #endif>
<label class="form-check-label control-label" for="languageen">
EN
</label>
</div>
<div class="form-check d-flex align-items-center">
<input type="radio" class="form-check-input form-control" name="language" id="languageid" value="{{ route(\Illuminate\Support\Facades\Route::currentRouteName(), 'id') }}" onchange="location = this.value;" #if(app()->getLocale() == 'id') checked #endif>
<label class="form-check-label control-label" for="languageid">
ID
</label>
</div>
Seems like you never passed $filter variable to your route, on the button element:
<a class="btn btn-default" href="{{ url(app()->getLocale().'/project/Branding') }}" role="button">Branding</a>
As you can see, you're only passing $locale parameter. In order for this to work, you pass your $filter variable as well
<a class="btn btn-default" href="{{ url(app()->getLocale().'/project/Branding/' . $filter) }}" role="button">Branding</a>
If you don't need $filter every time when you're calling this route, you can make it optional, with the ? operator:
Route::get('/project/{filter?}', 'ProjectController#filterProject')->name('filterdata');
zlatan
"Branding" is a filter
<a class="btn btn-default" href="{{ url(app()->getLocale().'/project/Branding') }}" role="button">Branding</a>

Laravel, Search by month, but I can't find anything

I'm doing a search a month, but I can't find anything. Start is a column of type date, 2020-07-23.
Controller:
public function cityName(Request $request, City $city)
{
$month = $request->month;
$status = $request->status;
if (!empty($month)) {
$chamado = Chamados::where('cite_id', $city->id)
->whereMonth('start', $month)
->when($status, function ($query, $status) {
return $query->where('status', $status);
})->get();
} else {
$chamado = Chamados::where('cite_id', $city->id)->get();
}
$total = Chamados::where('cite_id', $city->id)->value(DB::raw('SUM(total)'));
return view('admin.reports.city.city_name', compact('city', 'chamado', 'total', 'month'));
}
My form:
<form class="form-inline mb-3" action="{{ route('dashboard.report.city.name', $city->id) }}" id="formSearch" method="GET">
<input type="month" class="form-control form-control-sm mr-md-2" name="month" id="month" value="{{ isset($month) ? $month : '' }}">
<button type="button" name="filter" id="filter" class="btn btn-info btn-sm mr-2 mb-2">
<i class="fad fa-filter"></i> Filter
</button>
</form>
without search
with search
The whereMonth() method may be used to compare a column's value against a specific month of a year:
$chamado = Chamados::whereMonth('start', $month)

Resources