BadMethodCallException in Builder.php line 2405: in laravel - laravel

I am new to laravel and facing an issue,I am calling a controller via ajax call ,it is calling controller but when controller is calling model it is giving
"BadMethodCallException in Builder.php line 2405:" error.Following is my code
AJAX:
$.ajax({
url: "<?php echo url('master/add-car-to-db');?>",
method : "post",
data : completeData ,
success : function (data){
data=parse.JSON(data);
if(data.success == 1){
window.location="<?php echo url('master/add-car-view');?>";
}
},
error: function(data,status){
alert(JSON.stringify(data));
}
});
Controller:
public function postAddCarToDb(Request $request){
$result = Master::addNewCar($request);
echo $result;
die;
if ($result > 0) {
echo json_encode(Array('success' => 1));
} else {
echo json_encode(Array('success' => 0));
}
}
Model:
public static function addNewCar(Request $request){
return "success"
}
Route:
Route::controller('master', 'MasterController');
Thanks in advance

Reverse routing is deprecated in laravel 5 so on.
Route::controller('master', 'MasterController');
use
Route::post('master/add-car-to-db','MasterController#postAddCarToDb');
then in Controller
if you still want to use reverse routing you can
$.ajax({
url: "<?php echo action(YourController#yourAction) ;?>",
method : "post",
data : completeData ,
success : function (data){
data=parse.JSON(data);
if(data.success == 1){
window.location="<?php echo url('master/add-car-view');?>";
}
},
error: function(data,status){
alert(JSON.stringify(data));
}
});

Related

I could not get the returned value from the controller to the ajax success function inthe view in CI

This is the ajax function in the view
<script type="text/javascript">
$('#name').on('change',function(){
var uid = document.getElementById('name').value;
$.ajax({
url : '<?php echo base_url('index.php/Manager_Settings_Controller/getUsername');?>',
method : 'get',
data : {'uid' : uid },
success : function(result){
console.log(result);
}
});
});
This is the getUsername function from the controller
public function getUsername(){
$uid = $this->input->get('uid');
$this->load->model('Manager_Settings_Model');
$data = $this->Manager_Settings_Model->getUsername($uid);
return $data['username'];
}
And this is the model function
function getUsername($uid){
$this->db->select('username');
$query = $this->db->get_where('user',array('u_id'=>$uid));
foreach($query -> result() as $row){
$data = array(
'username' => $row->username,
);
}
return $data;
}
Its really great if someone can help me. Thanks inadvance
public function getUsername(){
$uid = $this->input->get('uid');
$this->load->model('Manager_Settings_Model');
$data = $this->Manager_Settings_Model->getUsername($uid);
echo $data['username'];
}
You got to echo not return in controller, so you get the error or result whatever in console.log in success function of ajax.

Codeigniter inline edit not working

I created the following code, but do not run. Can you help?
My Controller
public function updateDb()
{
//$this->load->model('user_m');
$this->page_m->inline($_POST['file_description']);
return;
}
My Model
public function inline( $file_description, $id ){
$this->db->set($data)->where($this->$id, $page['file_description'])->update($this->file);
}
Form
<?php echo $file->file_description?>
Jquery
function showEdit(file_description) {
$(file_description).css("background","#FFF");
}
function save(file_description) {
$(file_description).css("background","#FFF url(<?php echo base_url('assets/img/loaderIcon.gif');?>loaderIcon.gif) no-repeat right");
$.ajax({
url: "<?php echo base_url()?>index.php/admin/page/updateDb",
type: "POST",
id : '<?php echo $page->id;?>',
name : 'file_description'
});
}
Looks like you're not passing the ID to your model which it requires.
public function inline( $file_description, $id ){...}
$this->page_m->inline($_POST['file_description'], $_POST['id']);
You could use $this->input->post() though...

added a new record using ajax and codeigniter.i want to redirect to the view page after adding record. so i want to get the last insert id

Ajax function and controller action is given below.anyone can help to get the last insertid to redirect.the redirection is done in ajax success function. can it do in controller action.its not getting
Ajax function
<script type="text/javascript">
$('#rfqsubmit').click(function () {
//var title = $('#title').val();
var form_data = {
title: $('#txtname').val(),
merid: $('#mermerchant').val(),
userid: $('#customer').val(),
description: $('#txtrequirement').val(),
reqid: $('#requirementid').val(),
shipmethod: $('#shipmethod').val(),
shiplocation: $('#shiplocation').val(),
attachment: $('#txtattachments').val(),
bidclose: $('#txtbidclose').val(),
ajax: '1'
};
$.ajax({
url: "<?php echo base_url() ?>moderator/RFQ/addoffline",
type: 'POST',
data: form_data,
success: function () {
window.location.href ="<?php echo base_url() ?>moderator/Employee/manageemployee/";
// window.location.href ="<?php //echo base_url() ?>moderator/RFQ/viewrfq/"+ form_data.reqid;
// alert('added Successfully');
}
});
return false;
});
</script>
Controller action
$this->load->helper(array('form', 'url'));
$this->load->helper('file');
$data7 = array(
'rfq_title' => $this->input->post('title'),
'rfq_detail' => $this->input->post('description'),
'rfq_merchantid' => $this->input->post('merid'),
'rfq_userid' => $this->input->post('userid'),
);
$add= $this->requirement_model->forminsert($data7);
}
Your question is not clear anyhow if i got what you need try this.
in your controller action return json response:
$this->load->helper(array('form', 'url'));
$this->load->helper('file');
$data7 = array(
'rfq_title' => $this->input->post('title'),
'rfq_detail' => $this->input->post('description'),
'rfq_merchantid' => $this->input->post('merid'),
'rfq_userid' => $this->input->post('userid'),
);
$inserted_id= $this->requirement_model->forminsert($data7);
//your forminsert method in model should return
//$this->db->insert_id();
$response=array('id'=>$inserted_id,'message'=>"inserted successfully");
echo json_encode($response);
die();
}
Your model function return last_inserted_id:
public function forminsert($data7)
{
$this->db->insert('jil_mrorfq',$data7);
//it will return last id
return $this->db->insert_id();
}
Now in ajax success :
$.ajax({
url: "<?php echo base_url() ?>moderator/RFQ/addoffline",
type: 'POST',
data: form_data,
dataType:"Json",
success: function (data) {
//to access that id
var last_inserted_id = data.id;
window.location.href ="<?php echo base_url() ?>moderator/Employee/manageemployee/"+last_inserted_id;
}
});

Ajax response Not working in Codeigniter

I want to show tick.png image on clicking save button when data inserted in database successfully. My view file name is insert_your_committee, model is users_model & controller user
Here is my code of all there files:
My Script of my insert_your_commitee file, All console.log show correct result but success function is not working. What is the problem?
<script>
function save_committee($c_id,$m_id,$start_date){
console.log($c_id);
console.log($m_id);
console.log($start_date);
var mid=$('#irnum'+$start_date).val();
console.log("df"+mid);
var url= "<?php echo site_url("user").'/ref_add';?>";
$.ajax({
url: url,
type: "POST",
dataType:'json',
data: {c_id:$c_id,m_id:$m_id,start_date:$start_date,irnum:mid},
success: function(data){
console.log(data);
$('#'+$start_date).show();
$('#btn'+$start_date).hide();
}
});
}
</script>
My Controller
public function ref_add()
{
$this->load->model('Users_model');
$parms=$this->input->post();
$c_id=$this->input->post('c_id');
$m_id=$this->input->post('m_id');
$s_dateparms=$this->input->post('start_date');
$irnum=$this->input->post('irnum');
$data_to_store = array(
'Cid' =>$c_id,
'Mid' =>$m_id,
'Month' => $s_dateparms,
'Year'=>$s_dateparms,
'Ref_Number' =>$irnum,
);
$params=array();
if ($this->users_model->add_ref($data_to_store)) {
$params['status'] = TRUE;
} else {
$params['status'] = false;
}
$return["json"] = json_encode($params);
echo json_encode($return);
}
And Here is my model
function add_ref($data)
{
$this->db->insert('reference', $data);
$report = array();
$report['error'] = $this->db->_error_number();
$report['message'] = $this->db->_error_message();
if($report !== 0){
return true;
}else{
return false;
}
}
I am not completely sure, but in order to get ajax to work, I had to add return false; after the ajax call in the function.
In my case, I was using ajax for login and backend was in codeigniter.

