sending value from view to controller - php

I have a dropdown list to display values for certain building: i am not sure how can I send the value to a new page to display the values for that selected building only. i am getting the error when trying to load the page with the dropdown menu. I am not sure what i am doing wrong.
here is my index.blade:
<form action="{{ route('custom'),$datacenter }}">
<div class="form-group" id="dropdown">
<label>Data Center:</label>
<select name="datacenter" id="datacenter" class="form-group">
<option value="RDC46">RDC46</option>
<option value="RDC03">RDC03</option>
<option value="RDC05">RDC05</option>
<option value="DDC21">DDC21</option>
</select>
<button type="submit" class="btn btn-info">Submit</button>
</div>
here is my web route:
Route::get('custom\{datacenter}', 'AssigneeController#custom')->name('custom');
and here is my controller:
public function custom($datacenter)
{
$assignees = assignee::findorfail('datacenter')->paginate(10);
return view('custom',compact('assignees'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
I am getting the following error:
Missing required parameters for [Route: custom] [URI:
custom{datacenter}]. (View:
/Users/Documents/Laravel/blog/resources/views/assignees/index.blade.php)

You should try this:
<form action="{{ route('custom',[$datacenter]) }}">
<div class="form-group" id="dropdown">
<label>Data Center:</label>
<select name="datacenter" id="datacenter" class="form-group">
<option value="RDC46">RDC46</option>
<option value="RDC03">RDC03</option>
<option value="RDC05">RDC05</option>
<option value="DDC21">DDC21</option>
</select>
<button type="submit" class="btn btn-info">Submit</button>
</div>
</form>

It feels like a simple GET request to me.
Change your form to the following:
<form action="{{ route('custom') }}">
<div class="form-group" id="dropdown">
<label>Data Center:</label>
<select name="datacenter" id="datacenter" class="form-group">
<option value="RDC46">RDC46</option>
<option value="RDC03">RDC03</option>
<option value="RDC05">RDC05</option>
<option value="DDC21">DDC21</option>
</select>
<button type="submit" class="btn btn-info">Submit</button>
</div>
</form>
In your controller:
public function custom(Request $request)
{
// You will have access to the datacenter using $request->datacenter
$assignees = assignee::findorfail($request->datacenter)->paginate(10);
return view('custom',compact('assignees'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
There's no need for the datacenter segment in your route:
Route::get('custom', 'AssigneeController#custom')->name('custom');

Try this code,
<form action="{{ route('custom', $datacenter)}}">
<div class="form-group" id="dropdown">
<label>Data Center:</label>
<select name="datacenter" id="datacenter" class="form-group">
<option value="RDC46">RDC46</option>
<option value="RDC03">RDC03</option>
<option value="RDC05">RDC05</option>
<option value="DDC21">DDC21</option>
</select>
<button type="submit" class="btn btn-info">Submit</button>
</div>
</form>

Related

laravel passing parameters to route in view blade

So I'm very new to laravel and I have a filter in my website blade as follows:
goal: the user chooses a city and price from and price to and then clicks search and villas with what selected must be shown on another view! all values from the database!
I've tried to do this:
the route:
Route::get('/searched/{city_id}/{pricefrom}/{priceto}', [WebsiteController::class, 'search'])->name('search.clicked');
the controller:
public function index() {
$cities = DB::table('cities')
->select('*')->distinct()->get();
$users = Villa::with('City','Seller', 'Payment')->get();
$pricefrom = DB::table('villas')
->select('price')
->where('price', '<=', 50000)->distinct()->get();
$priceto = DB::table('villas')
->select('price')
->where('price', '>', 50000)->distinct()->get();
return view('website', compact('users','cities','pricefrom','priceto'));
}
public function search($city_id, $pricefrom, $priceto) {
$city = City::find($city_id);
$price = Villa::find($price);
$citydata = Villa::with('City','Seller', 'Payment')->where('city_id', $city_id)->get();
$lowdata = Villa::with('City','Seller', 'Payment')->where('price', $pricefrom)->get();
$highdata = Villa::with('City','Seller', 'Payment')->where('price', $priceto)->get();
$info = Villa::with('City','Seller', 'Payment')->get();
return view ('searched',compact('citydata','lowdata','highdata','info'))->with('city', $city )->with('price', $price);
}
the website view blade: here I didn't know how exactly I should pass the parameters for the route so I get an error
#foreach ($users as $villa)
<form action="{{ route('search.clicked', $villa->city_id,$villa->pricefrom,$villa->priceto) }}" method="get">
#csrf
<select name="city" class="form-select">
<option value="" id="searchoption"> City </option>
#foreach ($cities as $city)
<option >{{ $city->name }}</option>
#endforeach
</select>
<div class="col-lg-3 p-2">
<select name="pricefrom" class="form-select">
<option value="" id="searchoption"> Price From </option>
#foreach ($pricefrom as $low)
<option >{{ $low->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<select name="priceto" class="form-select">
<option value="" id="searchoption"> Price To </option>
#foreach ($priceto as $high)
<option >{{ $high->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<button type="button" class="btn btn-outline-success">search</button>
</div>
</form>
#endforeach
also Villa model
class Villa extends Model {
use HasFactory;
protected $fillable=[
"id", "title", "description", "price", "state", "status", "city_id", "seller_id", "payment_id",
"created_at", "updated_at"
];
public function City()
{
return $this -> hasOne(City::class,'id','city_id');
}
public function Seller()
{
return $this -> hasOne(Seller::class,'id','seller_id');
}
public function Payment()
{
return $this -> hasOne(Payment::class,'id','payment_id');
}
}
I think I have many errors I should fix And I want some help with my search bar!
I get this error:
Missing required parameters for [Route: search.clicked] [URI: searched/{city_id}/{pricefrom}/{priceto}] [Missing parameters: pricefrom, priceto].
try:
<form action="{{ route('search.clicked', [$villa->city_id,$villa->pricefrom,$villa->priceto]) }}" method="get">
According to route function second parameter should be string or array. String or Array if you're passing only 1 & array if you are passing 2 or more.
function route($name, $parameters = [], $absolute = true)
BLADE FILE:
#foreach ($users as $villa)
<form action="{{ route('search.clicked', $villa->city_id,$villa->pricefrom,$villa->priceto) }}" method="get">
#csrf
<select name="city" class="form-select">
<option value="" id="searchoption"> City </option>
#foreach ($cities as $city)
<option >{{ $city->name }}</option>
#endforeach
</select>
<div class="col-lg-3 p-2">
<select name="pricefrom" class="form-select">
<option value="" id="searchoption"> Price From </option>
#foreach ($pricefrom as $low)
<option >{{ $low->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<select name="priceto" class="form-select">
<option value="" id="searchoption"> Price To </option>
#foreach ($priceto as $high)
<option >{{ $high->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<button type="button" class="btn btn-outline-success">search</button>
</div>
</form>
#endforeach
to
#foreach ($users as $villa)
<form action="{{ route('search.clicked', ['city_id' => $villa->city_id, 'pricfrom' => $villa->pricefrom, 'priceto' =>$villa->priceto]) }}" method="get">
#csrf
<select name="city" class="form-select">
<option value="" id="searchoption"> City </option>
#foreach ($villa->cities as $city)
<option >{{ $city->name }}</option>
#endforeach
</select>
<div class="col-lg-3 p-2">
<select name="pricefrom" class="form-select">
<option value="" id="searchoption"> Price From </option>
#foreach ($villa->pricefrom as $low)
<option >{{ $low->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<select name="priceto" class="form-select">
<option value="" id="searchoption"> Price To </option>
#foreach ($villa->priceto as $high)
<option >{{ $high->price}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 p-2">
<button type="button" class="btn btn-outline-success">search</button>
</div>
</form>
#endforeach
You need add to route :
<form action="{{ route('search.clicked',$object->first()->city_id,$model->first()->pricefrom,$model->first()->priceto) }}" method="get">

Passing data to Queue Job gives 404 - Not other Error

From My Controller
use App\Jobs\MonthlyReport;
public function store(Request $request)
{
$report = new Report;
...
$report->save();
$this->dispatch(new MonthlyReport($report));
return $report;
}
MonthlyReport.php
use App\Report;
private $rep;
public function __construct(Report $report)
{
$this->rep = $report;
}
public function handle()
{
dd($this->rep);
}
It gives 404, no other error. But If I didn't pass $report variable then it's working. What should be the problem that trigger 404 instead of error or dd();
Note: The same logic is used in another project and it's working, but not here
My Web.php
Route::resource('report', 'ReportController');
My Form (Year and month data are coming from JQuery)
<form method="POST" action="/report">
#csrf
<div class="form-group">
<label class="required">Office</label>
<select class="form-control" id="change_office" required name="office">
<option value>choose office</option>
#foreach ($offices as $office)
<option value="{{ $office->id }}">{{ $office->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label class="required" for="year">Option</label>
<select id="option" class="form-control" name="option" required>
<option value="">Choose options</option>
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Yearly">Yearly</option>
<option value="Custom">Custom</option>
<option value="Time Base">Entry/Exit Report</option>
</select>
</div>
<div class="form-group" id="yearly_wrapper" style="display: none;">
<label class="required" for="year">Year</label>
<select id="year" class="form-control" name="year">
<option value="">Choose year</option>
</select>
</div>
<div class="form-group" id="monthly_wrapper" style="display: none;">
<label class="required">Select Month</label>
<select id="monthly" class="form-control" name="monthly">
<option value="">Select month</option>
</select>
</div>
<div>
<button type="submit" class="btn btn-outline-warning btn-round">
<i class="now-ui-icons ui-1_check"></i> Generate
</button>
</div>
</form>
If you just want to check the $report in your job handle, try something like this:
Job Class
public function handle()
{
Log::info('Report created', $this->rep);
return;
}
Controller
public function store(Request $request)
{
$report = new Report;
...
$report->save();
return MonthlyReport::dispatch($report);
}
If everything is ok, you should see the created report in your log file.

How can I get the selected index of a select?

I am in Laravel 5.7, I will wish to get the index of a select. I have tried this but without success so far.
<div class="form-group">
<label for="company-content">Ville</label>
<select name="fk_localite" id="" class="form-control">
<option value="">Selectionner votre ville</option>
#foreach($localites as $localite)
<option value="{{$localite->id}}">{{$localite->ville}}
</option>
#endforeach
</select>
Here is the solution !
<div class="form-group">
<label for="company-content">Ville</label>
<select name="fk_localite" id="" class="form-control">
#foreach($localites as $localite)
<option value="{{$localite->id}}" #if($eleves->fk_localite == $localite->id) selected #endif>{{ $localite->ville }}
</option>
#endforeach
</select>
</div>

Can't send collection to bootstrap-select in Laravel form

Here is an edit form I've created
#extends('main')
#section('title', 'Edit Post')
#section('content')
<form class="form-group" action="/categories/update" method="POST">
{{csrf_field()}}
<input name="_method" type="hidden" value="PUT">
<label for="category_id" id="category_id">Select Category</label>
<select class="form-control" id="category_id" style="height:auto;" name="category_id">
#foreach($categories as $category)
<hr class="custom-hr-heading"/>
<option value="{{$category->id}}" {{($category->id == $post->category->id)?"selected='selected'":""}}>
{{$category->name}}
</option>
#endforeach
</select>
<label for="title" class="mt-2" name="title" id="title">Your Post Title</label>
<input type="text" class="form-control mb-2" value="{{$post->title}}" name="title" id="title">
<textarea class="form-control" name="body">{{$post->body}}</textarea>
<label for="tags" id="tags" name="tags">Select Tags..</label>
<select class="selectpicker mt-3" id="tags[]" name="tags[]" multiple="multiple">
#foreach($tags as$tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
#endforeach
</select>
<br/>
<button type="submit" class="btn mt-3 btn-success btn-sm">Update</button>
</form>
<script type="text/javascript">
tinymce.init({
selector: 'textarea',
theme: 'modern',
height: 300
});
$('.selectpicker').selectpicker({
style: 'btn-info'
});
</script>
#stop
Controller
public function edit($id)
{
$post = Post::find($id);
$categories = Category::orderBy('name', 'asc')->get();
$tags = Tag::orderBy('name', 'asc')->get();
return view('posts.edit', compact('post', 'categories', 'tags'))
}
I get a themed select box that just says nothing selected. Am I passing the tags collection to the select box correctly?
I'm using Laravel 5.5, PHP 7.2, and Bootstrap 4.
I've tried bootstrap-select via CDN and local files.
You are missing a space
Change:
#foreach($tags as$tag)
To:
#foreach($tags as $tag)
As per Bootstrap 4's documentation, this is a short attribute, not a name-value pair, so I would do it like so (and clean it up a little):
<select class="form-control" id="category_id" style="height:auto;" name="category_id">
<option value="">Choose an Option</option>
#foreach($categories as $category)
<hr class="custom-hr-heading"/>
<option value="{{ $category->id }}" #if($category->id == $post->category->id) selected #endif >
{{ $category->name }}
</option>
#endforeach
</select>
The problem was infact two problems.
Bootstrap 4 (final) support is only available in the latest bootstrap-select beta release here
Also my code was a little out. Here is the final working code.
<select class="selectpicker mt-3 form-control" name="tags[]" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{$post->tags->contains($tag->id)?"selected='selected":""}}>{{$tag->name}}</option>
#endforeach
</select>

Get blank page after submit in Codeigniter?

I am working on CI and i created one simple CRUD to insert PDF with some data in table. Sometimes my code works but now its completely not working.I am getting blank page after submitting my form. And nothing is happens like print_r and var_dump etc. I dont know whats wrong in my code.
My sidebar :
From where i click to my controller to get my view.
<li>Upload Pay slips</li>
Its working fine. On click this i am able to go to my page that is add_pay_slips.
Controller for the same:
public function add_pay_slips()
{
$this->load->view('layouts/header');
$this->load->view('layouts/sidebar');
$data['salary']=$this->pay_slips->getPaySlips1();
$this->load->view('responsibilities/add_pay_slips',$data);
$this->load->view('layouts/footer');
}
After this i get my form view:
<form method="post" enctype="multipart/form-data" class="form-horizontal" action="<?php echo base_url();?>responsible/upload">
<div class="control-group">
<label class="control-label">Select Month</label>
<div class="controls">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Year</label>
<div class="controls">
<select name="year">
<option>2017</option>
<option>2016</option>
<option>2015</option>
<option>2014</option>
<option>2013</option>
<option>2012</option>
<option>2011</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Employee</label>
<div class="controls">
<select name="employee_name">
<?php
foreach ($salary as $row) {?>
<option value='<?php echo $row->EMPLOYEE_ID;?>'><?php echo $row->EMPLOYEE_NAME;}?></option>
<!--<input type="hidden" name="employee_id" value="<?php //echo $row->EMPLOYEE_ID?>">-->
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Country</label>
<div class="controls">
<select name="org_id">
<option value="40">UK</option>
<option value="65">INDIA</option>
<option value="47">POLAND</option>
<option value="57">GERMANY</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Upload Payslip</label>
<div class="controls">
<input type="file" name="userfile"/>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Save</button>
</div>
</form>
and for this view on submit button i created one controller to insert data.
function upload(){
$filename = 'pdf';
$config = array(
'upload_path' => 'uploads',
'allowed_types' => "pdf",
'file_name' => $filename
);
$config['allowed_types'] = 'pdf';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload())
{
$file_data = $this->upload->data();
$data1['userfile'] = $file_data['file_name'];
$data=
array(
'PAY_MONTH'=>$this->input->post('month'),
'PAY_YEAR'=>$this->input->post('year'),
'ORG_ID'=>$this->input->post('org_id'),
'EMPLOYEE_ID'=>$this->input->post('employee_name'),
'PDF'=>$data1['userfile'],
);
$this->load->model('pay_slips_model');
$result=$this->pay_slips->insert_payslips($data);
if($result==true)
{
$this->session->set_flashdata('msg',"PDF Added Successfully");
$this->load->view('layouts/header');
$this->load->view('layouts/sidebar');
$data['salary']=$this->pay_slips->getPaySlips1();
$this->load->view('responsibilities/add_pay_slips' ,$data,'refresh');
$this->load->view('layouts/footer');
}
else
{
$this->seesion->set_flashdata('msg1',"PDF Added Failed");
redirect('responsibilities/add_pay_slips');
}
}
}
And for this i have one model to insert data.
public function insert_payslips($data)
{
$this->db->insert('all_salary_slip_details',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
I have one page responsible.php my all controllers are on the same page.What is affecting my code. i am stuck here. Onclick Submit i just got my controller's url and blank page. payroll1/responsible/upload.

Categories