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.
Related
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">
I have created a function to retrieve a specific column in the database using eloquent, on my productsController
public function create()
{
$category = Product::select('category')->get();
return view('products.create');
}
And Now I want to use an if statement on the create.blade.php to check whether the value in the database is equal to 'liquor',
<div class="row mb-4">
<label for="category" class="col-md-4 col-form-label text-md-end">Category</label>
<div class="col-md-6">
<select name="category" id="category" class="form-select col-md-6">
<option value="liquor">liquor</option>
<option value= "food">food</option>
<option value="DIY">DIY</option>
<option value= "thrift shops">thrift shops</option>
<option value= "home decor">home decor</option>
<option value= "phones and tablets">phones and tablets</option>
<option value= "computing">computing</option>
<option value= "electronics">electronics</option>
<option value= "beauty products">beauty products</option>
<option value= "others">others</option>
</select>
</div>
</div>
#if($category == 'liquor')
<div class="row mb-4">
<label for="subcategory" class="col-md-4 col-form-label text-md-end">Sub Category</label>
<div class="col-md-6">
<select name="subcategory" id="subcategory" class="form-select col-md-6">
<option value="null">Select subcategory</option>
<option value="wine">Wine</option>
<option value="whisky">Whisky</option>
<option value="brandy">Brandy</option>
<option value="scotch">Scotch</option>
<option value="spirit">Spirit</option>
<option value="gin">Gin</option>
<option value="vodka">Vodka</option>
<option value="beer">Beer</option>
<option value="rum">Rum</option>
<option value="mixers">Mixers</option>
<option value="bourbon">Bourbon</option>
<option value="cognac">Cognac</option>
<option value="other">Other</option>
</select>
</div>
</div>
<div class="row mb-4">
<label for="volume" class="col-md-4 col-form-label text-md-end">Volume</label>
<div class="col-md-6">
<select name="volume" id="volume" class="form-select col-md-6">
<option value="null">Choose volume</option>
<option value="5ltr">5ltr</option>
<option value="1ltr">1ltr</option>
<option value="750ml">750ml</option>
<option value="500ml">500ml</option>
<option value="250ml">250ml</option>
<option value="other">Other</option>
</select>
</div>
</div>
#endif
However, I get the following error: Undefined variable $category
What could be the issue?
I think you forgot to send the $category to the view:
return view('products.create', compact("category"));
or like this:
return view('products.create')
->with('category', $category)
->with('title', 'New Title');
or like this:
return view('products.create', ['category' => $category]);
You can add as many vars/collections/or whatever as you wish both ways.
use this
#if(isset($category) && $category == 'liquor')
//your code
#endif
Also check this link for validation
you need to send $category from controller to your blade like this
public function create()
{
$category = Product::select('category')->get();
return view('products.create')->with('category',$category);
}
you can also do this with another way
public function create()
{
$category = Product::select('category')->get();
return view('products.create',compact('category'));
}
I am creating a real estate project, which will have a search option for different pages with different search filters.
Here is one example of the search view
<form class="form-row basic-select-wrapper" method="POST" action="{{route('filter')}}">
#csrf
{{-- <div class="form-group">
<input type="text" name="prop_name">
</div> --}}
<div class="form-group col-lg col-md-6">
<label>Property type</label>
<select name="type_id" class="form-control basic-select">
<option>Select Type</option>
#foreach ($types as $type)
#if ($type->status == 1)
<option value="{{ $type->id }}">{{ $type->name }}</option>
#endif
#endforeach
</select>
</div>
<div class="form-group col-lg col-md-6">
<label>Property Category</label>
<select name="cat_id" class="form-control basic-select">
<option>Select Category</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group col-lg col-md-6">
<label>Location</label>
<select name="loc_id" class="form-control basic-select">
<option>Select Location</option>
#foreach ($locations as $location)
<option value="{{ $location->id }}">{{ $location->loc_name }}</option>
#endforeach
</select>
</div>
<div class="form-group col-lg col-md-6">
<label>Budget</label>
<select name="start_price" class="form-control basic-select">
<option>Select Budget</option>
#foreach($price['price'] as $key => $val)
<option value="{{$key}}">{{$val}}</option>
#endforeach
</select>
</div>
<div class="form-group col-lg col-md-6">
<label>Bedroom(s)</label>
<select name="min_bed" class="form-control basic-select">
<option>Select Bedroom</option>
<option value="0">studio</option>
<option value="1">one bedroom</option>
<option value="2">two bedroom</option>
<option value="3">three bedroom</option>
<option value="4">four bedroom</option>
<option value="5">five bedroom</option>
<option value="6">six bedroom</option>
<option value="7">seven bedroom</option>
</select>
</div>
<div class="advanced-search" id="advanced-search">
<div class="card card-body">
<div class="form-group col-md-12">
<div class="mt-0">
<button class="btn btn-primary align-items-center" type="submit"><i
class="fas fa-search mr-1"></i><span>Search</span></button>
</div>
</div>
</div>
</div>
</form>
Here is my search controller
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//use App\Models\Listing;
use App\ListingSearch\ListingSearch;
class SearchController extends Controller
{
public function filter(Request $request)
{
return ListingSearch::apply($request);
}
}
Now, to implement this since there are multiple search filters and I wanted to simplify it. Here is the folder structure
folder structure
Here is the Listitng Search Class
<?php
namespace App\ListingSearch;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Listing;
use Illuminate\Http\Request;
class ListingSearch
{
public static function apply(Request $filters)
{
$query = static::applyDecoratorsFromRequest(
$filters, (new Listing)->newQuery()
);
// Get the results and return them.
return static::getResults($query);
}
private static function applyDecoratorsFromRequest(Request $request, Builder $query)
{
foreach ($request->all() as $filterName => $value) {
$decorator = static::createFilterDecorator($filterName);
if (static::isValidDecorator($decorator)) {
$query = $decorator::apply($query, $value);
}
}
return $query;
}
private static function createFilterDecorator($name)
{
return __NAMESPACE__ . '\\Filters\\' .
str_replace(' ', '',
ucwords(str_replace('_', ' ', $name)));
}
private static function isValidDecorator($decorator)
{
return class_exists($decorator);
}
private static function getResults(Builder $query)
{
return $query->get();
}
}
here is an example of one of the filters CATID for category
<?php
namespace App\ListingSearch\Filters;
use Illuminate\Database\Eloquent\Builder;
class CatId implements Filter
{
public static function apply($builder, $value)
{
return $builder->where('cat_id', $value);
}
}
The problem with this is that I have to enter all the required filters to get a result, otherwise if I only fill one filter I get null results, for example I want only apartments, without entering location or category. How can I achieve this?
search query
Did you try to filter the $value before stacking them in your where?
if (!empty($value)) {
}
empty() will take care of filtering out the empty and null values. So you end up adding to your query filter values that actually exists.
EDIT
foreach ($request->all() as $filterName => $value) {
if (!empty($value)) {
$decorator = static::createFilterDecorator($filterName);
if (static::isValidDecorator($decorator)) {
$query = $decorator::apply($query, $value);
}
}
}
EDIT 2
Your option tag has no value attribute defined and there for is not empty when posted to your controller because the browser defaults to the value inside the tag (Select Type, Select Category ect). You need to provide the value attribute as empty so that the POST does not use the inner text like this:
<option value="">Select Type</option>
[...]
<option value="">Select Category</option>
[...]
<option value="">Select Location</option>
[...]
<option value="">Select Budget</option>
Then if (!empty($value)) {} can do it's thing
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>
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.