变量存在时未定义的变量 laravel
Posted
技术标签:
【中文标题】变量存在时未定义的变量 laravel【英文标题】:undefined variable when the variable exists laravel 【发布时间】:2020-11-04 01:50:37 【问题描述】:当我单击删除所有选择的它说未定义的变量时,但是当我检查数据库时,帖子被删除,当我刷新页面时,帖子被从页面中删除。我不明白为什么它在工作时会出错。
刀片
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js"></script>
<meta name="csrf-token" content=" csrf_token() ">
删除所有选择
帖子名称
帖子详情
行动
@foreach($posts as $key => $post)
id">
id">
++$key
$post->关于
$post->图像
id" class="btn btn-danger btn-sm"
data-tr="tr_$post->id"
数据切换=“确认”
data-btn-ok-label="删除" data-btn-ok-icon="fa fa-remove"
数据-btn-ok-class="btn btn-sm btn-danger"
data-btn-cancel-label="取消"
data-btn-cancel-icon="fa fa-chevron-circle-left"
data-btn-cancel-class="btn btn-sm btn-default"
data-title="你确定要删除吗?"
数据放置=“左”数据单例=“真”>
删除
@endforeach
<script type="text/javascript">
$(document).ready(function ()
$('#master').on('click', function(e)
if($(this).is(':checked',true))
$(".sub_chk").prop('checked', true);
else
$(".sub_chk").prop('checked',false);
);
$('.delete_all').on('click', function(e)
var allVals = [];
$(".sub_chk:checked").each(function()
allVals.push($(this).attr('data-id'));
);
if(allVals.length <=0)
alert("Please select row.");
else
var check = confirm("Are you sure you want to delete this row?");
if(check == true)
var join_selected_values = allVals.join(",");
$.ajax(
url: $(this).data('url'),
type: 'DELETE',
headers: 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
data: 'ids='+join_selected_values,
success: function (data)
if (data['success'])
$(".sub_chk:checked").each(function()
$(this).parents("tr").remove();
);
alert(data['success']);
else if (data['error'])
alert(data['error']);
else
alert('Whoops Something went wrong!!');
,
error: function (data)
alert(data.responseText);
);
$.each(allVals, function( index, value )
$('table tr').filter("[data-row-id='" + value + "']").remove();
);
);
$('[data-toggle=confirmation]').confirmation(
rootSelector: '[data-toggle=confirmation]',
onConfirm: function (event, element)
element.trigger('confirm');
);
$(document).on('confirm', function (e)
var ele = e.target;
e.preventDefault();
$.ajax(
url: ele.href,
type: 'DELETE',
headers: 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
success: function (data)
if (data['success'])
$("#" + data['tr']).slideUp("slow");
alert(data['success']);
else if (data['error'])
alert(data['error']);
else
alert('Whoops Something went wrong!!');
,
error: function (data)
alert(data.responseText);
);
return false;
);
);
</script>
</html>
```
控制器
public function showem(Post $post)
$posts = Post::get();
return view('users.registered', compact('posts'));
public function deleteAll(Request $request)
$ids = $request->ids;
$deleted = Post::whereIn('id',explode(",",$ids))->delete();
return view('users.registered');
【问题讨论】:
此处未定义哪个变量? 抱歉仍在编辑问题。 $post 变量 请同时显示控制器代码。 @aynber 控制器添加 @STA 错误的图片已添加到帖子中 【参考方案1】:在一次又一次地查看您的完整问题后,我发现您在 detele 方法上遇到了这个错误。
您还需要在删除方法上发送posts
变量:
public function showem(Post $post)
$posts = Post::get();
return view('users.registered', compact('posts'));
public function deleteAll(Request $request)
$ids = $request->ids;
$deleted = Post::whereIn('id',explode(",",$ids))->delete();
$posts = Post::get();
return view('users.registered', compact('posts')); // here send the posts variable
【讨论】:
以上是关于变量存在时未定义的变量 laravel的主要内容,如果未能解决你的问题,请参考以下文章