I am trying to work out the best practice to calculate Avg Attendance.
I would like to show this on the Member View (html part is no issue)
I have the following 3 databases
Members DB
|id|first|last|..........
|1 |.....................
|2 |.....................
Rolls DB
|id|roll_id|member_id|status|
|1 |1 |1 |P |
|2 |1 |2 |V |
|3 |2 |1 |A |
|4 |2 |2 |C |
Rollmappings DB
|id|roll_date |roll_year|
|1 |2019-03-01|2019 |
|2 |2019-03-02|2019 |
I have the following in my Member Model
public function roll()
{
return $this->hasMany('App\Roll');
}
I have the following in my Roll Model
public function rollmapping()
{
return $this->hasOne('App\Rollmapping', 'id','roll_id');
}
I would like to count all records which does not equal "A"
So in the code blocks above Member 1 will needs to show me 50% attendance, and member 2 will show me 100% attendance.
I would need to have something in place which will auto roll over when we move into 2020
I do Have Carbon Date installed with the app as well
I got stuck on the link on pulling the year_roll into the select for the Roll and to count I can use:
$rollyear = Rollmapping::lastest()->value('roll_year');
$totalweeks = DB::table('rollmappings')
->where('roll_year', '=', $rollyear)
->get()
->count();
But I am stuck on on pulling the correct records from the roll using the roll_id as I only want the roll_id which have a year date of the current year.
Thanks
If I understand you're question correctly then you should be able to get what you need using whereHas
$count = Roll::whereHas('rollmapping', function ($query) {
$query->whereYear('roll_date', now()->year);
})->count();
If you would also like to exclude all the rolls where the status does not equal A then you would have something like:
$count = Roll::whereHas('rollmapping', function ($query) {
$query->whereYear('roll_date', now()->year);
})
->where('status', '!=', 'A')
->count();
Related
I want to filter product using their attributes using below query
return Product::with('attributes')
->whereHas('attributes', function ($query) use ($attribute_id_array,$attribute_value_array){
for($k=0;$k<count($attribute_id_array);$k++)
{
$attr_id=$attribute_id_array[$k];
$attr_val=$attribute_value_array[$k];
$query->where(function($q) use ($attr_id, $attr_val) {
$q->where('attribute_id', $attr_id)->where('value', $attr_val);
});
}
})->paginate(10);
I want to check if attributes of a product exist then filter it using the loop to filter out products that are in attributes_value & attributes_id array which does not match but presently this query is not filtering through attributes.
Here are the structure of tables and relations
Product Table :
|id | Title | Price|
-------------------------
|1 | Title 1 | 5000|
|2 | Product 2 | 7000|
and the other is
product_attribute table:
|id | product_id | attribute_id | attribute_name | value|
---------------------------------------------------------
|1 | 1 | 5 | Color | Red |
|2 | 1 | 6 | Size | XL |
|3 | 2 | 5 | Color | Green|
Product and Product attribute is related with following relation (In the product model):
public function attributes()
{
return $this->hasMany(ProductsAttribute::class, 'product_id ');
}
I believe you need to validate each attribute and value combinations to be present for the resultant products, so you can put your whereHas clause inside the loop as
$products = Product::with('attributes');
for($k=0; $k<count($attribute_id_array); $k++)
{
$attr_id=$attribute_id_array[$k];
$attr_val=$attribute_value_array[$k];
$products->whereHas('attributes', function ($query) use ($attr_id,$attr_val){
$query->where('attribute_id', $attr_id)->where('value', $attr_val);
});
}
$products = $products->paginate(10);
Sorry for a newbie question.
Currently I have log files which contains fields such as: userId, event, and timestamp, while lacking of the sessionId. My aim is to create a sessionId for each record based on the timestamp and a pre-defined value TIMEOUT.
If the TIMEOUT value is 10, and sample DataFrame is:
scala> eventSequence.show(false)
+----------+------------+----------+
|uerId |event |timestamp |
+----------+------------+----------+
|U1 |A |1 |
|U2 |B |2 |
|U1 |C |5 |
|U3 |A |8 |
|U1 |D |20 |
|U2 |B |23 |
+----------+------------+----------+
The goal is:
+----------+------------+----------+----------+
|uerId |event |timestamp |sessionId |
+----------+------------+----------+----------+
|U1 |A |1 |S1 |
|U2 |B |2 |S2 |
|U1 |C |5 |S1 |
|U3 |A |8 |S3 |
|U1 |D |20 |S4 |
|U2 |B |23 |S5 |
+----------+------------+----------+----------+
I find one solution in R (Create a "sessionID" based on "userID" and differences in "timeStamp"), while I am not able to figure it out in Spark.
Thanks for any suggestions on this problem.
Shawn's answer regards on "How to create a new column", while my aim is to "How to create an sessionId column based on timestamp". After days of struggling, the Window function is applied in this scenario as a simple solution.
Window is introduced since Spark 1.4, it provides functions when such operations is needed:
both operate on a group of rows while still returning a single value for every input row
In order to create a sessionId based on timestamp, first I need to get the difference between user A's two immediate operations. The windowDef defines the Window will be partition by "userId" and ordered by timestamp, then diff is a column which will return a value for each row, whose value will be 1 row after the current row in the partition(group), or null if the current row is the last row in this partition
def handleDiff(timeOut: Int) = {
udf {(timeDiff: Int, timestamp: Int) => if(timeDiff > timeOut) timestamp + ";" else timestamp + ""}
}
val windowDef = Window.partitionBy("userId").orderBy("timestamp")
val diff: Column = lead(eventSequence("timestamp"), 1).over(windowDef)
val dfTSDiff = eventSequence.
withColumn("time_diff", diff - eventSequence("timestamp")).
withColumn("event_seq", handleDiff(TIME_OUT)(col("time_diff"), col("timestamp"))).
groupBy("userId").agg(GroupConcat(col("event_seq")).alias("event_seqs"))
Updated:
Then exploit the Window function to apply the "cumsum"-like operation (provided in Pandas):
// Define a Window, partitioned by userId (partitionBy), ordered by timestamp (orderBy), and delivers all rows before current row in this partition as frame (rowsBetween)
val windowSpec = Window.partitionBy("userId").orderBy("timestamp").rowsBetween(Long.MinValue, 0)
val sessionDf = dfTSDiff.
withColumn("ts_diff_flag", genTSFlag(TIME_OUT)(col("time_diff"))).
select(col("userId"), col("eventSeq"), col("timestamp"), sum("ts_diff_flag").over(windowSpec).alias("sessionInteger")).
withColumn("sessionId", genSessionId(col("userId"), col("sessionInteger")))
Previously:
Then split by ";" and get each session, create a sessionId; afterwards split by "," and explodes to final result. Thus sessionId is created with the help of string operations.
(This part should be replaced by cumulative sum operation instead, however I did not find a good solution)
Any idea or thought about this question is welcomed.
GroupConcat could be found here: SPARK SQL replacement for mysql GROUP_CONCAT aggregate function
Reference: databricks introduction
dt.withColumn('sessionId', expression for the new column sessionId)
for example:
dt.timestamp + pre-defined value TIMEOUT
I'm wondering if its possible to set the operator when using a HasMany relationship in Laravel 4.2.
I'm working with a users table and an email log table. The log table has a userID stored in serialised format (as there may be more than one userID stored within the log).
Users table
+---------+
| user_ID |
+---------+
| 1 |
| 2 |
+---------+
emailLog Table
+----+--------------------+
| ID | user_ID |
+----+--------------------+
| 1 | a:1:{i:0;s:1:"2";} |
| 2 | a:1:{i:0;s:1:"1";} |
+----+--------------------+
Am I able to use a hasMany relation with a 'Like' operator rather than an equals operator to return the correct email log ID? Would the statement be written something like the below?
return $this->hasMany('emailLog', 'user_ID', '%user_ID%', 'LIKE');
The proper way to return join table with a where clause would be:
return $this->hasMany('emailLog','user_id')->where('user_id', 'LIKE', '%user_id%');
I found a way to do something similar
class User extends Model
{
public function emailLogs()
{
// old code
// return $this->hasMany(EmailLogs::class, 'user_ID');
// Replace HasMany by like query
return EmailLogs::where('user_ID', 'LIKE', "%{$this->user_ID}%");
}
}
I'd like to compare a Cucumber::Ast::Table of (at least) expected values with an array containing my actual values, but I want to ignore additional rows. For this, I thought I could use the diff! method with :surplus_row => false.
This works as expected (Cucumber reports success):
table (Cucumber::Ast::Table):
|id|name |
|1 |one |
|2 |two |
#actual:
|id|name |
|1 |one |
|2 |two |
|3 |three|
But this does not (I get Cucumber::Ast::Table::Different ):
table (Cucumber::Ast::Table):
|id|name |
|2 |two |
#actual:
|id|name |
|1 |one |
|2 |two |
|3 |three|
So it seems Cucumber is reporting a false positive if my #actual contains only "inner" rows. Is this a bug in Cucumber? Or am I making some stupid mistake?
CODE
table.feature:
Feature: Comparing Cucumber tables
Scenario: Comparing with a table with additional rows
Given I have a table with three rows and two columns
When I compare this table with an additional row and the same columns
Then I should get at least these rows
|id|name |
|2 |two |
table_steps.rb:
Given /^I have a table with three rows and two columns$/ do
#actual = [ { "id" => "1", "name" => "one" },
{ "id" => "2", "name" => "two"},
{ "id" => "3", "name" => "three" } ]
end
When /^I compare this table with an additional row and the same columns$/ do
# no-op
end
Then /^I should get at least these rows$/ do |table|
table.diff!(#actual, { :surplus_row => false } )
end
Cucumber version 1.2.1
Turns out it was a bug in the old cucumber version I was using. Upgrading from 1.2.1 to 1.3.10 fixed the issue.
See Cucumber Github Issue for details regarding the bug.
where can i find tutorial where the concept is uploading image and it save the image path in database with the user id and with delete function. thanks!
here are the concept form
+--------+ +------------+
| avatar | | | Browse
+--------+ +------------+
+----------+
| | name
+----------+
+----------+
| | age
+----------+
+----------+
| | address
+----------+
+----------+
| Submit |
+----------+
Here is one that does the upload, the database and delete should be easy from there
http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-file-uploading-and-image-manipulation/
Also review the codeigniter pages for this subject
Also of note
Codeigniter: Image Upload
EDIT----------------------------------------
I am assuming you are listing the images in a table to edit or delete them. I could be way off base with what you are doing. I am also assuming while you use the "user id" you also assign an id to the image itself.
For deletions I do something like this
$this->table->set_heading('Date', 'Title', 'Delete', 'Update');
foreach($records as $row){
$row->title = ucwords($row->title);
$this->table->add_row(
$row->date,
anchor("main/blog_view/$row->id", $row->title),
anchor("main/delete/$row->id", $row->id, array('onClick' => "return confirm('Are you sure you want to delete?')")),
anchor("main/fill_form/$row->id", $row->id)
);
}
$table = $this->table->generate();
See the second anchor? It picks up the ID of the image in the db and points to a controller function:
function delete()
{
$this->is_logged_in();
$this->post_model->delete_row();
$this->maint();
}
the Model
function delete_row()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('posts');
}