我在公共文件夹上有一个音频文件,如下所示 -
@foreach ($songs as $song)
<a href="{{asset('/audio/' . $song->song)}}" download="" >
<button type="button" class="btn btn-xs btn-info download" style="margin-bottom: 15px;">
<i class="glyphicon glyphicon-download">Download</i>
</button>
</a>
@endforeach
因此,当我点击该按钮时,它会下载该歌曲。我有一个歌曲数据库,我把歌曲和列download_count
设置为默认(0)。我想在每次单击按钮时增加download_count
。那我怎么能把这个逻辑传递给控制器来更新数据库?
答案 0 :(得分:0)
您只需通过Ajax动态更新下载计数。
一个简单的例子:
@foreach ($songs as $song)
<a href="{{asset('/audio/' . $song->song)}}">
<button type="button" class="download" data-id="{{ $song->id}}">Download</button>
</a>
@endforeach
<script>
$('.download').click(function(){
$.ajax({
url: "/update_download_count",
type:"POST",
data: {
song_id: $(this).attr('data-id')
},
success: function (data) {
console.log(data);
},
error: function (request, status, error) {
console.log('code: '+request.status+"\n"+'message: '+request.responseText+"\n"+'error: '+error);
}
});
});
</script>
您的路线:
Route::post('/update_download_count', 'SongController@updateDownloadCount');
您的控制器:
public function updateDownloadCount(){
$song_id = $_POST['song_id'];
// your database update logic
}