Laravel Query Builder Issue - mysql

I'm trying to make it so that the items pulled from the database are only from the current month, however when I do this using various methods it throws an error (which I can't debug due to a the issue mentioned here). In the code below if I just simply paginate the full results it works fine, but when I start to use query builder methods it gives an error, does anybody have any idea why this error is occurring?
public function index()
{
// $trades=Trade::paginate(10);
$currentMonth = date('m');
$trades = DB::table('trades')->whereMonth('date', $currentMonth)->paginate(10);
// dd($trades);
$pastwinners=Winner::paginate(10);
return view('raffle', compact('pastwinners'), compact('trades'));
}
As you can see above the $pastwinners variable and the first $trades variable provide the proper values, but when I'm trying to select only the ones from this month (using the 'date' field) it seems to break.

Try this one. And if you want to paginate your results, do not forget to add pagination links to your view. Also do not forget the import Carbon in your controller class.
https://laravel.com/docs/5.6/pagination
public function index()
{
$trades = Trade::whereMonth('date' , Carbon::today()->month)->paginate();
$pastwinners = Winner::all();
return view('raffle', compact('pastwinners', 'trades');
}

Related

Wrong GridView total rows count

I'm getting a weird result.
In my CustomerQuery i have some filters applied before the all() function.
(like
$this->andWhere(['profiles.type'=>'f']);
)
The filter gets applied before every find()->all(), but in the pagination count before the gridview it states
Showing 1-6 of 12 elements
If i check the query with Yii's debugger, i can see the count query used by yii to determine the number of elements is missing the profiles.type = "f" while the actual query doesn't.
Is this a bug? Is there another function i need to use instead of andWhere to apply the same filter to the count query?
Ok, it seems this in expected result (i'd expect and definitely prefer the count function to execute the exact same query, though).
To apply the same filter/where to both the real query and the count query you have to override the ActiveQuery's count method.
Here's an example
class ProfileQuery extends \yii\db\ActiveQuery{
...
public function standardFilters(){
$this->andWhere(['not',['table_name.status'=>2]]);
}
public function all($db = null){
$this->standardFilters();
return parent::all($db);
}
public function count($q = '*', $db = null) {
$this->standardFilters();
return parent::count($q, $db);
}
...

passing two parameters in index function using codeigniter

I want to pass two parameters in index function using codeigniter but its giving me error i don't know why its giving me error.
here is my controller
function index($one = null,$two = null)
{
$data['title'] = "Product Page ";
$this->load->view('home/header/header',$data);
$this->load->view('home/css/css');
$this->load->view('home/navbar/navbar2');
$this->load->view('products/product');
$this->load->view('home/footer/footer');
$this->load->view('home/js/js');
}
here is my routes setting
$route['product/(:any)'] = 'product/index/$1';
When i set my index function in one parameter its working fine but its not working with two parameters.
You can try with this, it will be working
$route['product/(.*)/(.*)']='product/index/$1/$2';

How to use Transactions in cakephp

I write this code, but its not working, it gives this error
Call to a member function commit() on a non-object
Hear is my code
$datasource = $this->Arrear->getDataSource();
$datasource->begin();
if($this->Customar->saveField("total_bake",$amount) && $this->Arrear->save()){
$dataSource->commit();
return $this->redirect(array('action'=>'index'));
}else{
$dataSource->rollback();
$this->Session->setFlash('Data insert Failed','failure');
}
Variables in php(and hence in cakephp as well) are case-sensitive
http://php.net/manual/en/language.variables.basics.php
you have in your first line
$datasource = $this->Arrear->getDataSource();
but you are committing like
$dataSource->commit();
you have the data source assigned to $datasource, but not to $dataSource. The last variable even is not defined, that is why it is showing that error. So, you have to be sure you are using exactly the same variable (with same capitalization) in all places.

How to pass one of the mysql query results in the model from controller?

In my controller I have called a model that makes a mysql query to fetch information from table. It is working okay but now under the same function I want to call another model and make a query based on one of the results from the previous mysql query. (The field name which I want to get the result of is "batch") . I have tried to get the value (batch) right in my controller, pass it into the model and then tried to make the second query but it seems like the second model is not getting the value from the controller and hence its not working. Would you please kindly help me with this?
Thanks in Advance :)
Here is my Controller
function Get($id){
$this->load->model('mod_studentprofile');
$data['query']= $this->mod_studentprofile->student_get($id);
// To get the batch name
$batch= $query ['batch']; // This I get from the above query result.
$this->load->model('batchname');
$data['query1']= $this->batchname->batchname($batch);
$data['tab'] = "Student Profile";
$data['main_content']='studentprofile';
$this->load->view('includes/template',$data);
}
Here is my model number 1
function student_get($id)
{
$query=$this->db->get_where('student',array('studentid'=>$id));
return $query->row_array();
}
Here is my model number 2
function batchname($batch)
{
$query1=$this->db->get_where('batch',array('batchid'=>$batch));
return $query1->row_array();
}
Well, you're not actually assigning anything to $batch, how about this:
$batch= $data['query'];
This now passes the variable along. As a side note, you can pass it as a paramater and be done with it in a single line, dependency injection style:
$data['query1']= $this->batchname->batchname($this->mod_studentprofile->student_get($id));
Are you getting a value back from the first query?
I would log the value you are getting back from the first query in your controller.
log_message('debug', 'Batch value is '.$batch);
And also put in some debug to see what the query that is being ran.
function batchname($batch)
{
$query1=$this->db->get_where('batch',array('batchid'=>$batch));
$str = $this->db->last_query();
log_message('debug', 'Batchname Query: '.$str);
return $query1->row_array();
}

How to alter this LINQ to SQL so it doesn't bomb at runtime?

public IQueryable<Story> FindAllStories(){
var stories = (from s in db.Stories
orderby s.DateEntered descending
select new Story
{
Title = s.Title,
UserName = s.aspnet_User.UserName
}
);
return stories;
}
I need to pass this as IQueryable so the pagination helper I found online can only pull the items I need. The problem is that at runtime when the helper tries to do source.Count(); the compiler isn't a happy camper because it's an 'explicit contruction of an entity type query'.
How would I alter this LINQ to SQL method so this does not happen?
Also, to help me grasp this, why does the previous code not work and this one does?
public IQueryable<Story> FindAllStories(){
var stories = (from s in db.Stories
orderby s.DateEntered descending
select s);
return stories;
}
Update
I'm beginning to think the way to accomplish this (verified it works) is to create a POCO called UserStory. The new class has 2 properties: one of type Story and the other string UserName. I can then return an IQueryable of UserStory without a problem.
That's great; however, I still don't get why that method would work and my other doesn't. The other is adding a property of string UserName to my partial class Story and passing that object between layers. What's the difference?
The following link is to a blog post tat describes an issue similar to yours. It seems like the solution was to return a type that inherets from Story instead of a Story type:
http://devlicio.us/blogs/derik_whittaker/archive/2008/04/25/linq2sql-explicit-construction-of-entity-exception.aspx
Hope this helps.