Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.7 how to pass request of controller to model and save

I am trying to pass $request from a function in controller to a function in model.

THis is my controller function:

PostController.php

public function store(Request $request, post $post)
{

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

    $post->save();

    return redirect(route('post.index'));

}

how save data in model Post.php?

I want the controller to only be in the role of sending information. Information is sent to the model. All calculations and storage are performed in the model

Thanks

like image 970
Mostafa Norzade Avatar asked Dec 28 '25 19:12

Mostafa Norzade


2 Answers

You can make it even easier. Laravel has it's own helper "request()", which can be called anywhere in your code.

So, generally, you can do this:

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

I'll show you full best practice example for update and create:

web.php

    Route::post('store/post/{post?}', 'PostController@post')->name('post.store');

yourform.blade.php - can be used for update and create

    <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'));
    }
like image 138
Сергей Игоревич Avatar answered Jan 01 '26 18:01

Сергей Игоревич


Based on description, not sure what you want exactly but assuming you want a clean controller and model . Here is one way

Model - Post

class Post {
    $fillable = array(
        'title', 'description'
    );
}

PostController

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

I hope it helps

like image 44
Farooq Ahmed Khan Avatar answered Jan 01 '26 18:01

Farooq Ahmed Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!