Construct Laravel query using Conditionals - mysql

I am trying to create a query based on data sent from a POST request. I got it working when I expect both values in my form to be set, however I would like one of the values to be optional.
In my Controller if I make that call
$year = request()->year;
if(request()->filled('month')){
$month = sprintf("%02d",request()->month);
}
That works for correctly setting $year and $month, but I run in to issues in my query. This is what my query looks like:
$matches = DB::table('games')
->join('clubs', 'games.opposition','=','clubs.id')
->join('stadiums', 'games.stadium','=','stadiums.id')
->select('date','clubs.name AS opposition','games.home/away','stadiums.name')
->whereYear('date',$year)
->whereMonth('date',$month)
->get();
I want that ->whereMonth statement to only be added if the month value is set in the form, so I tried to surround it with
->when(request()->filled('month'),function ($q) use ($month) {
return $q->whereMonth('date',$month);
})
But it's giving me a Undefined variable: month error.
Does anyone have any suggestions? I tried to replace request()->filled('month) with just $month but it gave me the same error.

It is because you don't set month if it's not there but it should work as the conditions are the same. Wouldn't simplifying it help, doing it all in one statement, instead of setting it beforehand. Then you don't have the problem of having instantiated the property.
->when(request()->filled('month'), function ($q) {
return $q->whereMonth('date', sprintf("%02d",request()->month));
})

Related

Laravel keeps getting the same cache result even if I input different search keyword

I got a api request that has a parameter in it which is projectname. The problem is when I search for example A the results will be A but when I search for B the result is till A even if I search C the result is still the same. I think the cache saved the first results from the first search string. My question is how could I save every results in every search query without getting the same result based on the search query?
Here is my code
public function getRecordDetails(Request $request){
if(!empty($request->limit)){
$limit = " LIMIT ".$_REQUEST['limit'];
}
else{
$limit= '';
}
if(empty($request->projectname)){
dd('Field is empty');
}
else{
$data = Cache::rememberForever('results', function () use($request) {
$result = DB::connection('mysql2')
->table('xp_pn_ura_transactions')
->whereRaw(DB::raw("CONCAT(block, ' ', street,' ',project_name,' ', postal_code,'')LIKE '%$request->projectname%' order by STR_TO_DATE(sale_date, '%d-%M-%Y') desc"))
->limit($request->limit)
->distinct()
->get();
$count = DB::connection('mysql2')
->table('xp_pn_ura_transactions')
->whereRaw(DB::raw("CONCAT(block, ' ', street,' ',project_name,' ', postal_code,'')LIKE '%$request->projectname%'"))
->count();
return json_encode(array('count'=>$count,'result'=>$result));
});
return $data;
}
}
PS: This question is based here How could I cache every api response results in my query in Laravel? I answered here but this is different problem based on my answer. Thanks for helping.
Laravel find the Cache by the key. You're using results as your key.
So no matter how different request you pass. It still can find the cache by results.
So it will return the first cache you store in results.
$key = "results:".$request->projectname.':' $request->limit;
Cache::rememberForever($key, function () use ($request) {
...
}
This one will store every different projectname you request.
However
Problem 1:
There are so many diff possibilities that user can request.
I don't think it is a good idea to store all these cache. If there are not that much, it is ok.
Solution:
Or you can use remember() instead of rememberForever()
$ttl = ????; // Find the appropriate time to expire the cache
$value = Cache::remember($key, $ttl, function () {});
Problem 2:
There is a $request->limit in your cache.
That means if someone insert or delete a record in that table. next time you request with another limit, you will face the duplicated records.
Solution:
So I think you can clear the cache after you create , update or delete the records.
Because you are using the same cache slug over and over. You should change the cache slug according to the changed input. Adding the $request as a use argument to your function will not magically change the cache slug.
In your case, this should work:
Cache::rememberForever("results_{$request->projectname}", function () use ($request) {
you should add text value after and before key id like bello
Cache::rememberForever('product_'.$product->id.'_key',function ()
});

Laravel - Eloquent - Filter based on latest HasMany relation

I have this two models, Leads and Status.
class Lead extends Model
{
public function statuses() {
return $this->hasMany('App\LeadStatus', 'lead_id', 'id')
->orderBy('created_at', 'DESC');
}
public function activeStatus() {
return $this->hasOne('App\LeadStatus', 'lead_id', 'id')
->latest();
}
}
class LeadStatus extends Model
{
protected $fillable = ['status', 'lead_id'];
}
This works fine, now I'm trying to get all Leads based on the 'status' of the last LeadStatus.
I've tried a few combinations with no success.
if ($search['status']) {
$builder = $builder
->whereHas('statuses', function($q) use ($search){
$q = $q->latest()->limit(1);
$q->where('status', $search['status']);
});
}
if ($search['status']) {
$builder = $builder
->whereHas('status', function($q) use ($search){
$q = $q->latest()->Where('status', $search['status']);
});
}
Has anybody done this with Eloquent? Do I need to write some raw SQL queries?
EDIT 1: I'll try to explain again :D
In my database, the status of a lead is not a 1 to 1 relation. That is because I want to have a historic list of all the statuses which a Lead has had.
That means that when a Lead is created, the first LeadStatus is created with the status of 'new' and the current date.
If a salesman comes in, he can change the status of the lead, but this DOES NOT update the previous LeadStatus, instead it creates a new related LeadStatus with the current date and status of 'open'.
This way I can see that a Lead was created on 05/05/2018 and that it changed to the status 'open' on 07/05/2018.
Now I'm trying to write a query using eloquent, which only takes in count the LATEST status related to a Lead.
In the previous example, if I filter by Lead with status 'new', this Lead should not appear as it has a status of 'open' by now.
Hope this helps
Try this:
Lead::select('leads.*')
->join('lead_statuses', 'leads.id', 'lead_statuses.lead_id')
->where('lead_statuses.status', $search['status'])
->where('created_at', function($query) {
$query->selectRaw('max(created_at)')
->from('lead_statuses')
->whereColumn('lead_id', 'leads.id');
})->get();
A solution using the primary key (by Borjante):
$builder->where('lead_statuses.id', function($query) {
$query->select('id')
->from('lead_statuses')
->whereColumn('lead_id', 'leads.id')
->orderBy('created_at', 'desc')
->limit(1);
});
I had this same problem and posted my solution here but I think it's worth re-posting as it improves on the re-usability. It's the same idea as the accepted answer but avoids using joins, which can cause issues if you want to eager load relations or use it in a scope.
The first step involves adding a macro to the query Builder in the AppServiceProvider.
use Illuminate\Database\Query\Builder;
Builder::macro('whereLatestRelation', function ($table, $parentRelatedColumn)
{
return $this->where($table . '.id', function ($sub) use ($table, $parentRelatedColumn) {
$sub->select('id')
->from($table . ' AS other')
->whereColumn('other.' . $parentRelatedColumn, $table . '.' . $parentRelatedColumn)
->latest()
->take(1);
});
});
This basically makes the sub-query part of the accepted answer more generic, allowing you to specify the join table and the column they join on. It also uses the latest() function to avoid referencing the created_at column directly. It assumes the other column is an 'id' column, so it can be improved further. To use this you'd then be able to do:
$status = $search['status'];
Lead::whereHas('statuses', function ($q) use ($status) {
$q->where('status', $userId)
->whereLatestRelation((new LeadStatus)->getTable(), 'lead_id');
});
It's the same logic as the accepted answer, but a bit easier to re-use. It will, however, be a little slower, but that should be worth the re-usability.
If I understand it correctly you need / want to get all Leads with a specific status.
So you probably should do something like this:
// In your Modal
public function getLeadById($statusId)
{
return Lead::where('status', $statusId)->get();
// you could of course extend this and do something like this:
// return Lead::where('status', $statusId)->limit()....->get();
}
Basically I am doing a where and returning every lead with a specific id.
You can then use this function in your controller like this:
Lead::getLeadById(1)

Laravel Select Query Accessing Returned Data

I've been working with Laravel for a short time, and am confused about accessing data retrieved from queries. I'm trying to save data to variable but am getting the following:
Trying to get property of non-object
Queries tried:
$data = DB::table('table_1')->select('user_id', 'email')->where('email', '=', Input::get('email_address'))->get();
// also tried
$data = DB::table('table_1')->where('email', '=', Input::get('email_address'))->pluck('user_id');
// accessing data
$userID = $data->user_id;
Both return the same.
To elaborate a little bit, ->get() will return a Collection. You might iterate over it as an array, but you can profit from methods that this class offers. pluck is one of these.
That's why $userID = $data->user_id; wouldn't work.
first() did the trick, rather than get().
$data = DB::table('table_1')->select('user_id', 'email')->where('email', '=', Input::get('email_address'))->first();

Laravel Fluent add select()s in separate places?

//Earlier in the code, in each Model:
query = ModelName::select('table_name.*')
//Later in the code in a function in a Trait class that is always called
if ($column == 'group_by')
{
$thing_query->groupBy($value);
$thing_query->select(DB::raw('COUNT('.$value.') as count'));
}
Is there a way to append or include a separate select function in the eloquent query builder?
The actual ->select() is set earlier and then this function is called. I'd like to add the count column conditionally in this later function that has the query passed into it.
For future reference, you can use the addSelect() function.
It would be good to have in the documentation, but you'll find it here in the API: http://laravel.com/api/4.2/Illuminate/Database/Query/Builder.html#method_addSelect
Yeah you just insert the block you wanna execute as a function....according to the documentation on Parameter Grouping , you can do like so...by passing the Where a function...
This code below probably wont do what you want, but, its something for you to build off of, and play around with.
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->group_by($value);
->select(DB::raw('COUNT('.$value.') as count'));
})
->get();
Try this:
$thing_query->groupBy($value)->get(DB::raw('COUNT('.$value.') as count'));
Also,if you are just trying to get the count and not select multiple things you can use ->count() instead of ->get()

What is wrong with this Code Igniter mySQL query?

I have two tables for storing information about a user. One is for authentication, the other is information the user will enter themselves. I am writing a model that will be used when the user interacts with this information. The following method is to return data for display and modification.
I need a query that will return 'email' and 'username' from $accounts_table and * from $profiles_table. I can't seem to get my head around the JOIN syntax though. I understand how joins work, but my queries throw sentax errors.
function get_userdata($id){
$data = array();
$this->db->get_where($this->profiles_table, array('user_id' => $id));
$this->db->join($this->accounts_table.'.email', $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
$data= $this->db->get();
return $data;
}
I see a couple of issues:
You should be using $this->db->where(), instead of $this->db->get_where(). get_where() executes the query immediately.
$this->db->get_where('user_id', $id);
Also the first argument of $this->db->join() should only be the table name, excluding the field.
$this->db->join($this->accounts_table, $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
And you're returning $data which is just an empty array(). You would need to pass the query results to $data like this:
$data = $record->result_array();
get_where executes the query. So, your join is its own query, which doesn't work.
You need to break get_where into where and from.
Also, in MySQL, you JOIN a table, not a field. If you want that field, add it to the SELECT.
$this->db->select($this->profiles_table.'.*');
$this->db->select($this->accounts_table.'.email,'.$this->accounts_table.'.username');
$this->db->from($this->profiles_table);
$this->db->where('user_id', $id);
$this->db->join($this->accounts_table, $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
$data = $this->db->get();
NOTE: $this->db->get() returns a query object, you need to use result or row to get the data.
I think you've a mistake:
$this->db->join($this->accounts_table.'.email', $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
First parameter should a table NOT a field: $this->accounts_table.'.email' is wrong IMHO. Or only a typo :)