Model & Controller error on my website project - php

Please help me, I cannot access my code it always shows the error:
An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Quotation_model::addModels(), 1 passed in C:\xampp\htdocs\sangil\application\controllers\Quotation.php on line 126 and exactly 2 expected
Filename: C:\xampp\htdocs\sangil\application\models\Quotation_model.php
Line Number: 122
Backtrace:
File: C:\xampp\htdocs\sangil\application\controllers\Quotation.php
Line: 126
Function: addModels
File: C:\xampp\htdocs\sangil\index.php
Line: 315
Function: require_once
This is the code:
Controller: Quotation.php
public function addQuotation()
{
$this->form_validation->set_rules('date', 'Date', 'trim|required');
$this->form_validation->set_rules('reference_no', 'Reference No', 'trim|required');
if ($this->form_validation->run() == false) {
$this->add();
} else {
$warehouse_id = $this->input->post('warehouse', true);
$data = array(
"date" => $this->input->post('date'),
"reference_no" => $this->input->post('reference_no'),
"warehouse_id" => $this->input->post('warehouse'),
"customer_id" => $this->input->post('customer'),
"biller_id" => $this->input->post('biller'),
"total" => $this->input->post('grand_total'),
"discount_value" => $this->input->post('total_discount'),
"tax_value" => $this->input->post('total_tax'),
"note" => $this->input->post('note'),
"shipping_city_id" => $this->input->post('city'),
"shipping_state_id" => $this->input->post('state'),
"shipping_country_id" => $this->input->post('country'),
"shipping_address" => $this->input->post('address'),
"shipping_charge" => $this->input->post('shipping_charge'),
"internal_note" => $this->input->post('internal_note'),
"mode_of_transport" => $this->input->post('mode_of_transport'),
"transporter_name" => $this->input->post('transporter_name'),
"transporter_code" => $this->input->post('transporter_code'),
"vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
"user" => $this->session->userdata('user_id')
);
if ($quotation_id = $this->quotation_model->addModel($data)) {
$log_data = array(
'user_id' => $this->session->userdata('user_id'),
'table_id' => $quotation_id,
'message' => 'Quotation Inserted'
);
$this->log_model->insert_log($log_data);
$sales_item_data = $this->input->post('table_data', true);
$js_data = json_decode($sales_item_data);
foreach ($js_data as $key => $value) {
if ($value == null) {
} else {
$product_id = $value->product_id;
$quantity = $value->quantity;
$data = array(
"product_id" => $value->product_id,
"quantity" => $value->quantity,
"price" => $value->price,
"gross_total" => $value->total,
"discount_id" => $value->discount_id,
"discount_value" => $value->discount_value,
"discount" => $value->discount,
"tax_id" => $value->tax_id,
"tax_value" => $value->tax_value,
"tax" => $value->tax,
"quotation_id" => $quotation_id
);
//$this->sales_model->checkProductInWarehouse($product_id,$quantity,$warehouse_id);
if ($this->quotation_model->addQuotationItem($data, $product_id, $warehouse_id, $quantity)) {
} else {
}
}
}
redirect('quotation/view/' . $quotation_id);
} else {
redirect('quotation', 'refresh');
}
}
}
Model: Quotation_model.php
public function addModel($data, $invoice)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
} else {
return FALSE;
}
}

you create model with 2 input
$data and $invoice
public function addModel($data, $invoice)
but only give 1 input in the controller when you call the model
$this->quotation_model->addModel($data)
delete the $invoice in your model if you dont need it, or add one more input when you call it from the controler.

public function addModel($data)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
}
return false;
}

Controller: Quotation.php
$quotation_id = $this->quotation_model->addModel($data);
so in your Model: Quotation_model.php
public function addModel($data)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
} else {
return FALSE;
}
}

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);
}

Php parses function without create_function and without eval

