Parsing data from 1 method to another method in codeigniter - php

On my controller i have this method.
public function abc() {
$data = array(
'firstname' => 'abc',
'username' => 'abc#gmail.com',
'password' => random_string('alnum', 6),
'email' => 'abc#gmail.com'
);
echo $data['firstname'];
}
i want to pass that array data ($data) on another function
public function abcx() {
$this->abc();
//print the variable data from abc method
}
How to do this ?

First Function :
public function abc() {
$data = array(
'firstname' => 'abc',
'username' => 'abc#gmail.com',
'password' => random_string('alnum', 6),
'email' => 'abc#gmail.com'
);
return $data['firstname'];
}
Another function
public function abcx() {
$data = $this->abc();
//print the variable data from abc method
echo $data;
}

Related

Undefined array key "id" in CodeIgniter 4

Undefined array key "id" in CodeIgniter 4
enter image description here
public function update($id)
{
$slug = url_title($this->request->getVar('nama_rak'), '-', true);
$this->rakModel->save([
'id' => $id,
'nama_rak' => $this->request->getVar('nama_rak'),
'slug' => $slug,
'detail_rak' => $this->request->getVar('detail_rak'),
'created_at' => $this->request->getVar('created_at'),
'updated_at' => $this->request->getVar('updated_at')
]);
return redirect()->to('/master/master_rak')->with('pesan', 'Data Berhasil diubah!');
}
this is the construct
class Master extends BaseController
{
protected $rakModel;
public function __construct()
{
$this->rakModel = new RakModel();
}
// Pengadaan Buku
public function master_rak()
{
$data = [
'pages' => 'Master Data',
'title' => 'Data Rak',
'rak' => $this->rakModel->getRak()
];
echo view("master/master_rak", $data);
}

Array to string conversion using laravel 6

i added some fields to users table of laravel auth, and when i want to create a new user it gives me error Call to a member function hasFile () on array.
RegisterController.php
protected function create(array $data)
{
$jdate = Carbon::now();
//image
if($data->hasFile('image'))
{
$image = $data->file('image');
$imagee = Crypt::encryptString($image);
$image->storeAs("public\annonces\\".$jdate->format('F').$jdate->year,$imagee.'.'.$image->extension());
$data->image = "annonces\\".$jdate->format('F').$jdate->year."\\".$imagee.'.'.$image->extension();
}
//images
$dataim = array();
if($data->hasFile('images'))
{
foreach($data->file('images') as $file)
{
$namee = "public\\annonces\\".$jdate->format('F').$jdate->year."\\".time().'.'.$file->extension();
$name = Crypt::encryptString($namee).'.'.$file->extension();
$file->storeAs("public\\annonces\\".$jdate->format('F').$jdate->year, $name);
array_push($dataim,$name);
}
}
$data->images=json_encode($dataim);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'telephone' => $data['telephone'],
'ville' => $data['ville'],
'ighrem' => $data['ighrem'],
'autrei' => $data['autrei'] ?? null,
'hay' => $data['hay'],
'autreh' => $data['autreh'] ?? null,
'adressem' => $data['adressem'],
'adresser' => $data['adresser'],
'image' => $data['image'] ?? null,
'images' => $data['images'] ?? null
]);
}
May you use as below
protected function create(Request $data){}

An uncaught Exception was encountered, Call to undefined method

