my question is if is possible and how can i fill the description field on the payment form, whith a reference number from my slq database ?
Thank you.
You can try something like this query to define the description:
<?php $product = SELECT desc FROM table WHERE id = 'x'; ?>
And then use the $product variable to populate the value for "item_name" or whatever variable for the item description that matches the product you're using. I don't know what payment method you're using so I can't be more specific.
Related
I want to make a query that gets all the users from table "users" and i need to have only user.id.
$users = User::all();
this will get the whole model User but this is a real performance issue for my app. There is too much data going through.
I need to append some data for each user so i can calculate the working hours.
So the question is how to fetch all users without any other data except $user->id?
$name = DB::table('users')->select('id')->get();
For the certain columns, I think this is best:
$users = User::select('id')->get();
See Documentation.
Use the pluck() method:
$users = User::pluck('id');
The pluck method retrieves all of the values for a given key
Getting all User objects with id only:
$users = User::select('id')->get();
Getting all id as straight int value
According to documentation: https://laravel.com/docs/5.3/queries#selects
Specifying A Select Clause (most efficient)
$users = DB::table('users')->select('id')->get();
Retrieving A Single Column From A Row (but this will process all columns)
$name = DB::table('users')->where('name', 'John')->pluck('name');
I used webpatser/laravel-countries
in my controlleer
$countries= DB::table('countries')->lists('name');
to select country list in my form i got all the country
{!! Form::select('country',$countries,null) !!}
in my schema now i have like this
$table->string('country');
i can select the country but when i submit country column blank. any idea ?
Edit 2:
Your insert need to look like this:
DB::table('users')->insert(
['country' => 'Canada']
);
https://laravel.com/docs/5.2/queries#inserts
Edit: if you want to submit the string of the country name then you need the string to be the key in your array. Like this:
$countries= DB::table('countries')->lists('name', 'name');
That's not typical use of a database relationship though. I'd expect the user table to have a country_id integer field that matches the id field in countries. That's what my original answer (below) covers.
Original:
You'll want the id in your $countries array. I bet right now your ids are off by one, starting at 0 instead of 1 like in your DB.
$countries= DB::table('countries')->lists('name', 'id');
There are different ways to save relations in laravel, but I actually prefer to skip that when possible and save directly to the country_id field.
{!! Form::select('country_id',$countries,null) !!}
But that depends on how your relationship is setup. And how you're saving. Show all the code!
I found this in the guide, but have no idea how to implement the same
yii\db\Query::count(); returns the result of a COUNT query. Other
similar methods include sum($q), average($q), max($q), min($q), which
support the so-called aggregational data query. $q parameter is mandatory
for these methods and can be either the column name or expression.
Say for example I have a table name 'billing' with columns:
name amount
charge1 110.00
charge2 510.00
Total - 620.00
How I implement using
yii\db\Query::sum('amount');
I have also tried like
$command = Yii::$app->db->createCommand("SELECT sum(amount) FROM billing");
yii\db\Query::sum($command);
but page generates error.
Thanks.
The first part of code you tried appears to be attempting to use Query Builder. In this case, you must create an instance of a query, set the target table, and then compute the sum:
Via Query Builder
(http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html):
$query = (new \yii\db\Query())->from('billing');
$sum = $query->sum('amount');
echo $sum;
The second part of code you tried appears to be attempting to use Data Access Objects. In this case, you can write raw SQL to query the database, but must use queryOne(), queryAll(), queryColumn(), or queryScalar() to execute the query. queryScalar() is appropriate for an aggregate query such as this one.
Via Data Access Objects
(http://www.yiiframework.com/doc-2.0/guide-db-dao.html):
$command = Yii::$app->db->createCommand("SELECT sum(amount) FROM billing");
$sum = $command->queryScalar();
echo $sum;
Within a model the sum could also be fetched with:
$this->find()->where(...)->sum('column');
You can directly use yii query concept in Search Model
$this->find()->from('billing')->where(['column'=>value])->sum('amount');
OR
$this->find()->where(['column'=>value])->sum('amount');
Outside the Model (Billing is model name)
Billing::find()->where(['column'=>value])->sum('amount');
i hope your model name is Billing
inside Billing model use
$this->find()->sum('amount');
in other models
Billing::find()->sum('amount');
Iam having a form which contaings of name,id etc..in this form iam getting dropdown from the same table only some ids here iam selecting multiple values and that should be inserted into db but only one value is inserting into the table remaining is not inserting can any one help me regarding this.
Model:
$data=array(
'first_name'=>$this->input->post('first_name'),
'exam_id'=>$this->input->post('exam_id'),
);
$this->db->insert('table',$data);
This is my dropdown
$this->table = 'table';
$exam = $this->dropdown('exam_id','examr_id');
return $exam;
This is my view:
$exam['']='--Select --';
$exam_id="id='exam_id' ";
if($this->input->post('exam_id')) $selected=$this->input->post('exam_id');else $selected='';
echo form_multiselect('exam_id',$exam,$selected,$exam_id);?>
any one help me it will be more helpfull for me
When you select multiple values and check that selected value pass in your url using GET or POST method. And than split your requested data using implode function. and save into database.
I am trying to display all users that have a meta data of with a specific value.
So in my case, I have a custom user meta data field called isblogger, with the options yes or no. So I would like to list users that have the isblogger value as yes.
Currently here is the code I have that is dispaying all users
global $wpdb;
$authors = $wpdb->get_results("SELECT
ID, user_nicename FROM $wpdb->users");
foreach($authors as $author) { ?>
list name etc...
Any help on this would be fantastic and much appreciated.
You have to add Where condition isblogger='yes'