BadMethodCallException 方法 [插入] 不存在。在 Laravel

问题描述 投票:0回答:2

我有模态形式的刀片文件以及插入数据库的 addbusiness 模型和 Post 控制器,但是当我尝试插入它时显示此错误

错误方法调用异常
方法[插入]不存在。

但是当我查看我的控制器时,有一个函数

insert
这是我的控制器代码

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

这是我的添加商业模式

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

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

我的模态形式看起来像这样

  <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>

请提供任何帮助和适当的文档,我们将不胜感激,因为我是 Laravel 的新手,并且已经研究这个问题好几天了。

更新 这是我的路线代码

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

Route::post('addbusiness', 
  ['as' => 'addbusiness', 'uses' => 'PostsController@insert']);
php laravel composer-php
2个回答
1
投票

您显示的路线调用 postsController 中的 insert 方法:

'PostsController@insert'

但是您向我们展示的插入方法位于

addbusinessController
中。 从您向我们展示的代码来看,这听起来确实应该在 addbusinessController 中,因此您应该更新路线以使用它。

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

1
投票

我想你可能想使用命名路线

所以你的路线看起来会是链接

Route::post('addbusiness', 'PostsController@insert')->name('addbusiness');

然后

{{ route('addbusiness') }}
助手应该为您返回正确的 URL。

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.