I'm having this error and been trying to figure whats wrong for like 3 days straight with no luck:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined method Nsbh::post()
Filename: application\controllers\nsbh.php
Line Number: 25
nsbh.php
<?php
Class Nsbh extends CI_Controller{
var $API ="";
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->library('session');
$this->load->library('curl');
$this->load->helper('form');
$this->load->helper('url');
}
function index(){
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh'));
$this->load->view('nsbh/list',$data);
}
function create(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
$insert = $this->curl->simple_post($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($insert)
{
$this->session->set_flashdata('hasil','Insert Succeed');
}else
{
$this->session->set_flashdata('hasil','Insert Failed');
}
redirect('nsbh');
}else{
$this->load->view('nsbh/create');
}
}
function edit(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
$update = $this->curl->simple_put($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($update)
{
$this->session->set_flashdata('hasil','Update Succeed');
}else
{
$this->session->set_flashdata('hasil','Update Failed');
}
redirect('nsbh');
}else{
$params = array('id'=> $this->uri->segment(3));
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh',$params));
$this->load->view('nsbh/edit',$data);
}
}
function delete($id){
if(empty($id)){
redirect('nsbh');
}else{
$delete = $this->curl->simple_delete($this->API.'/nsbh', array('id'=>$id), array(CURLOPT_BUFFERSIZE => 10));
if($delete)
{
$this->session->set_flashdata('hasil','Deleted');
}else
{
$this->session->set_flashdata('hasil','Failed');
}
redirect('nsbh');
}
}
}
And this is what's on line 25
'id'=> $this->post('id'),
You are missing input in 'id' => $this->post('id')
So, the right code is as follow:
'id' => $this->input->post('id')
You have to use input on all other places also.
change from
$data = array( 'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
to
$data = array('id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
As you already did in edit function
In the constructor add $this->load->library('form_validation');
So your constructor looks like this:
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('session');
$this->load->library('curl');
$this->load->library('form_validation');
}

last inserted id in Php codeIgniter

In my model I have return below function for get record id
function getLastInserted()
{
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration");
return $query->result();
}
Now I want to pass that ID to mycontroller for record insertion
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
Now I want to access model function in controller and pass record id in data function How I do ??
set return in model and in controller write
$insert_id=$this->db->insert_id();
In Controller load your model first using
$this->load->model('model_name');
Then call to model's getLastInserted() function.
$ID = $this->model_name->getLastInserted();//you will get id here
In model return $query->row()->userid; insead return of $query->result().
Then modify the controller:
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $this->model_name->getLastInserted(),
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
hi i worked out the solution on last inserted id dear here the code:
controller:
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
model:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
model:
public function newregistration($sid)
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $sid,
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}

Error Fatal error: Call to a member function insert() on a non-object

I'm using a callback after inserting in the database, to pass values to this other table, i think the method i used its fine. But it gives me this error when its inserting to the other table in the database, probably in my model, or something in the controller im kind of clueless in this error, can someone please help me.
Before someone say its to put $this->load->database(); its already in my autoload in config file :)
Fatal error: Call to a member function insert() on a non-object
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Finances::$inventory_model
Filename: controllers/finances.php
Line Number: 125
CONTROLLER:
public function inventory_management($post_array,$primary_key)
{
$this->load->model('inventory_model');
if($post_array['id_expense']!=null){
$type = 'add';
$id_exp_detail = $primary_key;
$result = $this->inventory_model->insert([
'id_exp_detail' => $id_exp_detail,
'name' => $post_array['brand'],
'quantity' => $post_array['item_quantity'],
'lote' => $post_array['package_code'];
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}else if ($post_array['name']!=null){
$type = 'sub';
$id_treatment = $primary_key;
$result = $this->inventory_model->insert([
'id_treatment' => $id_treatment,
'name' => $post_array['name'],
'quantity' => $post_data['recomended_dose'],
'lote' => $post_array['id_package'],
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}else{
$type = 'sub';
$id_fertilization = $primary_key;
$result = $this->inventory_model->insert([
'id_fertilization' => $id_fertilization,
'name' => $post_data['name'],
'quantity' => $post_data['quantity'],
'lote' => $post_data['id_package'],
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}
if($result){
echo "validation Complete";
}
}
MODEL
class CRUD_model extends CI_Model
{
protected $_table = null;
protected $_primary_key = null;
// ------------------------------------------------------------------------
public function __construct()
{
parent::__construct();
}
public function insert($data)
{
$this->db->insert($this->_table, $data);
// return $this->db->insert_id();
}
MODEL:
class inventory_model extends CRUD_model
{
protected $_table = 'inventory_management';
protected $_primary_key = 'id';
// ------------------------------------------------------------------------
public function __construct()
{
parent::__construct();
}
// ------------------------------------------------------------------------
}
here is the wrong you call wrong model file
$this->load->model('inventory_model');
it should be
$this->load->model('crud_model');
and you use wrong way to pass array in function
$param = array('id_exp_detail' => $id_exp_detail, 'name' => $post_array['brand'], 'quantity' => $post_array['item_quantity'], 'lote' => $post_array['package_code'], 'type' => $type, 'date' => date(), 'id_user' => $this->session->userdata('id_user'), 'id_entity' => $this->session->userdata('id_entity'));
$result = $this->inventory_model->insert($param);

Categories