Laravel multiple where AND conditions not working - laravel

I want to sort the car product details by the below query. But it returns an empty array. Please help me.
public function fetchall($min_price, $max_price, $milage, $old_year, $new_year, $carbrand)
{
$cars = Cars::where([
['price','>=',$min_price],
['price','<=',$max_price],
['year','>=',$old_year],
['year','<=',$new_year],
['milage','=', $milage],
['model_id','=', $carbrand]])->get();
if (!empty($cars)) {
return $cars;
} else {
return "Check your details and submit again";
}

Instead of using !empty($cars) use !$cars->isEmpty().
$cars is an object of Illuminate\Support\Collection so empty() won't work.
See Available Methods for Illuminate\Support\Collection.

Related

Using latestOfMany() in Laravel

If I have a model called Placement which can have many PlacementTqLimit and I want to get the latest one, I'm assuming I can use this.
public function latestLimit()
{
return $this->hasOne(PlacementTqLimit::class)->latestOfMany();
}
And then add a custom attribute to get the value
public function getLatestLimitValueAttribute()
{
if (empty($this->latestLimit())) {
return '100';
}
return $this->latestLimit()->value;
}
However, when I dump the $this->latestLimit() I'm getting a Illuminate\Database\Eloquent\Relations\HasOne... I'm sure I shouldn't have to call return $this->hasOne(PlacementTqLimit::class)->latestOfMany()->latest(). Am I doing something wrong here?
$this->latestLimit() will return hasOne that extend query builder.
If you want to get the latest of PlacementTqLimit model just use $this->latestLimit without () or you can also use $this->latestLimit()->first()
So, to get the latest value limit, example code is:
public function getLatestLimitValueAttribute()
{
if (!$this->latestLimit) {
return '100';
}
return $this->latestLimit->value;
}

How to get the value from Laravel Collection

I have a Model called OfficeHours which as various attributes and the model User belongsTo OfficeHours. I am trying to fetch a particular element from a collection but am getting blank in blade.
public function office_hours() {
return $this->hasMany(OfficeHours::class);
}
public function user(){
return $this->belongsTo(User::class);
}
In Blade when i do the following:
{{$logged_in_user->office_hours->where('dow',2)}}
it works and gets me the following collection:
{"2":{"id":3,"user_id":4,"dow":2,"fromtime":"07:00:00","totime":"16:00:00","created_at":"2019-01-31 14:48:32","updated_at":null}}
now how i do i get the elements of that collection?
i tried
{{$logged_in_user->office_hours->where('dow',2)->get('fromtime')}}
but it gives me blank
How do i access the elements?
To preface, you shouldn't be doing that kind of logic in the view file, this is what controllers are for. You'd be able to do $fromtime = ...; and pass that to the view via return view(...)->with(["fromtime" => $fromtime]);
But, that being said, you should be able to do
{{ $logged_in_user->office_hours->where("dow", 2)->first()->fromtime }}
Note you're gonna get an error if ->where("dow", 2) doesn't return a Collection, as null doesn't have a ->first() method on it.

How to get a value in controller from get method and return it back to another view?

The problem looks basic but it is really painful!
I'm using get method and getting value in controller and I want the same value to return in another view.
How can I do that???
Please help!!!
This is my function from controller:
public function guest(){
if (Input::get('Cash On Delivery')){
$get = Input::get('Cash On Delivery');
return Redirect::to('guest/guestview/'.$get);
}
Well, with regards to your answer, using $_REQUEST directly is not Laravel's way of doing things :(
I believe this is better
public function guest(Request $request)
{
if ($request->payment_method == ('Cash On Delivery'))
{
return view('guest/guestview', ['guest'=>$request->payment_method]);
}
}
Ok Guys, I figured it out,
Just do this below.
public function guest(Request $request){
if ($request->payment_method == ('Cash On Delivery')){
$get = $_REQUEST['payment_method'];
return view('guest/guestview', compact('get'));
}

how to pass arguments to filter in laravel4.2

in my project , i want to pass 'user_id' to filter to check.
this is isAdmin function:
public function isAdmin($id){
$user=User::find($id);
if($user->role==10){
return true;
}
return false;
}
and this is my route file:
Route::group(array('before'=>'admin'),function(){
Route::get('dashboard','UserController#Dashboard');
});
this is my filte that iwant give user_id ($id), and idont know how.
Route::filter('admin',function($id)
{
if(Auth::guest() or ! Auth::user()->isAdmin($id))
return Redirect::guest('user/logout');
});
For this case, you can directly check from the filter like this:
Route::filter('admin',function()
{
if(Auth::guest() or Auth::user()->role != 10)
return Redirect::guest('user/logout');
});
You do not need to pass params.
Also have a look to this link if you really want to pass params.
Link of Laravel Doc: here

getting the value of the single field output using the codeigniter active record

the following function is supposed to read the name of the given asset code from the database. but it triggers the error: "Trying to get property of non-object"
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get()->result()->row('name');
}
All I want is to have the name of the asset returned back to the controller! Your help is highly appreciated!
Use row() like,
return $this->db->get()->row()->name;
Use row() for a single row, and result() for multiple rows.
do like this, asset_types is your table name
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get('asset_types');
}
And in your controller acess it like
$result=$this->modelname->sban_name('$asset')->row();
$name=$result->name;
I think it's important to check if the record that satisfies the conditions even exists in the database. Code for the model:
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
$row = $this->db->get()->row();
if (isset($row)) {
return $row->name;
} else {
return false;
}
}
Simply call the function from the controller like so:
$response = $this->model_name->sban_name($asset)
Try this code of block , I already checked and works fine:
function sban_name($asset)
{
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code', $asset);
return $this->db->get()->row()->name;
}

Resources