我使用访存功能从数据库用户进行动态选择。当我单击时,ajax无效。
无法加载资源:服务器响应状态为500 (内部服务器错误)。如何解决?
My Controller
class TambahController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$user = User::where('jabatan','Captain Sales')->get();
return view('sales.tambah_aktivitas',compact('user'));
}
function fetch(Request $request)
{
$select = $request->get('select');
$value = $request->get('value');
$dependent = $request->get('dependent');
$data = DB::table('users')
->where($select, $value)
->groupBy($dependent)
->get();
$output = '<option value="" disabled selected >Pilih '.ucfirst($dependent).'</option>';
foreach($data as $row)
{
$output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
}
echo $output;
}
}
My View
<div class="mb-4">
<label for="username">Divisi <span class="badge badge-danger">wajib</span></label>
<select name="kepada_divisi" id="nik" class="form-control dynamic" data-dependent="username" required autofocus>
<option value="1" disabled selected >Pilih Divisi</option>
@foreach($user as $iniuser)
<option value="{{$iniuser->nik}}">{{$iniuser->nik}}</option>
@endforeach
</select>
</div>
<div class="mb-4" >
<label for="username">Username</label>
<select name="kepada" id="username" class="form-control dynamic" >
<option value="" disabled selected >-------------------------</option>
</select>
</div>
My AJAX
<script type="text/javascript">
$('.dynamic').change(function(){
if($(this).val() != '')
{
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var _token = $('input[name="_token"]').val();
$.ajax({
url:"{{ route('tambah_aktivitas.fetch') }}",
method:"POST",
data:{select:select, value:value, _token:_token, dependent:dependent},
success:function(result)
{
$('#'+dependent).html(result);
}
})
}
});
$('#nik').change(function(){
$('#username').val('');
$('#kategori3').val('');
});
$('#kategori2').change(function(){
$('#kategori3').val('');
});
$("select").val();
</script>
My Route
Route::get('/tambah_aktivitas','Sales\TambahController@index');
Route::post('tambah_aktivitas/fetch', 'Sales\TambahController@fetch')->name('tambah_aktivitas.fetch');
我希望当我单击“尼克选择”时,选择选项将显示与该尼克相同的用户名。
答案 0 :(得分:0)
您应该在storage/logs
文件夹中找到该错误的详细说明。乍看之下,问题出在这里:
$user = User::where('jabatan','Captain Sales')->get();
实际上返回一个集合。我想您想这样做:
$user = User::where('jabatan','Captain Sales')->first();
,或者您可以执行compact('user')
来代替$user->toArray()
。这样,在前端,您实际上会收到一个包含用户的数组,而不是包含Illuminate\Support\Collection
类的数组。