Php 将数据插入到Laravel 5.2中的数据透视表中
我是拉威尔的新手。我使用的是Laravel5.2,在将数据插入到pivot表时遇到了一个问题,我使用pivot表处理多对多关系。为了将数据传递到服务器,我使用jquery ajax post请求。它的代码如下Php 将数据插入到Laravel 5.2中的数据透视表中,php,mysql,pivot-table,laravel-5.2,Php,Mysql,Pivot Table,Laravel 5.2,我是拉威尔的新手。我使用的是Laravel5.2,在将数据插入到pivot表时遇到了一个问题,我使用pivot表处理多对多关系。为了将数据传递到服务器,我使用jquery ajax post请求。它的代码如下 $("#btnSave").click(function(){ var path = JSON.stringify(route); var token = $('input[name="_token"]').val(); $.post("/tour", { tourname:
$("#btnSave").click(function(){
var path = JSON.stringify(route);
var token = $('input[name="_token"]').val();
$.post("/tour",
{
tourname: $("#name").val(),
startpoint: $("#select_startpoint").val(),
endpoint : $("#select_endpoint").val(),
waypoints : path,
'_token': token
},function(){
alert("Path has been saved");
window.location.href = "/tour";
}); });
public function store(Request $request){
$user = Auth::user();
$tour = new Tour;
$tour->name = $request->tourname;
$tour->user_id = $user->id;
$tour->startpoint = $request->startpoint;
$tour->endpoint = $request->endpoint;
$tour->save();
$json = $request->waypoints;
$waypoints = json_decode($json);
foreach($waypoints as $waypoint){
$city = City::where('name', '=', $waypoint)->firstOrFail();
$tour->cities()->attach($city->id);
} }
这里的route是一个带有字符串集的JavaScript数组,我使用Json在服务器中传递值。这里我使用RESTful资源控制器来处理请求,其存储方法如下
$("#btnSave").click(function(){
var path = JSON.stringify(route);
var token = $('input[name="_token"]').val();
$.post("/tour",
{
tourname: $("#name").val(),
startpoint: $("#select_startpoint").val(),
endpoint : $("#select_endpoint").val(),
waypoints : path,
'_token': token
},function(){
alert("Path has been saved");
window.location.href = "/tour";
}); });
public function store(Request $request){
$user = Auth::user();
$tour = new Tour;
$tour->name = $request->tourname;
$tour->user_id = $user->id;
$tour->startpoint = $request->startpoint;
$tour->endpoint = $request->endpoint;
$tour->save();
$json = $request->waypoints;
$waypoints = json_decode($json);
foreach($waypoints as $waypoint){
$city = City::where('name', '=', $waypoint)->firstOrFail();
$tour->cities()->attach($city->id);
} }
在将数据插入透视表的过程中,我希望首先从数据库中获取特定城市的city\u id
,因为数组中只有该城市的名称。
当我执行代码时,巡更表得到正确更新,但透视表(city\u tour
)没有。当我进一步调试时,我注意到当自定义指定一个整数值时(例如:$tour->cities()->attach(2);
),代码工作正常。将值分配给查询中的$waypoint
变量似乎有问题。但我不明白,非常感谢你的帮助 您可以在以下位置尝试('name','LIKE',“%$waypoint%”)。。。。。“=”通常不能很好地演奏弦乐,除非是精确匹配
就像在SQL中得到最接近的匹配。
将%与类似项一起使用:
寻找城市“阿尔及尔”。
这会找到这个城市
$city = 'Algiers';
City::where('name', 'LIKE', "$city" )->firstOrFail();
如果你有一个空白,那么你可能什么也得不到
$city = ' Algiers';
City::where('name', 'LIKE', "$city" )->firstOrFail();
如果使用%则忽略空格或字符
$city = ' Algiers'; //extra space on the end
City::where('name', 'LIKE', "%$city" )->firstOrFail();
或者,如果您想忽略与单词结尾的任何偏差:
$city = 'Algier'; //with 's' missing
City::where('name', 'LIKE', "$city%" )->firstOrFail();
或者您不必使用LIKE,但要确保$city在列中
希望这有助于如果这有效$this->cities()->附加(2);那么您的问题可能在这里-->$city=city::where('name','=',$waypoint)->firstOrFail();您可以在以下位置尝试('name'、'LIKE'、“%$waypoint%”)。。。。。“=”通常不能很好地演奏弦乐,除非它是精确的match@HBensiali我尝试过你的想法,但失败了。但是当我在查询中使用一个字符串时(例如
$city=city::where('name','LIKE','cityname')->firstOrFail();
)。查询已执行。因此,变量似乎没有在查询中赋值。嘿,您必须在任意一侧添加%符号$city=city::where('name','LIKE',“%$cityname%”)->firstOrFail();无论如何。。。。“喜欢”是必须的字符串。“=”当你处理像整数或布尔数这样的绝对数时