I have two input field a and b. Create table with column a, b and sum. I want to input value of a and b from form and store the sum of a and b in sum column.
public function store(Request $request){
$number_one = $request->a;
$number_two = $request->b;
$sum = $number_one + $number_two;
DB::table('yourtable')->insert(
['one' => $number_one, 'two' => $number_two,'sum' => $sum]
);
//OR
$table = new table; //modelname
$table->one = $number_one;
$table->two = $number_two;
$table->sum = $sum;
$table->save();
if(!$table)
return 0;
else return 1;
}
You can do this way to:
public function store(Request $request){
$request["sum"] = $request->a + $request->b;
SumTable::store($request->all());
}
Related
I am using cakePHP 4 and I want to paginate my search query. I have users index page and when I search something whose resultant data exceed the defined limit of showing data. Whenever I search something I want the search string to retain in the input box even if I jump to the next page
public function index(){
$this->conditions = array();
if($this->session->check('conditions')){
$this->conditions = $this->session->read('conditions');
}
if ($this->request->is('post')) {
$data = $this->request->getData();
if(!empty($data['search_string'])){
$searchString = trim($data['search_string']);
if($data['search_by'] == 1){
$conditions = array('Users.id LIKE '=> $searchString);
}elseif($data['search_by'] == 2){
$conditions = array('Users.username LIKE '=>'%'.$searchString.'%');
}elseif($data['search_by'] == 3){
$conditions = array('Groups.name LIKE ' => '%'.$searchString.'%');
}
}
$this->conditions = array ($conditions);
$this->session->write('conditions',$this->conditions);
$this->paginate['conditions'] = $this->conditions;
$this->paginate['contain'] = ['Userdetails','Groups'];
$this->paginate['limit'] = 8;
$users = $this->paginate($this->Users);
$this->set('users',$users);
}else{
$this->session->write('conditions',$this->conditions);
$this->paginate['conditions'] = $this->conditions;
$this->paginate['contain'] = ['Userdetails','Groups'];
$this->paginate['limit'] = 8;
$users = $this->paginate($this->Users);
$this->set('users',$users);
}
}
I need to store interest_amount which is computed using get_Attribute function however now I need to insert it into table.
I have used static::saving function to compute interest_amount I don't know how to call it using $data in controller
Loan.php
use Illuminate\Database\Eloquent\Model;
class Loan extends Model
{
protected $fillable = [
'amount',
'interest',
'status',
'duration',
'member_id',
'loan_type_id',
'interest_type_id',
'loan_payment_type_id'
];
protected $appends = 'interest_amount';
public function getInterestAmountAttribute()
{
return ($this->amount)/100 * $this->interest;
}
public function member()
{
return $this->belongsTo(Member::class,'member_id','id');
}
protected static function boot()
{
parent::boot();
static::saving(function($model) {
$model->interest_amount = ($model->amount/100 ) * $model->interest;
});
}
}
LoanController.php
$data['amount'] = $request->amount;
$data['interest'] = $request->interest;
$data['interest_amount'] = $interest_amount;
$data['duration'] = $request->duration;
$data['status']= $request->status;
$data['member_id'] = $request->name;
$data['loan_type_id']= $request->loan_type;
$data['interest_type_id']= $request->interest_type;
$data['loan_payment_type_id']= $request->payment;
if(DB::table('loans')->insert($data)){
return redirect()->route('loan')->with('success','data was successfuly inserted');
}
I initially didn't have interest_amount column but I added it later. I don't know how to call the saving function with the approach I have used in my controller.
Model events will not fire when you use the query builder to insert data into your tables.
You have to use the Model to save the data, something like this:
If your model attributes are mass assignable:
if((new Loan($data))->save()){
return redirect()->route('loan')->with('success','data was successfuly inserted');
}
otherwise:
$loan = new Loan;
$loan->amount = $request->amount;
$loan->interest = $request->interest;
$loan->interest_amount = $interest_amount;
$loan->duration = $request->duration;
$loan->status = $request->status;
$loan->member_id = $request->name;
$loan->loan_type_id = $request->loan_type;
$loan->interest_type_id = $request->interest_type;
$loan->loan_payment_type_id = $request->payment;
if($loan->save()){
return redirect()->route('loan')->with('success','data was successfuly inserted');
}
i am submitting the location from textbox and putting values in comma seprated form like ngp,akola,kanpur.
Now i want if i have 3 values in location textbox then it insert 3 rows in location table one for ngp,second for akola and third for kanpur.
but it also takes last inserted id of user table and post all values in location table.
my location table have fields id,name,user_id.
in user_id column i need the last inserted id of user table and in name column i need to insert all values in table..
please help me to do this..below is my code.
function add() {
if (!empty($this->request->data)) {
$this->User->set($this->data['User']);
$isValidated = $this->User->validates();
if ($isValidated) {
// Generating UUID
if (!empty($this->request->data['User']['company_name'])) {
$this->request->data['User']['is_business'] = 1;
}
if ($this->User->save($this->request->data, array('validate' => false))) {
$lastId = $this->User->id;
$location = implode(',',$this->request->data['User']['location']);
for(i=1;i<=$location;i++){
$this->Location->save($location);
}
}
}
}
}
$lastId = $this->User->id;
$location = explode(',',$this->request->data['User']['location']);
foreach($location as $value){
$data = array();
$data["name"] = $value;
$data["user_id"] = $lastId;
$this->Location->save($data,false);
$this->Location->id = false;
}
Model::saveAll(array $data = null, array $options = array())
$this->request->data['Location'] = explode(',',$this->request->data['User']['location']);
if ($this->User->saveAll($this->request->data, array('validate' => false))){ // ...}
I'm trying to save data in CakePHP in this way:
get max entry_id from table translations
(SELECT MAX(entry_id) as res FROM translations) + 1
save data into translations table
$translationData = array('entry_id' => $entry_id, 'language_id' => $key, 'text' => $item);
$translation = new Translation();
$translation->set($translationData);
$translation->save();
again get max entry_id
save next set of data
again get max entry_id
save next set of data
Please have a look at my code:
$this->request->data['Product']['name_tr_id'] = $this->Translation->saveTranslations($this->request->data['Product']['name']);
$this->request->data['Product']['description_tr_id'] = $this->Translation->saveTranslations($this->request->data['Product']['description']);
$this->request->data['Product']['seo_title_tr_id'] = $this->Translation->saveTranslations($this->request->data['Product']['seo_title']);
$this->request->data['Product']['seo_keywords_tr_id'] = $this->Translation->saveTranslations($this->request->data['Product']['seo_keywords']);
$this->request->data['Product']['seo_desc_tr_id'] = $this->Translation->saveTranslations($this->request->data['Product']['seo_desc']);
saveTranslations method:
public function saveTranslations($data) {
$entry_id = $this->getNextFreeId();
foreach($data as $key => $item) {
if($item != "") {
$this->create();
$temp['Translation']['entry_id'] = $entry_id;
$temp['Translation']['language_id'] = $key;
$temp['Translation']['text'] = $item;
$this->save($temp);
}
}
return $entry_id;
}
getNextFreeId method:
public function getNextFreeId() {
$result = $this->query("SELECT MAX(entry_id) as res FROM translations");
return $result[0][0]['res'] + 1;
}
I don't know why entry_id have all the time the same value.
After many hours of searching for solution I finally managed to solve the problem in very easy way - just add false parameter in query method:
$this->query("INSERT INTO translations(entry_id, language_id, text) VALUES($entry_id, $key, '$item')", false);
and
$result = $this->query("SELECT SQL_CACHE MAX(entry_id) as res FROM translations", false);
I need perform query "LOCK TABLE myTable WRITE"
<?php
$db = Database::instance();
$query = 'LOCK TABLE `myTable` WRITE';
$db->query(Database::WHAT_IS_THE_TYPE_SHOULD_SPECIFY_HERE_?, $query);
In framework Kohana 3.3
file ~/modules/database/classes/Kohana/Database.php
implements folowing types:
const SELECT = 1;
const INSERT = 2;
const UPDATE = 3;
const DELETE = 4;
but none of them fit in my case.
Any idea. Thanks.
Google works wonders. http://forum.kohanaframework.org/discussion/6401/lockunlock/p1
You can pass NULL as the first argument:
$db = Database::instance();
$query = 'LOCK TABLE `myTable` WRITE';
$db->query(NULL, $query);
From Kohana: http://kohanaframework.org/3.3/guide-api/Database_MySQL#query
if ($type === Database::SELECT)
{
// Return an iterator of results
return new Database_MySQL_Result($result, $sql, $as_object, $params);
}
elseif ($type === Database::INSERT)
{
// Return a list of insert id and rows created
return array(
mysql_insert_id($this->_connection),
mysql_affected_rows($this->_connection),
);
}
else
{
// Return the number of rows affected
return mysql_affected_rows($this->_connection);
}
As you can see that's why you're able to pass NULL.