我正在尝试将$request
从控制器中的函数传递给模型中的函数。
这是我的控制器功能:
PostController.php
public function store(Request $request, post $post)
{
$post->title = $request->title;
$post->description = $request->description;
$post->save();
return redirect(route('post.index'));
}
如何在模型Post.php中保存数据?
我希望控制器仅充当发送信息的角色。信息被发送到模型。所有的计算和存储都在模型中进行
谢谢
答案 0 :(得分:1)
您可以使其变得更加容易。 Laravel有自己的助手“ request()”,可以在代码中的任何地方调用它。
因此,通常,您可以这样做:
PostController.php
public function store()
{
$post_model = new Post;
// for queries it's better to use transactions to handle errors
\DB::beginTransaction();
try {
$post_model->postStore();
\DB::commit(); // if there was no errors, your query will be executed
} catch (\Exception $e) {
\DB::rollback(); // either it won't execute any statements and rollback your database to previous state
abort(500);
}
// you don't need any if statements anymore. If you're here, it means all data has been saved successfully
return redirect(route('post.index'));
}
Post.php
public function postStore()
{
$request = request(); //save helper result to variable, so it can be reused
$this->title = $request->title;
$this->description = $request->description;
$this->save();
}
我将向您展示有关更新和创建的完整最佳实践示例:
web.php
Route::post('store/post/{post?}', 'PostController@post')->name('post.store');
yourform.blade.php -可用于更新和创建
<form action='{{ route('post.store', ['post' => $post->id ?? null]))'>
<!-- some inputs here -->
<!-- some inputs here -->
</form>
PostController.php
public function update(Post $post) {
// $post - if you sent null, in this variable will be 'new Post' result
// either laravel will try to find id you provided in your view, like Post::findOrFail(1). Of course, if it can't, it'll abort(404)
// then you can call your method postStore and it'll update or create for your new post.
// anyway, I'd recommend you to do next
\DB::beginTransaction();
try {
$post->fill(request()->all())->save();
\DB::commit();
} catch (\Exception $e) {
\DB::rollback();
abort(500);
}
return redirect(route('post.index'));
}
答案 1 :(得分:0)
您只需简单地实例化即可创建新模型:
$post = new Post; //Post is your model
然后将内容记录下来
$post->title = $request->title;
$post->description = $request->description;
,最后将其保存到数据库中:
$post->save();
要使用create方法将所有数据保存在模型中。使用create并在模型的fillable属性中设置列时需要设置质量分配。
protected $fillable = [ 'title', 'description' ];
然后使用输入内容调用
$post = Post::create([ 'parametername' => 'parametervalue' ]);
and if request has unwanted entries like token then us except on request before passing.
$post = Post::create([ $request->except(['_token']) ]);
希望这会有所帮助。
答案 2 :(得分:0)
根据描述,不确定要确切确定什么,但假设您想要一个干净的控制器和模型。这是一种方法
class Post {
$fillable = array(
'title', 'description'
);
}
class PostController extend Controller {
// store function normally don't get Casted Objects as `Post`
function store(\Request $request) {
$parameters = $request->all(); // get all your request data as an array
$post = \Post::create($parameters); // create method expect an array of fields mentioned in $fillable and returns a save dinstance
// OR
$post = new \Post();
$post->fill($parameters);
}
}
我希望对您有帮助
答案 3 :(得分:0)
我发现可以回答我的问题:
将$request
传递到模型Post.php中的my_method:
PostController.php:
public function store(Request $request)
{
$post_model = new Post;
$saved = $post_model->postStore($request);
//$saved = response of my_method in model
if($saved){
return redirect(route('post.index'));
}
}
并将数据保存在模型中:
Post.php
我们可以将实例或布尔值返回给控制器。
我将bool(保存方法响应)返回给控制器:
public function postStore($request)
{
$this->title = $request->title;
$this->description = $request->description;
$saved = $this->save();
//save method response bool
return $saved;
}
通过这种方式,所有计算和存储都在模型中执行(将数据保存在MVC中的最佳方式)
答案 4 :(得分:0)
public function store(Request $request)
{
$book = new Song();
$book->title = $request['title'];
$book->artist = $request['artist'];
$book->rating = $request['rating'];
$book->album_id = $request['album_id'];
$result= $book->save();
}