I have a search working that finds users whose preferences match a landlords property. This is all well and good, but I want the landlord to be able to select a property, then press search, and pass that property object to be compared against.
This is an image of the search
Whichever property is chosen from the list is used to be searched against in the search.
This is the view of the search
<div class="row">
<div class="col-md-10">
<select class="form-control" id="property_address" name="property_address">
<!--Gets all counties from DB -->
#foreach ($properties as $property)
<option value="{{$property->address . ', ' . $property->town . ', ' . $property->county}}">{{$property->address . ', ' . $property->town . ', ' . $property->county}}</option>
#endforeach
</select>
</div> <!-- ./ col-6-->
</div> <!-- ./ row-5 -->
<div class="row mt-md-4">
<div class="col-md-4">
<button type="submit" class="form-control btn btn-primary">
Search
</button>
</div>
</div>
This is the controller
The first part renders the search form
public function searchhome(){
//Search Filter UI
//Populates fields.
$user = Auth::user();
$properties = PropertyAdvert::where('user_id', Auth::id())->get();
return view('pages/account/search/index', compact('user', 'properties'));}
Next is the logic
public function searchresults(Request $request){
//Gets all users that are tenants
$tenants = User::where('userType', 'tenant')->first();
//Gets all preferances
$Prefereances = TenantPreferance::all()->first();
//Gets the prefereances that match a tenant id
$pref = $Prefereances::where('user_id', $tenants->id)->first();
//Gets the current signed in users property
//Attempting to get value of property by name
$property = $request->property_address;
$result = $pref
->where('county' , $property->county)
->where('type' , $property->type)
->where('rent', '<=', $property->rent)
->where('bedrooms', '<=', $property->bedrooms)
->where('bathrooms', '<=', $property->bathrooms);
$users = $result->get();
return view('pages/account/search/results', compact('users'));
}
I tried using the request object to set the selected property as the one being compared against.
Any ideas?
Rather than populating the select value with the address, populate it with the ID of the property, then get that property from the database by the ID passed from the select.
$property = Property::find($request->property_id);
$result = $pref
->where('county' , $property->county)
->where('type' , $property->type)
->where('rent', '<=', $property->rent)
->where('bedrooms', '<=', $property->bedrooms)
->where('bathrooms', '<=', $property->bathrooms);
$users = $result->get();
Related
I'm facing a problem while storing data...I'm using multiselect on my view and selecting multiple courses from that.
This is my View:
<div class="form-group">
<label for="">Select Course </label>
<select class="mul-select" multiple="true" name="course_code[]" id="group"class="form-control">Group ID's
<option value="">Select Course Code</option>
#foreach($courses as $c)
<option value="{{$c->id}}">{{$c->course_title}}</option>
#endforeach
</select>
</div>
My initial controller and it's not dynamic.I can select only there courses and store into my database through three foreign of same table.
public function store(Request $r){
$teacher_id = $r->teacher_id;
$session = $r->session;
$semester = $r->semester;
$course1=NULL;
$course2=NULL;
$course3=NULL;
$size =sizeof($r->course_code);
if($size>0)
$course1 = $r->course_code[0];
if($size>1)
$course2 = $r->course_code[1];
if($size>2)
$course3 = $r->course_code[2];
$obj = new Assigncourse();
$obj->teacher_id = $teacher_id;
$obj->session = $session;
$obj->semester =$semester;
$obj->course1 =$course1;
$obj->course2 = $course2;
$obj->course3 = $course3;
if($obj->save())
{
if($course1!=NULL)
{$affected = DB::table('courses')
->where('id', $course1)
->update(['status' => 1]);
}
if($course2!=NULL)
{$affected = DB::table('courses')
->where('id', $course2)
->update(['status' => 1]);
}
if($course3!=NULL)
{$affected = DB::table('courses')
->where('id', $course3)
->update(['status' => 1]);
}
return redirect()->back()->with('msg','Succesfully Inserted');
}
}
In my detail-tickets view i want to show all the data of a loaded product with all its fields, but when i want to view the detail i get all my products and no only the one product that belongs to that ticket.
So the following is how im storing my tickets:
public function store(Request $request){
/*dd($request->all());*/
$ticket = new Ticket();
if($request->file('file')){
$file = $request->file('file');
$filename = time().'.'.$file->getClientOriginalName();
$request->file->move('storage/', $filename);
$ticket->file = $filename;
}
/*$ticket->cuenta_id = $request->cuenta_id;*/
$ticket->contact_id = $request->contact_id;
$ticket->statusTicket_id = $request->statusTicket_id;
$ticket->typeTicket_id = $request->typeTicket_id;
$ticket->idOwner = Auth::user()->id;
$ticket->product_id = $request->product_id;
$ticket->serial_number = $request->serial_number;
$ticket->accesories = $request->accesories;
$ticket->entry = Carbon::parse ( $request->entry)->toDateString();
$ticket->deliver = Carbon::parse ($request->deliver)->toDateString();
$ticket->save();
Session::flash('success');
return redirect()->route('tickets.view');
}
And this is my detail method:
public function detail($id){
$detailData = Ticket::find($id);
$detailData['contacts'] = Contact::all();
$detailData['products'] = Product::all();
$detailData['ptypes'] = Ptype::all();
$detailData['brands'] = Brand::all();
$detailData['models'] = ModelP::all();
return view('backend.ticket.detail-ticket', compact('detailData'));
}
So in my view im passing my product as the following:
#foreach($detailData['products'] as $product)
<div class="form-group col-md-3">
<label for="product_id">Producto</label>
<input type="text" name="product_id" value="{{$product->ptype->productType . ", " . $product->marca->brandName . " " . $product->modelo->modelName}}" class="form-control" readonly>
</div>
#endforeach
And this is an image of how it is bringing me data:
There should be only one product but instead i have all my product list.
Any idea what im doing wrong while passing the data to my view?
in this line:
$detailData['products'] = Product::all();
you are loading All products not only the wanted product ...
it should be:
$detailData['products'] = Product::find($detailData->product_id);
I want to save multi-select dropdown selected names with comma separated to the database. With my code it saves only last selected value to the database.
Blade.php
<div class="form-group">
<select name="team[]" id="team" class="selectpicker" multiple>
#foreach ($tdropdown as $tdrop =>$id)
<option value="{{$id}}">{{$tdrop}}</option>
#endforeach
</select>
</div>
Controller
public function empstore(Request $request){
$employee = new employee();
$employee->team = $request->team;
$tarray = $request->input('team');
foreach ($tarray as $key => $n) {
$employee->team = $tarray[$key];
$result = DB::table('teams')->where('id', '=', "$employee->team")->value('name');
// $employee->team = implode(',', $tarray);
$employee->team = $result;
//var_dump($result);
$employee->save();
}
//die();
return redirect()->route('employee.index')->with('success','Data Added');
}
When I var_dump($result); it outputs all the selected values as follows.
string(2) "Team one" string(2) "Team two"
But the above code saves only last selected value, it means only "Team two" to the database. Please help me to save all the selected values to the Database. Thank you.
$teams = $request->input('team', []);
$employee->team = DB::table('teams')
->whereIn('id', $teams) // only find the ids we received
->pluck('name') // only get the 'name' column
->implode(','); // implode into a comma separated list
$employee->save();
I want to calculate the minimum value from a collection of form fields.
I have one-to-many relationship, where one shop can have many items ans that is working fine without any errors.
My form
<form action="{{ route('form_submit') }}" method="post">
#csrf
<h3>Item 1</h3>
<input type="text" name="item[]">
<input type="text" name="price[]">
//Like this I can add many fields
<input type="submit">
</form>
Controller
public function store(Request $request, $id){
$shop = Shop::findorfail($id);
$item = Item::where('shop_id', $shop->id)->get(); //working fine
$i=0;
$price = request('price')
foreach( $items as $item)
$item->price = request('price')[$i];
$i++;
//I tried
$price_group = collect($item->price)->where('shop_id', $shop->id);
$min_price = min($price_group);
$item->save();
}
Route
Route::post('/{id}', 'Controller#store')->name('form_submit');
But it does not calculate the minimum price. When I dd($min_price), its total blank. What am I missing here?
I think is just...
$items = Item::where('shop_id', $shop->id)->get();
$min = $items->min('price');
And take a look on eager loading for relations. It's much better.
I solved this by #Jonas's solution. And I make this my answer.
Controller
public function store(Request $request, $id){
$shop = Shop::findorfail($id);
$item = Item::where('shop_id', $shop->id)->get(); //working fine
$i=0;
$price = request('price')
foreach( $items as $item)
$item->price = request('price')[$i];
$i++;
//This works
$min_price = min(request('price'));
$item->save();
}
For the past two days I was trying to build this relatively simple search system but I just can't figure this out..
Basically, I have a few select tags which contain options to filter the users..
Something like this:
<div class="col-sm-4">
<label for="price">Price:</label><br>
<select class="browse-select" name="price">
<option>Any</option>
<option value="1">Less than 100$</option>
<option value="2">More than 100$</option>
</select>
</div>
<div class="col-sm-4">
<label for="delivery">Delivery:</label><br>
<select class="browse-select" name="delivery">
<option>Any</option>
<option value="1">1 month or less</option>
<option value="7">7 days or less</option>
<option value="3">3 days or less</option>
</select>
</div>
Those are in a form (get request form) and here's my controller.
public function index(Request $request){
$users = User::paginate(10);
$service = Service::all();
//Search Logic
$category = $request->input('category');
$price = $request->input('price');
$delivery = $request->input('delivery');
$searchQuery = Service::with('user');
$searchQuery->where('category','=',$category);
if ($price == 1) { //price input has a value of 1 which means (less than 100)
$searchQuery->where('price','<',100)->where('category','=',$category);
}elseif($price == 2){
$searchQuery->where('price','>',100)->where('category','=',$category);
}
$result = $searchQuery->get();
return view('browse.index', compact('users','service','result'));
}
As you can see I have a relationship between Users and Services. Because I need to access user's services in order to compare the values and query the database.
What I have so far, works but only for one select. If I select a category, that works fine and the user is displayed. But, if I try for example to choose a category and also a price then it returns all the users.
How should I do this properly? I feel like I'm missing something here..
I believe you need to add additional checks and split your query. Something like
$searchQuery = Service::with('user');
if($request->has('category'))
{
$searchQuery->where('category', $request->input('category'));
}
if($request->has('price'))
{
$operator = (1 === $request->input('price')) ? '>' : '<';
$searchQuery->where('price', $operator, 100);
}
$result = $searchQuery->get();