How to add a progressbar in Laravel chunk function - laravel-5

I want to do something with customer table and there are lot of records so I will use chunk function to do it per 100 records. And I also want to add a progress bar to display the progress.
but the code I write does not work .
the error occurs at "$bar" variable.
$count = DB::table('customers')->count();
$bar = $this->output->createProgressBar($count);
DB::table('customers')->chunk(100, function ($customers,$bar) {
foreach($customers as $customer) {
//do something with customer
$bar->advance();
}
$bar->finish();
}

this is the correct way:
->chunk(100, function ($users) use ($bar){}

The previous answer is 50% correct.
DB::table('customers')->chunk is running per 100 rows, so you have to add $bar->finish(); out of the callback
$count = DB::table('customers')->count();
$bar = $this->output->createProgressBar($count);
DB::table('customers')->chunk(100, function ($customers,$bar) use($bar) {
foreach($customers as $customer) {
//do something with customer
$bar->advance();
}
});
$bar->finish();

Related

Why does Laravel eager loading not work with object functions?

$jobs = Job::with(['topics', 'topic'])->get();
// This works as intented, does not make extra queries.
foreach ($jobs as $job) {
$job->topics;
}
// This code produces extra queries even tho "topic" is loaded above.
foreach ($jobs as $job) {
$job->getLogo();
}
// Function in Job Model
function getLogo() {
return $this->topic->getLogo();
}
// Function in Topic Model
function getLogo() {
return $this->logo;
}
Even though $job has loaded topic it still makes extra queries. How do I resolve this?
It looks like your post is mostly code; please add some more details.

Laravel count column of related model

I want to count the number of upvotes and downvotes from ScripRating for a certain Script.
Script.php:
public function ratings()
{
return $this->hasMany('ScriptRating');
}
ScriptRating.php:
public function script()
{
return $this->belongsTo('Script');
}
The script_rating database table:
id (primary, increments)
script_id(integer)
rating(integer) <-- Can be either 1 (upvote) or -1 (downvote)
To retrieve a script and display the ratings:
$script = Script::where('title', '=', $title)->get();
{{ $script->ratings }}
This works fine, it returns an array: [{"id":1,"script_id":1,"rating":1}]. But at this point I'm stuck. How could I count the total upvotes and downvotes for a certain script?
I also have one more small question what I'm finding confusing. This does the same as the code above:
$script = Script::where('title', '=', $title)->with('ratings')->get();
{{ $script->ratings }}
What is the difference between these two methods and which one should I use?
Thanks in advance!
Edit
I made three scopes:
public function scopeTotalRating($query, $scriptId) {
return $query->where('script_id', $scriptId)->get()->sum('rating');
}
public function scopeThumbsUp($query, $scriptId) {
return $query->where('script_id', $scriptId)->having('rating', '=', 1)->get()->sum('rating');
}
public function scopeThumbsDown($query, $scriptId) {
return $query->where('script_id', $scriptId)->having('rating', '=', -1)->get()->sum('rating');
}
And display them as following:
{{ ScriptRating::thumbsUp($script->id) }}
You can use
{{ $script->ratings->count() }}
This will display the number total ratings a script has.
However what you're interested in doing is grouping the ratings into upvotes and downvotes, so you'll need to query your relationship by a group by clause.
Script::where('title', '=', $title)->with([
'ratings' => function($query) {
$query->groupBy('rating');
})
])->get();
I believe the collection returned should be now grouped by 1 and -1. Let me know of the results!
EDIT: You can also take a look here at the documentation on querying relationships:
http://laravel.com/docs/4.2/eloquent#querying-relations
EDIT for response:
The simplest way to do this without using group by would be separate queries:
$script = Script::where('title', $title)->first();
if ($script) {
$upvotes = ScriptRating::where('script_id', $script->id)->having('rating', '>', 0)->get()->count();
$downvotes = ScriptRating::where('script_id', $script->id)->having('rating', '<', 0)->get()->count();
}
Also the difference between your scripts mentioned is called eager loading or lazy loading. When you specify ->with() in your query, this is called eager loading. If you don't do this, the query will be ran when you specify $script->ratings
More about eager/lazy loading here:
http://laravel.com/docs/4.2/eloquent#eager-loading
Edit for another response:
You would use the ->whereHas('ratings') function if you want to gather scripts that only have ratings. You can also check the existence of the script having ratings by doing an if statement:
if ($script->ratings->count() > 0) {
// The script has ratings
} else {
// The script does not have ratings
}
If you don't want to keep repeating this code you could always put a function inside your Script.php model by using the following:
public function hasRatings()
{
return $this->ratings->count() > 0;
}
Then you can do:
if ($script->hasRatings())
You can add to the Script model class those 2 functions:
public function ratingsSumRelation()
{
return $this->hasOne('ScriptRating')->selectRaw('script_id, sum(rating) as sum_all')
->groupBy('script_id');
}
public function getRatingSumAttribute()
{
return $this->ratingsSumRelation ?
$this->ratingsSumRelation->sum_all: 0;
}
and now display sum using:
{{ $script->rating_sum }}

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;
}

how to combine results of query and return that result using codeigniter