I am trying to create a class to clean data for a brand before adding it to my database. As you can see I have added general filters (which can be used elsewhere). On the other hand, some fields will need a personalized cleaning. That's why I created 'function' in my array. My code is currently functional however the "create_function" function is deprecated and I would like to remove it but I cannot find an alternative without using "eval". Can you help me find a solution? Thank you.
<?php
class VehMarques
{
private static $fields_allowed = [
'_id' =>
[
'instanceof' => '\MongoDB\BSON\ObjectID',
],
'name' =>
[
'function' => 'if(!isset($name) && !isset($age)){return false;}',
],
'user' =>
[
'required',
'instanceof' => '\MongoDB\BSON\ObjectID',
],
'centre' =>
[
'required',
'instanceof' => '\MongoDB\BSON\ObjectID',
],
'time' =>
[
'instanceof' => 'MongoDB\BSON\UTCDateTime',
],
];
public static function add(array $fields)
{
$fields_options=array();
foreach(self::$fields_allowed as $key => $val)
{
foreach($val as $key1 => $val1)
{
if(in_array($val1, array('required')))
{
$fields_options[$val1][$key] = null;
}
else
{
$fields_options[$key1][$key] = $val1;
}
}
}
if(!empty(self::$fields_allowed) && !empty(array_diff_key($fields, self::$fields_allowed)))
{
return false;
}
if(!empty($fields_options['function']))
{
foreach($fields_options['function'] as $func)
{
$func = preg_replace('/\$([a-zA-Z0-9]+)/', '$fields[\'$1\']', $func);
if(create_function('$fields', $func)($fields) === false)
{
return false;
}
}
}
if(!empty($fields_options['required']) && !empty(array_diff_key($fields_options['required'], $fields)))
{
return false;
}
if(!empty($fields_options['instanceof']))
{
foreach($fields_options['instanceof'] as $key => $val)
{
if(!($fields[$key] instanceof $val))
{
return false;
}
}
}
if(!isset($fields['_id']))
{
$fields['_id'] = new \MongoDB\BSON\ObjectID();
}
if(!isset($fields['time']))
{
$fields['time'] = new MongoDB\BSON\UTCDateTime();
}
return true;
}
}
$insert_marque = array(
'_id' => new \MongoDB\BSON\ObjectID(),
'name' => 'Test',
'user' => new \MongoDB\BSON\ObjectID(),
'centre' => new \MongoDB\BSON\ObjectID(),
'time' => new MongoDB\BSON\UTCDateTime()
);
var_dump(VehMarques::add($insert_marque));
?>

Non static method 'load' should not be called statically

this is the function, the error is in line 25 in
'$data = Excel::load($path)->get();' saying that None-Static méthode 'load' should not be called statically:
function import(Request $request)
{
$this->validate($request, [
'select_file' => 'required|mimes:xls,xlsx'
]);
$path = $request->file('select_file')->getRealPath();
$data = Excel::load($path)->get();
if($data->count() > 0)
{
foreach($data->toArray() as $key => $value)
{
foreach($value as $row)
{
$insert_data[] = array(
'zi' => $row['zi'],
'siteId' => $row['siteId'],
'gsmId' => $row['gsmId'],
'topoCont' => $row['topoCont'],
'plannRedr' => $row['plannRedr'],
'Country' => $row['country'],
'dateReal' => $row['dateReal'],
'semReal' => $row['semReal'],
'statuts' => $row['country'],
);
}
}
if(!empty($insert_data))
{
DB::table('tbl_customer')->insert($insert_data);
}
}
return back()->with('success', 'Excel Data Imported successfully.');
}
}
add this in your controller :
use Maatwebsite\Excel\Facades\Excel;

laravel 5.1 foreach Trying to get property of non-object

i am having 3-4 for each in my code which all use Eloquent and in all of them i get the same error
Trying to get property of non-object
when i dd the first argument of foreach which is an eloquent it returns an array of json with all the data in database of that table so its not returning null though i know it may break or bring null while in a foreach loop but i dont know where place one of them with model and controller here so here is the loop it self
<?php foreach(\App\Menu::all()->where('slug','main')->first()->items as $top_menu): ?>
<li class="dropdown">
<a href="<?php echo e(URL($top_menu->link)); ?>" <?php if($top_menu->link_blank): ?> target="_blank" <?php endif; ?>>
<?php echo e($top_menu->title); ?>
</a>
</li>
<?php endforeach; ?>
and here is my controller
class MenuItemsController extends AdminController
{
var $object = 'menu_items';
var $route_name = 'menu_items';
var $object_title = 'item';
var $object_titles = 'items';
var $attachments_config = [
'main_image' => [],
'default_thumb_sizes' => [],
'require' => ['main_image']
];
var $dt_fields_db = [];
var $dt_fields_heading = ['شناسه','منو','عنوان','موقعیت','نمایش','ثبت','عملیات'];
var $dt_fields_full = [
'id' => ['menu_items.id'],
'menu_title' => ['menus.title'],
'title' => ['menu_items.title'],
'position' => ['menu_items.position'],
'display' => ['menu_items.display'],
'created_at' => ['menu_items.created_at'],
'actions' => ['actions']
];
var $messages = array();
public function __construct() {
parent::__construct();
view()->share(['attachments_config' => $this->attachments_config]);
}
function index() {
$data['extra_assets'] = array(
array('type' => 'css','path' => 'datatables/datatables.bootstrap.css'),
array('type' => 'js','path' => 'datatables/jquery.dataTables.min.js'),
array('type' => 'js','path' => 'datatables/datatables.bootstrap.js'),
array('type' => 'script','path' => 'pagescripts/shared/datatables_script.php'),
);
$data['dt_fields_heading'] = $this->dt_fields_heading;
$data['dt_fields'] = $this->dt_fields_full;
$data['heading'] = $this->object_titles;
return view('admin.shared.datatable',$data);
}
function datatable() {
$this->dt_filtered_actions = dt_actions_filter($this->route_name,['edit','delete']);
$objects = DB::table('menu_items')
->join('menus', 'menu_items.menu_id','=', 'menus.id')
->select([
'menu_items.id',
'menus.title as menu_title',
'menu_items.title',
'menu_items.display',
'menu_items.position',
'menu_items.created_at'
]);
return Datatables::of($objects)
->editColumn('display', function($model) {return label_status($model->display);})
->editColumn('created_at', function($model) {return ($date = jDate::forge($model->created_at)->format('%H:%M:%S - %y/%m/%d'))?$date:"";})
->addColumn('actions', function($model) {
return dt_actions($this->route,$model->id,$this->dt_filtered_actions);
})
->make(true);
}
function add() {
$data['extra_assets'] = array(
array('type' => 'js','path' => 'select2/select2.full.min.js'),
array('type' => 'css','path' => 'select2/select2.min.css'),
array('type' => 'css','path' => 'select2/select2-bootstrap.css'),
array('type' => 'helper','name' => 'select2'),
);
$data['menus'] = Menu::all()->lists('title','id');
$data['heading'] = 'add'.$this->object_title;
return view('admin.'.$this->route_name.'.add',$data);
}
function create(\Illuminate\Http\Request $request) {
$validator = Validator::make($request->all(), [
'menu_id' => 'required',
]);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
$input = $request->all();
$input['display'] = (isset($input['display']))?$input['display']:0;
$menu = Menu::find($input['menu_id']);
if($menu->items->count() == $menu->capacity) {
$this->messages[] = array('type' => 'danger', 'text' => 'menu is full');
Session::flash('messages', $this->messages);
return back();
}
MenuItem::create($input);
$this->messages[] = array('type' => 'success', 'text' => 'success.');
Session::flash('messages', $this->messages);
return redirect()->route('admin.'.$this->route_name.'.index');
}
function edit(MenuItem $MenuItem) {
$data['object'] = $MenuItem;
$data['extra_assets'] = array(
array('type' => 'script','path' => 'pagescripts/shared/image_preview_script.php'),
array('type' => 'js','path' => 'select2/select2.full.min.js'),
array('type' => 'css','path' => 'select2/select2.min.css'),
array('type' => 'css','path' => 'select2/select2-bootstrap.css'),
array('type' => 'helper','name' => 'select2'),
array('type' => 'js','path' => 'ckeditor/ckeditor.js'),
array('type' => 'js','path' => 'ckeditor/config.js'),
array('type' => 'js','path' => 'ckfinder/ckfinder.js'),
array('type' => 'js','path' => 'ckfinder/config.js'),
array('type' => 'script','path' => 'pagescripts/shared/ckeditor_script.php'),
);
$data['menus'] = Menu::all()->lists('title','id');
$data['heading'] = 'edit'.$this->object_title;
return view('admin.'.$this->route_name.'.edit',$data);
}
function update(MenuItem $MenuItem,\Illuminate\Http\Request $request) {
$validator = Validator::make($request->all(), [
'menu_id' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$input = $request->all();
$input['display'] = (isset($input['display']))?$input['display']:0;
$MenuItem->update($input);
$this->messages[] = array('type' => 'success', 'text' => 'success.');
Session::flash('messages', $this->messages);
return redirect()->back();
}
function delete(\Illuminate\Http\Request $requests,MenuItem $MenuItem) {
if($requests->ajax() && $MenuItem->delete()) {
return "OK";
} else {
return "ERROR";
}
}
function deleteall(\Illuminate\Http\Request $requests) {
$ids = $requests->get('ids');
if($requests->ajax()) {
foreach (explode(',',$ids) as $id) {
if(intval($id)) MenuItem::find($id)->delete();
}
return "OK";
} else {
return "ERROR";
}
}
}
the problem was because my database table didn't got any row by the slug of menu and the query returned null and that error happens that error generally happens when your query returns null thanks all for help

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');
}

Categories