hi! In my application there is a user select field. when a user is selected it should show the users info like their total purchase sales and balance. so far i've done this:
this is the blade file
<td>
<select class="form-control select2 {{ $errors->has('team') ? 'is-invalid' : '' }}" name="fabricator_id" id="fabricator_id">
@foreach($fabricators as $user)
<option value="{{ $user->id }}" >{{ $user->identity }}</option>
@endforeach
</select>
</td>
<tr id="user_info">
<td>
Total Purchase: <div id="someDiv">this is where i want my data</div>
</td>
<td>
Total Points:
</td>
<td>
Total Redeemed Points:
</td>
<td>
Total Available Points:
</td>
</tr>
<script type="text/javascript">
$("#user_info").hide();
$(document).ready(function () {
//on change type
$('#fabricator_id').change(function (e) {
$.ajax({
url: "<?= url('/admin/fabricator/') ?>/" + $(this).val(),
method: 'GET',
dataType: 'json',
success: function (data) {
$("#user_info").show();
alert(data.name);
}
});
});
});
</script>
Controller:
public function fabricator($id){
$fabricator_details = User::select(\DB::raw('users.*, SUM(transactions.stock) as total_purchase, SUM(transactions.reward_points) as total_points, SUM( (transactions.redeem) )as redeemed_points' ))
->where('users.id', '=', $id)
->leftJoin('transactions', 'transactions.fabricator_id', '=', 'users.id')
->get();
return response()->json(['data'=>$fabricator_details]);
}
and this is my json response:
{"data":[{"id":20,"identity":"68099","name":"Md. Shahidul Islam","email":"[email protected]","email_verified_at":null,"created_at":"2021-12-30 05:06:34","updated_at":"2022-03-07 09:49:17","deleted_at":null,"team_id":16,"image":"","balance":1591140,"division":"Keraniganj","district":"Dhaka","upazilla":"Dhaka","total_purchase":"8008700","total_points":"1601740","redeemed_points":"12600"}]}
alert(data.name) alerts undefined