我有一个接受参数的自定义路由。这是:在laravel中获取create方法的参数值时返回null
Route::get('reservation/{id}/create',
['as' => 'reservation.create', 'uses' => '[email protected]'
]);
我有这个变量保护$ student_id。该ID被分配到ReservationController的创建方法的参数,如下图所示:
class ReservationController extends Controller
{
protected $student_id;
public function index()
{
return $this->student_id;
}
public function create($id)
{
$this->student_id = $id;
$subjects = Subject::with('sections')->get();
return view('reservation.form',compact('subjects'));
}
public function store(Request $request)
{
$subject = new Reservation();
$subject->section_subject_id = $request->sectionSubjectId;
$subject->student_id = $this->student_id;
$subject->save();
}
}
当返回$ id参数的创建方法,我得到确切的ID号。我还为$ student_id分配了$ id。但是,当在商店方法上分配$ student_id时,我会得到空值。我知道我在这里做错了,有人可以帮我解决这个问题。
好吧,让我添加一些信息:在我的网址时使用reservation.create路由我有这个地址localhost:8000/reservation/1 /创建 该网址的数字1是我想要的学生ID在我的商店方法中获取并分配给student_id。
我也有这种形式的看法:
form.blade.php
<body>
@foreach($subjects as $subject)
@foreach($subject->sections as $section)
<tr>
<td>{{ $section->section_code }}</td>
<td>{{ $subject->subject_code }}</td>
<td>{{ $subject->subject_description }}</td>
<td>{{ $section->pivot->schedule }}</td>
<td>{{ $subject->units }}</td>
<td>{{ $section->pivot->room_no }}</td>
<td>
<button
v-on:click="addSubject({{ $section->pivot->id }})"
class="btn btn-xs btn-primary">Add
</button>
<button class="btn btn-xs btn-info">Edit</button>
</td>
</tr>
@endforeach
@endforeach
</body>
同时我让我们vue.js和VUE资源
all.js
的methods:{
addSubject: function(id){
this.$http({
url: 'http://localhost:8000/reservation',
data: { sectionSubjectId: id },
method: 'POST'
}).then(function(response) {
console.log('success');
},function (response){
console.log('failed');
});
}
}
因为student_id数据为空,则使用 $主语> student_id数据= $请求 - > ID;或$请求 - > student_id数据或任何你使用 –