i am new with codeigniter.
i have used the following code to execute query recursively.
Suppose $q query select 4 id (10,11,20,24)
then for each id showreply function (in foreach) call recursively then how can return the combine result.
$resultq3 = $this->showreply($reply_id);
<?php
public function showreply($reply_id)
{
$q1 =$this->db->select('*')
->from('forum_reply AS fr')
->where('fr.parent_id',$reply_id1)
->order_by('fr.id ')->get();;
foreach($q1->result_array() as $row4)
{
$id = $row4['id'];
$parent_id = $row4['parent_id'];
if($parent_id!=0)
{
$this->showreply($id);
}
}
return $result;
}
?>
I'm not really understanding what it is you're asking here. Maybe showing the showReply function would help, but you already have the combined result and are splitting that out in your foreach so what's the problem? Also why are you assigning reply_id to reply_id1? What is the point of that? Just use $reply_id in your query.
You're also executing an if statement that makes little sense since you can filter out the id's you don't want in the query itself (and are you seriously ever going to have an id that = 0?)
In fact the more I look at this code the more confused I become. Where is $id getting populated for $this->showreply($id)?
<?php
public function showreply($reply_id)
{
$q1 =$this->db->select('*')
->from('forum_reply AS fr')
->where('fr.parent_id',$reply_id)
->where('fr.parent_id !=',0)
->order_by('fr.id ')->get();;
//$i=0;
foreach($q1->result_array() as $row4)
{
$parent_id = $row4['parent_id'];
$this->showreply($id);
}
//below is the actual answer to your question on how to return the combined results.
return $q1->result_array();
}
?>
Okay after rereading your question I think I have a better understanding. If you pass the id's as an array like this:
$reply_id = array(10,11,20,24);
You can then modify your query to use:
$this->db->where_in('fr.parent_id', $reply_id);
That will return the results as one combined result with all 4 ids included.

codeigniter count_all_results

I'm working with the latest codeIgniter released, and i'm also working with jquery datatables from datatables.net
I've written this function: https://gist.github.com/4478424 which, as is works fine. Except when I filter by using the text box typing something in. The filter itself happens, but my count is completely off.
I tried to add in $res = $this->db->count_all_results() before my get, and it stops the get from working at all. What I need to accomplish, if ($data['sSearch'] != '') then to utilize the entire query without the limit to see how many total rows with the search filter exists.
If you need to see any other code other than whats in my gist, just ask and I will go ahead and post it.
$this->db->count_all_results() replaces $this->db->get() in a database call.
I.E. you can call either count_all_results() or get(), but not both.
You need to do two seperate active record calls. One to assign the results #, and one to get the actual results.
Something like this for the count:
$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
And for the actual query (which you should already have):
$this->db->select($your_columns);
$this->db->from('table');
$this->db->where($your_conditions);
$this->db->limit($limit);
$query = $this->db->get();
Have you read up on https://www.codeigniter.com/userguide2/database/active_record.html#caching ?
I see you are trying to do some pagination where you need the "real" total results and at the same time limiting.
This is my practice in most of my codes I do in CI.
$this->db->start_cache();
// All your conditions without limit
$this->db->from();
$this->db->where(); // and etc...
$this->db->stop_cache();
$total_rows = $this->db->count_all_results(); // This will get the real total rows
// Limit the rows now so to return per page result
$this->db->limit($per_page, $offset);
$result = $this->db->get();
return array(
'total_rows' => $total_rows,
'result' => $result,
); // Return this back to the controller.
I typed the codes above without testing but it should work something like this. I do this in all of my projects.
You dont actually have to have the from either, you can include the table name in the count_all_results like so.
$this->db->count_all_results('table_name');
Count first with no_reset_flag.
$this->db->count_all_results('', FALSE);
$rows = $this->db->get()->result_array();
system/database/DB_query_builder.php
public function count_all_results($table = '', $reset = TRUE) { ... }
The
$this->db->count_all_results();
actually replaces the:
$this->db->get();
So you can't actually have both.
If you want to do have both get and to calculate the num rows at the same query you can easily do this:
$this->db->from(....);
$this->db->where(....);
$db_results = $this->get();
$results = $db_results->result();
$num_rows = $db_results->num_rows();
Try this
/**
* #param $column_name : Use In Choosing Column name
* #param $where : Use In Condition Statement
* #param $table_name : Name of Database Table
* Description : Count all results
*/
function count_all_results($column_name = array(),$where=array(), $table_name = array())
{
$this->db->select($column_name);
// If Where is not NULL
if(!empty($where) && count($where) > 0 )
{
$this->db->where($where);
}
// Return Count Column
return $this->db->count_all_results($table_name[0]);//table_name array sub 0
}
Then Simple Call the Method
Like this
$this->my_model->count_all_results(['column_name'],['where'],['table name']);
If your queries contain a group by, using count_all_results fails. I wrote a simple method to work around this. The key to preventing writing your queries twice is to put them all inside a private method that can be called twice. Here is some sample code:
class Report extends CI_Model {
...
public function get($page=0){
$this->_complex_query();
$this->db->limit($this->results_per_page, $page*$this->results_per_page);
$sales = $this->db->get()->result(); //no table needed in get()
$this->_complex_query();
$num_results = $this->_count_results();
$num_pages = ceil($num_results/$this->results_per_page);
//return data to your controller
}
private function _complex_query(){
$this->db->where('a', $value);
$this->db->join('(subquery) as s', 's.id = table.s_id');
$this->db->group_by('table.column_a');
$this->db->from('table'); //crucial - we specify all tables here
}
private function _count_results(){
$query = $this->db->get_compiled_select();
$count_query = "SELECT count(*) as num_rows FROM (".$query.") count_wrap";
$r = $this->db->query($count_query)->row();
return $r->num_rows;
}
}

Resources