代码之家  ›  专栏  ›  技术社区  ›  Mostafa Norzade Sachin Shanbhag

laravel 5.7如何将控制器的请求传递到建模和保存

  •  1
  • Mostafa Norzade Sachin Shanbhag  · 技术社区  · 6 年前

    $request

    public function store(Request $request, post $post)
    {
    
        $post->title = $request->title;
        $post->description = $request->description;
    
        $post->save();
    
        return redirect(route('post.index'));
    
    }
    

    如何在model Post.php中保存数据?

    谢谢

    4 回复  |  直到 6 年前
        1
  •  2
  •   Farooq Ahmed Khan    6 年前

    标杆

    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);
        }
    }
    

    我希望有帮助

        2
  •  2
  •   Mostafa Norzade Sachin Shanbhag    6 年前

    因此,一般来说,您可以这样做:

    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'));
        }
    
        3
  •  1
  •   Mostafa Norzade Sachin Shanbhag    6 年前

    我想回答我的问题:

    通过 $request 对于model Post.php中的my_方法:

    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
  •   kshitij    6 年前

    您只需通过实例化创建新模型:

    $post = new Post;  //Post is your model
    

    然后把内容记录下来

    $post->title = $request->title;
    $post->description = $request->description;
    

    $post->save();
    

    使用“创建”方法保存模型中的所有数据。在模型的“可填充”属性中使用“创建”和“设置列”时,需要设置体量指定。

      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']) ]);
    

    希望这有帮助。

        5
  •  0
  •   li bing zhao    4 年前
     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();
     }