phplaravelcomposer-php

BadMethodCallException method [insert] does not exist. in laravel


I have blade file in modal form and a addbusiness model and Post controller that insert to database but when I try insert it show me this error

BadMethodCallException
Method [insert] does not exist.

But when I look at my controller there is a function as insert here is my controller code

<?php

namespace App\Http\Controllers;
use App\Post;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Requests;
//use DB;

use Session;

class addbusinessController extends Controller
{
    
    public function index(){
        //fetch all posts data
        $posts = Post::orderBy('created','desc')->get();
        
        //pass posts data to view and load list view
        return view('posts.index', ['posts' => $posts]);
    }
    
    public function details($id){
        //fetch post data
        $post = Post::find($id);
        
        //pass posts data to view and load list view
        return view('posts.details', ['post' => $post]);
    }
    
    public function add(){
        //load form view
        return view('posts.add');
    }
    
    public function insert(Request $request){
        //validate post data
        $this->validate($request, [
            'Fname' => 'required',
            'content' => 'required'
        ]);

        DB::table(' business')->insert(
            ['owner_id' => 3, 'bus_name' => 'Fname',]
        );
        
        //get post data
        /*$postData = array(
            'title' => $request->input('title'),
            'content' => $request->input('content'),
        );*/
        $postData = $request->all();
        
        //insert post data
        Post::create($postData);
        
        //store status message
        Session::flash('success_msg', 'Post added successfully!');

        return redirect()->route('dashboard');
    }
    
    public function edit($id){
        //get post data by id
        $post = Post::find($id);
        
        //load form view
        return view('posts.edit', ['post' => $post]);
    }
    
    public function update($id, Request $request){
        //validate post data
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required'
        ]);
        
        //get post data
        $postData = $request->all();
        
        //update post data
        Post::find($id)->update($postData);
        
        //store status message
        Session::flash('success_msg', 'Post updated successfully!');

        return redirect()->route('dashboard');
    }
    
    public function delete($id){
        //update post data
        Post::find($id)->delete();
        
        //store status message
        Session::flash('success_msg', 'Post deleted successfully!');

        return redirect()->route('posts.index');
    }
    
}`

here is my add business model

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class addbusiness extends Model
{
      //fillable fields
    protected $fillable = ['Fname', 'staffphn'];
}
`

and my modal form looks like this

  <script>
  function AddNewBus(){
        var title = '<center>New Business</center>';
        var size='large';
       var content = '<form action="{{ route('addbusiness') }}" id="UpUser" method="post">'+
          '<center><label id="msg1"></label></center>'+
          '<div class="form-group has-feedback">'+
         ' {{ csrf_field() }}'+
            '<input type="text" class="form-control" name="Fname" id="Fname" value="" placeholder="Business Name" required />'+
            '<span class="glyphicon glyphicon-briefcase form-control-feedback"></span>'+
          '</div>'+
           
          '<div class="form-group has-feedback">'+
            '<select class="form-control" id="ubuscountry" name="ubuscountry" onchange="setState(this);"  >' +
              '<option value="">--Select Your Country--</option>'+
               
            '</select><span class="glyphicon glyphicon-user form-control-feedback"><label style="color:red;">*</label></span>'+
          '</div>'+
           '<div class="form-group has-feedback">'+
            '<select class="form-control" id="ubusstate" name="ubusstate"  >' +
              '<option value="">--Select Your State/Province--</option>'+
             

          '</select>'+
            '<span class="glyphicon glyphicon-phone form-control-feedback"><label style="color:red;">*</label></span>'+
          '</div>'+
          '<div class="form-group has-feedback">'+
            '<input type="number" class="form-control" name="staffphn" id="staffphn" value="" placeholder="Phone Number" />'+
            '<span class="glyphicon glyphicon-phone form-control-feedback"><label style="color:red;">*</label></span>'+
          '</div>'+
          '<div class="form-group has-feedback">'+
            '<input type="email" class="form-control" name="staffmail" id="staffmail" value="" placeholder="Email" />'+
            '<span class="glyphicon glyphicon-envelope form-control-feedback"></span>'+
          '</div>'+
          '<div class="form-group has-feedback">'+
            '<textarea name="Address" id="Address" class="form-control" placeholder="Address with LAND MARK"></textarea>'+
            '<span class="glyphicon glyphicon-home form-control-feedback"><label style="color:red;">*</label></span>'+
          '</div>'+
         
          '<div class="row">'+
            '<!-- /.col -->'+
            '<div class="col-xs-4 pull-right">'+
              '<button type="submit"  name="addbus" value="kk" class="btn btn-success btn-block btn-flat">Add Business</button>'+
            '</div>'+
          '</div>'+

       '</form>';
        var footer = '<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>';

              setModalBox(title,content,footer,size);
              $('#myModal').modal('show');
      }
</script>

please any help and proper documentation would be appreciated as am new to laravel and have been on this issue for some days.

UPDATED here is my route code

Route::get('/', 'HomeController@index')->name('dashboard');

Route::post('addbusiness', 
  ['as' => 'addbusiness', 'uses' => 'PostsController@insert']);

Solution

  • The route you show calls the insert method in the postsController:

    'PostsController@insert'
    

    But the insert method you showed us is in the addbusinessController. From the code you've show us it sounds like this really should be in addbusinessController, so you should update your route to use it.

    Route::post('addbusiness', 
        ['as' => 'addbusiness', 'uses' => 'addbusinessController@insert']);