php - Laravel - 如何在我的 Laravel 代码中从 json 中提取值
问题描述
在我的Laravel-5.8中,我使用 JSON 将数据从控制器传递到视图。
我正在将 Laravel-5.8 用于 Web 应用程序项目。
我正在尝试在更改时在文本框中设置我的下拉加载值。
public function findScore(Request $request)
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first();
$child = DB::table('appraisal_goal_types')->where('company_id', $userCompany)->where('id',$request->id)->first();
$parentid = DB::table('appraisal_goal_types')->select('parent_id')->where('company_id', $userCompany)->where('id',$request->id)->first();
if(empty($child))
{
abort(404);
}
$weightedscore = DB::table('appraisal_goals')->select(DB::raw("SUM(weighted_score) as weighted_score"))->where('appraisal_identity_id', $identities)->where('employee_id', $userEmployee)->where('parent_id', $parentid)->get();
$maxscore = DB::table('appraisal_goal_types')->select('max_score')->find($child->parent_id);
return response()->json([
'maxscore' => $maxscore->max_score,
'weightedscore' => $weightedscore
]);
}
我将 max_score 和 weighted_score 作为 JSON 发送。
路线:
Route::get('get/findScore','Appraisal\AppraisalGoalsController@findScore')->name('get.scores.all'); 查看刀片
<form action="{{route('appraisal.appraisal_goals.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Goal Type:<span style="color:red;">*</span></label>
<select id="goal_type" class="form-control" name="goal_type_id">
<option value="">Select Goal Type</option>
@foreach ($categories as $category)
@unless($category->name === 'Job Fundamentals')
<option hidden value="{{ $category->id }}" {{ $category->id == old('category_id') ? 'selected' : '' }}>{{ $category->name }}</option>
@if ($category->children)
@foreach ($category->children as $child)
@unless($child->name === 'Job Fundamentals')
<option value="{{ $child->id }}" {{ $child->id == old('category_id') ? 'selected' : '' }}> {{ $child->name }}</option>
@endunless
@endforeach
@endif
@endunless
@endforeach
</select>
</div>
</div>
<input type="text" id="max_score" class="form-control" >
<input type="text" id="weighted_score" class="form-control" >
</form>
<script type="text/javascript">
$(document).ready(function() {
$(document).on('change', '#goal_type', function() {
var air_id = $(this).val();
var a = $(this).parent();
console.log("Its Change !");
var op = "";
$.ajax({
type: 'get',
url: '{{ route('get.scores.all') }}',
data: { 'id': air_id },
dataType: 'json', //return data will be json
success: function(data) {
// console.log("price");
console.log(data.maxscore);
console.log(data.weightedscore);
$('#max_score').val(data.maxscore);
$('#weighted_score').val(data.weightedscore);
},
error:function(){
}
});
});
});
</script>
当我单击更改下拉菜单时,max_score 工作正常,但 weighted_score 有错误:
GET http://localhost:8888/peopleedge/get/findScore?id=2 500 (Internal Server Error).
然后我在控制台上得到了这个:
和文本框:
我需要在文本上显示直接值,而不是在图中显示的 JSON 对象。例如,文本框中只有 60 个。
我该如何解决这个问题?
谢谢你。
解决方案
$weightedscore
是一个对象,所以你应该对$weightedscore
你所做的事情做同样的事情$maxscore
:将属性而不是对象添加到响应中。
更改$weightedscore
为$weightedscore->weighted_score
中的return
部分。
return response()->json([
'maxscore' => $maxscore->max_score,
'weightedscore' => $weightedscore->weighted_score
]);
您可以在使用or$weightedscore
返回它之前输出它,然后您将看到它是一个对象,该对象具有一个属性,如您的 SQL 语句部分所述。var_dump($weightedscore);
print_r($weightedscore);
weighted_score
SELECT ... as()
推荐阅读
- java - 无法滚动动态添加的片段 (Java)
- html - 打印贴纸之间的空白页 HTML CSS
- excel - 如何找到值然后将整行复制到另一张纸
- python - 错误:命令引发异常:AttributeError:“消息”对象没有属性“成员”
- json - 防止 Azure Logic App 将 JSON 输出到单行
- rust - Rust 声明性宏和 tt 的作用域
- javascript - 如何在反应中获取用户来自的先前路线
- c# - 基础提供程序在打开时失败(SQL Server 2019 和 EF)
- javascript - 将参数分配给事件是如何工作的?
- python - 在 sklearn.linear_model._logistic 中遇到问题