serverfireteam/panel validation:how to check for unique value in column - php

I'm using Laravel with serverfireteam/panel and I need that the validation checks for a unique value in column.
I tested this:
$this->edit->add('nr','Nummer','text')->rule('required')->rule('unique:nr');
result Query:
SQL: select count(*) as aggregate from `nr` where `nr` = 22
and this:
$this->edit->add('nr', 'Nummer','text')->rule('required')->rule('unique:.kunden nr');
which results in:
(SQL: select count(*) as aggregate from `kunden nr` where `nr` = 22)
Is it possible to check for uniqueness with rules in serverfireteam/panel and how does it work?

I found the solution for the problem. It's similar Syntax used like in laravel's default Syntax https://laravel.com/docs/5.1/validation.
But here you have always to add the tablename:
->rule('unique:tablename,columnname')
for example:
$this->edit->add('nr', 'Nummer','text')->rule('required')->rule('unique:kunden,nr');

Related

fetching field in laravel query

I have two tables user_master and user_teams with common field user_name.
I want to join the table and get the team value group by teams
tried as
$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->groupBy('user_teams.team')
->pluck('user_teams.team')
->toArray();
by may values are duplicating.I'm using postgresql
since you didn't determine select fields ... the default will be '*'
this is why you are getting duplicated fields ...
just add :
->select('user_teams.team')
and i think that all.
i recommend not using group without aggregation ....
so my advice your query to like this:
$filter_teams = DB::table('user_teams')
->join('user_master','user_master.user_name','=','user_teams.user_name')
->whereIn('user_master.geo',$geo)
->select('user_teams.team')->distinct()
->pluck('user_teams.team')
->toArray();

selecting MAX(val) from a sql resource in php

I have a sql result that looks like this:
MAX(val) | instance
17410742.00 | 0
I need to loop through it in php but I can't seem to select the Max(val) column. Usually I would do a something like this:
foreach ($sqlmax as $maxrow){
$myvar=$maxrow['instance'];
}
Which returns the 'instance' value, but I can't get the syntax to retrieving the Max(val) as
foreach ($sqlmax as $maxrow){
$myvar=$maxrow['Max(val)'];
}
doesn't work. I get the error Notice: Undefined index:
How do I select the Max(val) result in the same way I can select the 'instance' value? Thanks
Use AS in your SQL Query:
SELECT MAX(val) AS max_val, instance FROM table WHERE instance = 0;
Then you will be able to access the column as $myvar=$maxrow['max_val'];.
The query I wrote here is just an example, since you do not provide yours.
I will go ahead an assume you use mysql and will provide a link to MySQL docs:
A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses. For example:
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable ORDER BY full_name;
Assign a column name in your query, so you can access the field easier:
SELECT MAX(val) AS max_value ....
Then you can access it with
$myvar=$maxrow['max_value']
Simply do it like
select max(val) as mval
and access it like
$maxrow['mval']
Just providing an alternate answer, in case you don't want (or are unable to) use AS.
The problem here was that the key of the array is case sensitive, so you have to use $maxrow['MAX(val)'] instead of $maxrow['Max(val)'].
SELECT MAX(val) AS instance
Access this
foreach ($sqlmax as $maxrow){
$myvar=$maxrow['instance'];
}

Laravel groupBy Database query Builder not working

I have this problem in a query using laravel groupBy, it simply return a groupBy error. I have read the documentation about this but can't really figure it out. I also read the same problem pointing that it is because of postgreSQL that I need to include all the columns in grouBy clause. I tried it but still it doesn't return the distinct values. Please help me with this. Below is my code. Thanks a lot.
Controller function
public function index(){
$purchases = Purchase::groupBy('purchase_order_id')->get();
return view('purchases/purchases_crud', ['allPurchases' => $purchases]);
}
Table to query
Error
QueryException in Connection.php line 680:
SQLSTATE[42803]: Grouping error: 7 ERROR: column "purchases.id" must appear
in the GROUP BY clause or be used in an aggregate function
LINE 1: select * from "purchases" group by "purchase_order_id"
^ (SQL: select * from "purchases" group by "purchase_order_id")
There you have it add group by "purchases.id" or restrict the select to only the operates that are needed.
->select("purchase_order_id","purchases.id")
->groupBy("purchases.id") // add if possible
Agreggates for your case should mean something like ->select("sum(po_total)")
If we group by id, we get all results as id is unique, my mistake. You want something like this
DB::table("purchases")->select("purchase_order_id", DB:raw("sum(po_total)"))->groupBy("purchase_order_id")->g‌​et();
Rule of thumb is you either select a field with Sum() or have it on the group by

codeigniter select as

how doHow do we do
select table.value as table_value from table in codeigniter?
The AS part doesnt work because when i try to access the value,
this doesnt work:
$qry_inp = 'select table.value as table_value from table '
$query = $this->db->query($qry_inp);
echo $query->row('table_value ');// this will be empty, but it shouldn`t be
doesn`t matter if its in AR or simple query
Pretty simple thing.
$this->db->select('COLUMN_ACTUAL_NAME as `COLUMN_NAME_YOU_WANT_TO_SHOW`');
i'm joining two tables in which column names are same so i separate both tables columns by using as keyword , this is how you can use AS in codeigniter
$this->db->select("departments.name AS 'dname'");
$this->db->select('positions.name');
Where is that behaviour documented? row doesn't take a column name as a parameter; it optionally takes a row number, and that's it. Access it like any other field:
echo $query->row()->table_value;

Sphinx RT Index and SphinxQL Query

We are deploying the RT index in our architecture. But we need some clarification and some difficulties faced during the deployment.
Schema defined in Index:
index logtable
{
type = rt
path = /usr/local/sphinx20/var/data/logtable
rt_attr_string = TransactionId
rt_attr_uint = CustomerId
rt_attr_timestamp = DateOfTransaction
rt_attr_string = CustomerFeedback
rt_field = TransactionType
}
Faced Problem
Question 1:
How we can get the count() query result in SPHINXQL. Because its important for us, based on customer count we have to take it to show in our application.
Example below,
Query - select count(*) from logtable where CustomerId='871';
In SphinxQL - We didnt get this result and getting the following error.ERROR 1064 (42000): index logtable: invalid schema: Count(*) or #count is queried, but not available in the schema.
Question 2:
i declared as a STRING attribute in conf for the field of "TransactionId", but i cant able to retrieve the records if that fields use in where condition.
Example below,
select * from logtable where TransactionId='TRA23454';
Following error i am getting,
ERROR 1064 (42000): sphinxql: syntax error, unexpected $undefined, expecting CONST_INT or CONST_FLOAT or '-' near '"TRA23454"'
Please help us to close these issues if knows.
Kumaran
select * from logtable where TransactionId='TRA23454';
Answer :
select * from logtable where MATCH('#TransactionId TRA23454')
In first example instead of count(*) you need to use 'show meta;' query after search query, it will contain total_count field.
select id from logtable where CustomerId='871';
show meta;
In the second example string attributes can't be used in WHERE, ORDER or GROUP clauses.
Actually you need to convert TransactionId into integer and use integer attribute. It is quite simple to do using crc32 mysql function.

Categories