我是Laravel 5.3的新手并且正在学习。现在我已经创建了表单,我想通过控制器和模型将表单值插入数据库。现在我从表单到控制器获取所有数据,我想将它传递给模型。但是当我将数据传递给模型时我得到一个错误< / p>
"BadMethodCallException in Controller.php line 82:
Method [savemodel] does not exist"
这是我的查看页面代码:
{{Form::open(array('action' => 'Testing@store', 'method' => 'post'))}}
<div class="form-group">
{!! Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Description:', ['class' => 'control-label']) !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Submitform', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
这是我的Controller(Testing.php):
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\TestingModel;
class testing extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('pages.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
echo 'hi';
$storeform=new TestingModel();
$storeform->savemodel($request);
}
}
这是我的模型(TestingModel):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model
{
public function savemodel()
{
echo 'hello';exit;
}
}
我不知道我做得对不对。如何从模型中插入数据?请帮帮我
答案 0 :(得分:0)
这是我的模态。
`
<?php`
`namespace App;`
`use Illuminate\Database\Eloquent\Model;`
`use App\UserUploads;`
`use App\FileUplods;`
`class FileUplods extends Model`
`{`
`protected $table = 'upload_files';`
`protected $fileable = ['user_id', 'origanal_file_name' , 'sortby' , 'new_file_name_after_upload'];`
`}`
and here is my controller
`<?php`
`namespace App\Http\Controllers;`
`use Illuminate\Http\Request;`
`use App\Http\Requests;`
`use App\FileUplods;`
`class ImageController extends Controller`
`{`
`public function ajaxfileupload(Request $request)`
`{`
`$fileupload = new FileUplods();`
`$fileupload->user_id = $user_id;`
`$fileupload->origanal_file_name = $request->origanal_file_name;`
`$fileupload->sortby = $request->sortby;`
`$fileupload->new_file_name_after_upload = $request->new_file_name_after_upload;`
`$fileupload->save();`
`}`
`}
这是我的控制器尝试这样做会非常有效。
答案 1 :(得分:0)
首先在控制器中为模型添加命名空间
use App\TestingModel
然后你的控制器功能就像这样
public function store(Request $request)
{
//echo 'hi';
$storeform=new TestingModel;
$storeform->title = $request->input('title');
$storeform->description = $request->input('description');
$storeform->save();
}
并像这样编辑你的模型
$table = 'table_name';
$fillable = [ 'title', 'description' ];
如果你想从模型中运行函数,那么就像这样编写代码
$storeform = TestingModel::savemodel();