[英]Passing Array to controller for saving in many-to-many relationship
I am trying to allow my users to add tags to submitted job postings. 我正在尝试允许我的用户将标签添加到已提交的职位发布中。
To do this, I am using the bootstrap-tokenfield plugin. 为此,我正在使用bootstrap-tokenfield插件。
Jquery script to gather array and submit: jQuery脚本收集数组并提交:
//create a new array and push the skills on to it.
var tokenfield = new Array();
$(".skills").each(function() {
tokenfield.push($(this).val());
});
This is then submitted to my controller function along with all of the form details using ajax. 然后使用ajax将其与所有表单详细信息一起提交给我的控制器功能。
controller: 控制器:
//save job
$job = new Job;
$job->user_id = $this->userID;
//other fields...
$job->save();
//After job is saved iterate over tokenfield array and save to pivot table using Laravel Eloquent properties:
foreach($_POST['tokenfield'] as $val){
//only skill name is passed from form. Find id:
$skill = Skill::where('name','=', $val)->first();
if(JobSkill::create(array('job_id' => $job->id, 'skill_id' => $skill->id)))
{ return Response::json(array('success' => 200));}
I am getting the error: attempting to get property of non-object related to $skill->id so I did a dd($_POST['tokenfield']); 我收到错误消息:尝试获取与$ skill-> id相关的非对象的属性,因此我做了dd($ _ POST ['tokenfield']); and this was the result:
结果是:
array(1) { [0]=> string(33) "Window Installation, deck removal"}
My question is: Am I not creating the array properly or am I not accepting it properly in the controller? 我的问题是:是我没有正确创建数组还是在控制器中没有正确接受它? TIA
TIA
Simply Put Laravel is not accepting $_POST['tokenfield'] 简而言之,Laravel不接受$ _POST ['tokenfield']
not so great at JAVA but 在JAVA上不是很好,但是
var tokenfield = new Array();
$(".skills").each(function() {
tokenfield.push($(this).val());
});
aint this meant to be 不是这个意思是
var tokenfield = new Array(
$(".skills").each(function() {
tokenfield.push($(this).val());
}));
correct me if i am wrong but to me this looks like that the controller cant find the post data 纠正我,如果我错了,但是对我来说,这似乎是控制器找不到发布数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.