简体   繁体   English

Laravel - 如何获取外键并将其传递给控制器

[英]Laravel - How to get a foreign key and pass it to controller

我正在创建一个调查,为了创建一个问题,我需要从调查表中获取survey_id并在其中放置一个在表单中的隐藏字段中创建问题视图并将其传递给问题控制器。

Since you passe survey_id all you need to the do is use the right model (Question) in the store method, everything else same correct. 由于你通过survey_id所有你需要做的就是在store方法中使用正确的模型(Question),其他一切都是正确的。

 /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

      $input = $request->all();
      Question::create($input); //<==== You need to use Question insteed of Survey
      return redirect('survey');
    }

You can try below code,i hope this is help you: 您可以尝试下面的代码,我希望这可以帮助您:

$input = $request->all();
$survey = new Survey ();
$survey ->fill($input)->save();
if (isset($input['survey_id']) && !empty($input['survey_id'])) {
            $survey_id= $input['survey_id'];

            foreach ($survey_id as $index => $value) {
                Question::create([
                    'survey_id' => $survey ->id,
                    'title' => $value
                ]);
            }
        }

@Rachid's comments is right, but its not the reason of the error... @Rachid的评论是对的,但不是错误的原因......

The problem is the resource's route -> controler@action mapping doesn't include any param for the /create route as we can see bellow... 问题是资源的route -> controler@action mapping不包含/create路由的任何参数,我们可以看到如下...

来自Laravel的资源控制器文档......

... and your QuestionController::create function needs the param $id . ...而你的QuestionController::create函数需要param $id

//...
class QuestionController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id) // <---- Right here...
//...

So, to bind your foreign key to this resource i'll need to do something like this... 所以,要将你的外键绑定到这个资源,我需要做这样的事情......

Route::resource('survey.question', 'QuestionController');

This way you'll have your routes mapped sothing like this... 这样你就可以将你的路线映射成这样的......

GET /survey/{survey}/question           survey.question.index
GET /survey/{survey}/question/create    survey.question.create

Then change your template, from: 然后更改您的模板:

{!! Form::open(array('action' => 'QuestionController@store', 'id' => 'Question')) !!}

To: 至:

{!! Form::open(array('action' => 'QuestionController@store', 'id' => 'Question'), ['survey' => Request::route('survey')]) !!}

Then you can use your QuestionController::create function like this... 然后你可以使用你的QuestionController::create函数......

public function create(Survey $survey)
{
    return view('question.create')->with('survey', $survey);
}

You can also use parameters in resourceful routes like this: 您还可以在资源丰富的路线中使用参数,如下所示:

Route::resource('survey/{survey}/question', 'QuestionController');

Then in your controller you can use: 然后在您的控制器中,您可以使用:

class QuestionController extends Controller
{
    // ...

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        // $id will be the {survey} id from the route parameter.
    }

    // ...
}

You could as well use route model binding to avoid the manual Survey retrival in the controller: 您也可以使用路径模型绑定来避免控制器中的手动Survey retrival:

use App\Survey;

class QuestionController extends Controller
{
    // ...

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Survey $survey)
    {
        return view('question.create', compact('survey'));
    }

    // ...
}

Remember to update the form action as well to include the pameter 请记住更新表单操作以包括pameter

Be Careful! 小心! If you register the parameter in the resource like I have shown, methods as show, edit, update and destroy (that accepts the id of the resource to act on in the url) will receive two route parameters, one will be the survey id , the second one would be the question id (your resource). 如果您在资源中注册参数,就像我已经显示的那样,show,edit,update和destroy等方法(接受要在url中执行的资源的id)将收到两个路由参数,一个是调查ID ,第二个是问题ID (您的资源)。 Keep that in mind! 记住这一点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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