简体   繁体   中英

Laravel 5.0 - Using Request in function

I am building an application that uses the repository pattern. Now what I have done is reuse functionality but I have hit a bit of a hiccup. I have one view that shows several different models on it. These models are related through a one-to-one relationship (MovieBasic to MovieDetail) and one-to-many relationship (MovieBasic to MoviePersonnel). The issue I am having is that I have two different request to validate my forms. They are MovieBasicRequest, which validates my movie's basic information (Title, synopsis) and MovieDetailRequest, which validates my movie's detail information (price, screen type, runtime, etc). So to distinguish between which request to use I have added a parameter to my url as follows:

movie_basic.blade.php

<?php $params = ['id' => $movie->id, 'type' => 'movie_basic']; ?>
<h4>Movie Baiscs <span class="pull-right"><a href="{{ action('MovieController@edit', $params) }}">Edit</a></span></h4>
<hr>
<table class="table table-bordered">
  <tbody>
    <tr>
      <td>{{ $movie->movie_title}}</td>
    </tr>
    <tr>
      <td>{{ $movie->movie_synopsis }}</td>
    </tr>
  </tbody>
</table>

I know that using the <?php ?> tags is not best practice but I will clean that up later. So because of my $params the URL will look like so

www.moviesite.dev/1/edit?movie_basic

Which will call the edit function in the controller like so

MovieController.php

/**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $movie = $this->movieBasic->find($id);

        return view('cms.edit', compact('movie', 'type'));
    }

In this case the type does not really play a role because of the relationship between MovieBasic and MovieDetail models. However it does play a role in my update function below:

MovieController.php

/**
     * Update the specified resource in storage.
     *
     * @param  int  $id, MovieBasicRequest $request
     * @return Response
     */
    public function update($id)
    {
        if(strcmp($_GET['type'], 'movie_basic') == 0)
        {
            $movie = $this->movieBasic->find($id);
            $this->request = new MovieBasicRequest;
            $this->movieBasic->update($id, $this->request);
        }
        elseif(strcmp($_GET['type'], 'movie_detail') == 0)
        {
            $movie = $this->movieBasic->find($id);
            $this->request = new MovieDetailRequest;
            $this->movieDetail->update($id, $this->request);
        }

        return redirect()->action('MovieController@show', compact('movie'));
    }

Essentially what this function does is determine what is being passed in and from there call the correct request. However the way I have it now it just creates an empty array and thus validates nothing. Is there any way to uses these requests to validate information passed in? Or to validate input before I pass it to the update function of the repository?

PS. I have also tried this:

$this->movieBasic->update($id, MovieBasicRequest $request);

but I get an "Undefined variable $request" error.

You should better combine them. And you can use sometimes on your form validation for handling both where you will only validate present fields. So that your MovieRequest can be like below

class MovieRequest extends Request
{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'movie_title' => 'sometimes|required|min:3',
            'price' => 'sometimes|required|integer'
            // and so on
        ];
    }

}

So you can update your controller as below and use for the both pages. For instance, if the price is not set within the request, then it will skip validating it, but if it's present and empty, then it will throw an error message as it's required.

public function update($id, MovieRequest $request)
{
    $movie = $this->Movie->find($id);
    $movie->fill($request->all());
    $movie->save();
    return redirect()->action('MovieController@show', compact('movie'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM