Php Larvel如何在查询中传递多个值

Php Larvel如何在查询中传递多个值,php,mysql,laravel,laravel-5,laravel-5.2,Php,Mysql,Laravel,Laravel 5,Laravel 5.2,我正在尝试使用laravel 5.2查询在表中创建新行 我的表格“answers”有5列->answer\u id(auto\u inc)、people\u id(与Auth::id相同)、question\u id('Integer')、answer(输入字段)、created\u at(timestamp()) 应答控制器文件: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Auth;

我正在尝试使用laravel 5.2查询在表中创建新行

我的表格“answers”有5列->
answer\u id(auto\u inc)、people\u id(与Auth::id相同)、question\u id('Integer')、answer(输入字段)、created\u at(timestamp())

应答控制器文件:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;

class answerController extends Controller
{
    public function store(Requests\answerAddRequest $request)
    {
        Auth::user()->questions('question_id')->answers()->create($request->all());
    }
}
我无法在答案表中添加新行,因为我无法理解如何在表中传递问题id

问题示范班

class answers extends Model
{
    protected $table='answers';
    protected $fillable='answer';
    protected $primaryKey='answer_id';
    public function setUpdatedAtAttribute($value) {}
    public function User()
    {
        return $this->belongsTo('App\User');
    }
    public function questions()
    {
        return $this->belongsTo('App\questions','people_id');
    }
}
class questions extends Model
{
    protected $table='questions';
    protected $primaryKey='question_id';
    protected $fillable = [
       'question', 'created_at','details'
    ];
    //protected $hidden=['question_id'];
    public function setUpdatedAtAttribute($value) {}
    public function User() {
        return $this->belongsTo('App\User');
    }
    public function answers()
    {
        return $this->hasMany('App\answers','answer_id');
    }
    public function scopefetchQuestions($query) {
        return $query->select('question','question_id')->where('people_id','=',Auth::id())->get();
    }
}
它正在使用当前代码引发此错误:

BadMethodCallException in Builder.php line 2405:
Call to undefined method Illuminate\Database\Query\Builder::answers()
如何修复它?

请尝试以下操作:

class answerController extends Controller
{
    public function store(Requests\answerAddRequest $request)
    {
        $answer = App\Answer::create($request->all());
        Auth::user()->questions()->where('question_id', $request->get('question_id'))->first()->answers()->save($answer);
    }
}
还要将外键添加到
$filleble
数组中

你是真的把一个问题id放在问题中(“问题id”)还是把它作为一个字符串?没有“问题id”是“问题”表中的一列查询成功了,但现在
Auth::user()
没有返回任何“id”,我得到的是“0”表示用户id用户只有
id
,如果要更改它,您需要更改
受保护的$primaryKey
变量,但在这种情况下,旧的用户数据将被损坏,您需要更改与用户模型交互的每个关系方法的本地键和外键。最好只保留
id