JQuery Ajax POST in Codeigniter

I have searched a lot of tutorials with POST methods and saw answered questions here too but my POST still doesn't work...I thought i should post it here if you guys see something that i don't!
My js - messages.js:
$(document).ready(function(){
$("#send").click(function()
{
$.ajax({
type: "POST",
url: base_url + "chat/post_action",
data: {textbox: $("#textbox").val()},
dataType: "text",
cache:false,
success:
function(data){
alert(data); //as a debugging message.
}
return false;
});
});
My view - chat.php:
<?php $this->load->js(base_url().'themes/chat/js/messages.js');?> //i use mainframe framework which loading script this way is valid
<form method="post">
<input id="textbox" type="text" name="textbox">
<input id="send" type="submit" name="send" value="Send">
</form>
Last My controller - chat.php
//more functions here
function post_action()
{
if($_POST['textbox'] == "")
{
$message = "You can't send empty text";
}
else
{
$message = $_POST['textbox'];
}
echo $message;
}
$(document).ready(function(){
$("#send").click(function()
{
$.ajax({
type: "POST",
url: base_url + "chat/post_action",
data: {textbox: $("#textbox").val()},
dataType: "text",
cache:false,
success:
function(data){
alert(data); //as a debugging message.
}
});// you have missed this bracket
return false;
});
});
In codeigniter there is no need to sennd "data" in ajax post method..
here is the example .
searchThis = 'This text will be search';
$.ajax({
type: "POST",
url: "<?php echo site_url();?>/software/search/"+searchThis,
dataType: "html",
success:function(data){
alert(data);
},
});
Note : in url , software is the controller name , search is the function name and searchThis is the variable that i m sending.
Here is the controller.
public function search(){
$search = $this->uri->segment(3);
echo '<p>'.$search.'</p>';
}
I hope you can get idea for your work .
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class UserController extends CI_Controller {
public function verifyUser() {
$userName = $_POST['userName'];
$userPassword = $_POST['userPassword'];
$status = array("STATUS"=>"false");
if($userName=='admin' && $userPassword=='admin'){
$status = array("STATUS"=>"true");
}
echo json_encode ($status) ;
}
}
function makeAjaxCall(){
$.ajax({
type: "post",
url: "http://localhost/CodeIgnitorTutorial/index.php/usercontroller/verifyUser",
cache: false,
data: $('#userForm').serialize(),
success: function(json){
try{
var obj = jQuery.parseJSON(json);
alert( obj['STATUS']);
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
}
The question has already been answered but I thought I would also let you know that rather than using the native PHP $_POST I reccomend you use the CodeIgniter input class so your controller code would be
function post_action()
{
if($this->input->post('textbox') == "")
{
$message = "You can't send empty text";
}
else
{
$message = $this->input->post('textbox');
}
echo $message;
}
<script>
$("#editTest23").click(function () {
var test_date = $(this).data('id');
// alert(status_id);
$.ajax({
type: "POST",
url: base_url+"Doctor/getTestData",
data: {
test_data: test_date,
},
dataType: "text",
success: function (data) {
$('#prepend_here_test1').html(data);
}
});
// you have missed this bracket
return false;
});
</script>

Resources