Not retrieving set of values from table. Laravel - mysql

I'm trying to get values from my linking table sfees with columns student_id and mfee_id. Here, there might be multiple student_id with different mfee_id. The thing is that, i want to retrieve all mfee_id with same student_id.
I have used following syntax, but it is only returning single value:
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->value('mfee_id');//trying to get only mfee_id
return $sfees;
}
How can i solve this problem?
//edited
My table looks like:

You need to do a groupBy -
$sfees = sfee::where('student_id', '=',$sid)->groupBy('student_id')->get();
UPDATE
Try something like this -
$sfees = sfee::where('student_id', '=',$sid)->lists('mfee_id');
Or you can use the Schema Builder like this -
DB::table('sfees')->where('student_id', '=', $id)->lists('mfee_id');

Related

Concatenate 2 columns laravel

I am trying to concatenate 2 columns and researched how to do it but I don't know where the error is, it says column not found, this is my query
public function obtenerCargo() {
if ($this->rcargo_id == null) {
$this->listCargo = recepcionCargo::select(
'recepcion_cargo.id',
'recepcion_cargo.rcargo_id',
'recepcion_cargo.no_factura',
'cargo_id.cargo_id',
'recepcion_cargo.porcentaje',
'recepcion_cargo.cargo_minimo',
'recepcion_cargo.cargo_devolucion',
'recepcion_cargo.cargo_reparacion',
'recepcion_cargo.cargo_almacenaje',
'recepcion_cargo.cargo_visita',
'recepcion_cargo.cargo_traslados',
'recepcion_cargo.cargo_fletes',
'recepcion_cargo.total',
'recepcion_cargo.cargo_porcentaje',
'recepcion_cargo.total_sp',
DB::raw("CONCAT(recepcion_cargo.total,' ',recepcion_cargo.total_sp) AS TOTAL",'recepcion_cargo.id'))
->pluck('TOTAL', 'recepcion_cargo.id')
->join('cargo_id', 'cargo_id.id', '=', 'recepcion_cargo.car_id')->get();
} else {
$this->listCargo = recepcionCargo::select(
'recepcion_cargo.id',
'recepcion_cargo.rcargo_id',
'recepcion_cargo.no_factura',
'cargo_id.cargo_id',
'recepcion_cargo.porcentaje',
'recepcion_cargo.cargo_minimo',
'recepcion_cargo.cargo_devolucion',
'recepcion_cargo.cargo_reparacion',
'recepcion_cargo.cargo_almacenaje',
'recepcion_cargo.cargo_visita',
'recepcion_cargo.cargo_traslados',
'recepcion_cargo.cargo_fletes',
'recepcion_cargo.total',
'recepcion_cargo.cargo_porcentaje',
'recepcion_cargo.total_sp',
DB::raw("CONCAT(recepcion_cargo.total,' ',recepcion_cargo.total_sp) AS TOTAL",'recepcion_cargo.id')
)
->pluck('TOTAL', 'recepcion_cargo.rcargo_id')
->join('cargo_id', 'cargo_id.id', '=', 'recepcion_cargo.car_id')
->where('recepcion_cargo.rcargo_id', '=', $this->rcargo_id)->get();
}
}
I am using my model and db:raw with pluck according to this it should work but it does not, in my view a table is displayed and it is not convenient to have 2 total fields so it is better for me to use concat
What is happening in your queries is:
You are selecting a lot of columns from a table, including one that belongs to another table and a DB::raw statement
You are performing the query and extracting the fields TOTAL and recepcion_cargo.id (that is what pluck does, it first performs the query, then extracts the results, so at this point your query will fall)
You are joining a Illuminate\Support\Collection to another table
I would recommend a couple actions:
First, you should join before executing the query (that is, before calling pluck
Second, to improve the performance, only select the columns that you are going to use
Hope it helps. Good luck!

Symfony Doctrine: Search in simple_array

I got values stored in my database column field as value1,value2,value3,value4, so a simple_array column.
So i'm using Doctrine to make a search using this:
$searchQuery = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->andWhere("p.vals <= :value2")
->setParameter('value2', $request->query->get('value2'));
->orderBy("p.creationtime", 'DESC');
So expecting value2 is in the 2nd position of a simple array like value1,value2,value3, how can i ask QueryBuilder to select the second value in the string?
I think this query try to get all the values in p.vals, results are not right, shound select just one.
How can I select eg. the 2nd value in p.vals?
I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function
public function getItemFromArray(array $array, $index)
{
return isset($array[$index]) ? $array[$index] : null;
}
And if you want to find item with condition use
array_filter()

Laravel Sum column database Eloquent

Trying to get the sum of a int field in one of my table should be pretty easy, unfortunately it is not as I'm getting different result whether I use Laravel, MySQL or Excel.
Laravel 5.4 gives me 20506:
Table::sum('field_name');
MySQL gives me 1830:
Select sum(field_name) from table;
And the data from the Excel sheet before importing it into the database:
Sum gives me 145689
Any idea? I tried to cast to integer before doing the sum but it doesn't work.
All the numbers are pretty big but don't contain comma or dot.
Examples of values I have to sum: (contain sometimes empty cells)
17906774
99630157
28581131
159551532
20312892
668928885
$query = YourModel::query();
$query->withCount([
'activity AS yoursum' => function ($query) {
$query->select(DB::raw("SUM(amount_total) as paidsum"))->where('status', 'paid');
}
]);
You can use laravel aggregate function SUM as :
$result = DB::table('table_name')
->select(DB::raw('SUM(field_name) as total_field_name'))
->get();
For more details you can follow:
https://laravel.com/docs/5.4/queries
Thanks
Try with
$result = DB::table(tablename)
->selectRaw('sum(column)')
->get();
If it still gives you wrong result, maybe wait for someone to give you a better answer. This is all I could think of.

filling a select field with model with certain tag - laravel

I have no problem with selecting a model tagged with certain tag
$lis = Capacitytype::find(124)->entities()->orderBy('name','asc')->get();
In my Capacitytype model I have this relation:
public function entities()
{
return $this->belongsToMany('App\Models\Entity', 'entity_capacitytypes', 'capacitytype_id', 'entity_id');
}
MY PROBLEM:
I wish to use the same selection f models to fill a select field in a form. (I use select2.js)
When I try to implement this code
$lis = array(null => 'Commitee') + Capacitytype::find(124)->entities()->orderBy('name','asc')->lists('name', 'id')->all();
I get this error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select name, id from entities inner join entity_capacitytypes on entities.id = entity_capacitytypes.entity_id where entities.deleted_at is null and entity_capacitytypes.capacitytype_id = 6 order by name asc)
I tried to solve this issue by adding table name to my code, but this is as far as I was able to go:
->lists('entities.name', 'entities.id')
My Question:
how modify the code to get the desired collection for my select field?
Thank you.
You could make use of Laravel's with() to perform an eager loading as a workaround for this issue. To do so, just pass the name of the relation (i.e. entities) to the method:
Capacitytype::find(124)->with('entities')->orderBy('name','asc')->lists('entities.name', 'entities.id');
Or else, you could first fetch the records, and then reformat it to match your usecase:
$list = Capacitytype::find(124)->entities()->orderBy('name','asc')->get()->toArray();
As such, you can refine the returned list of arrays, to match your id => name format (hence, a substitute for the lists method).
$refinedList = array();
foreach($list as $entity){
$refinedList[$entity['id']] = $entity['name'];
}
And, there you go.

zend framework automatically alter queries

My database (mysql) tables use TIMESTAMP columns, and whenever I want them returned in a query, I want them to be queried as "UNIX_TIMESTAMP(columnname)".
How do you easily modify queries in zend framework to achieve this?
For example, the current code is:
select = $this->select();
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
This eventually becomes:
select * from tablename where user_id = 42;
I want something that automatically finds the TIMESTAMP column and changes the resulting query to:
select user_id,name,unix_timestamp(created) where user_id = 42;
I know I can use a MySQL view to achieve this, but I'd rather avoid that.
Thanks.
RR
You should be able to specify the fields you want in the select using the $select->from() object.
Zend_Db_Select
You should end up with something like this.
$select = $this->select();
$select->from(
array('t' => 'tablename'),
array('user_id', 'name', 'UNIX_TIMESTAMP(created)')
);
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
If you wanted to run an expression that doesn't have parenthese in the function, Use the Zend_Db_Expr() method to escape the query properly.