public function normalizeTopics(array $topics)
{
return collect($topics)->map(function ($topic){ //通过collect()和map()方法遍历数组
if (is_numeric($topic)){ //如果是数字就返回数字
Topic::find($topic)->increment('questions_count');//将话题关联的问题数量+1
return (int)$topic;
}
$newTopic = Topic::create(['name'=>$topic,'questions_count'=>1]);//如果不是数字就表示没有这个话题因此新建一个topic,将问题数设置为1
return $newTopic->id;//返回这个topic的id值
})->toArray();//转换成数组
}
这是全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{asset('resources/js_css/Bootstrap.css')}" rel="stylesheet">
<title>Laravel</title>
</head>
<body>
<div id="app">
<div class="container">
<crud :fields="fields"></crud>
</div>
</div>
<template id="crud-template">
<table class="sortable table-bordered table-hover table-striped">
<thead>
<tr>
<th v-for="field in fields">@{ field.name}</th>
</tr>
</thead>
<tbody>
<tr v-for="doctor in list">
<td v-for="field in fields">@{doctor[field.name]}</td>
</tr>
</tbody>
</table>
</template>
</body>
<script src="{asset('resources/js_css/jQuery.js')}"></script>
{--<script src="{asset('resources/js_css/vue.js')}"></script>--}
<script src="//cdn.bootcss.com/vue/1.0.27/vue.js"></script>
<script src="{asset('resources/js_css/sorttable.js')}"></script>
<script src="{asset('resources/js_css/vue-resource.min.js')}"></script>
<script>
Vue.component('crud',{
template:'#crud-template',
props:['fields'],
data:function () {
return{
list:{}
}
},
created:function () {
var vm=this;
//通过vue-resource.js拿到数据
// vm.$http.get('api/doctor').then((response)=>{
// vm.list=response.data;
// })
//通过vue-resource.js拿到数据用它的resource来拿数据
var resource=this.$resource('api/doctor');
resource.get().then((response)=>{
vm.$set('list',response.data);
})
//通过jQuery.js拿到数据
// $.getJSON('api/doctor',function (data) {
// vm.list=data;
// })
}
})
</script>
<script>
new Vue({
el:'#app',
data:{
fields:[
{name:'id',isKey:true},
{name:'name'},
{name:'age'},
{name:'sex'},
{name:'phone'}
]
}
})
</script>
</html>
对了,我表头排序是用的sorttable.js排序的实现的 <table class="sortable">
老师帮忙看看楼下的问题好吗, 我自己学,没人教,这个问题困扰了我一天了
我想问问各位大神,为什么我通过
Route::get('/', function () {
$doctors=\App\Doctor::latest()->get();
return view('welcome',compact('doctors'));
});
然后直接接收数据如: :list={$doctors}
提取数据库里面的数据放到表格里面后,点击表头排序Id的时候可以正常按照1,2,3,4,5,6,7,8,9,10,11,12...排序,
但是如果通过
Route::get('api/doctor', function () {
return \App\Doctor::latest()->get();
});
然后用
created:function () {
var vm=this;
//通过vue-resource.js拿到数据
// vm.$http.get('api/doctor').then((response)=>{
// vm.list=response.data;
// })
//通过jQuery.js拿到数据
$.getJSON('api/doctor',function (data) {
vm.list=data;
})
}
这上面两种方式拿到数据后,点击排序却是如下排序显示:
1,10,11,12,2,21,22,3,4,5,6,7,....,
求各位大神指点一下
JellyBool大神偶像,你讲得真的很好啊,我是一个初学者,看了你的视频,大概理解了之后,自己不断重复背着写代码,实现你讲的功能。不懂的再看,然后再重复自己写。现在感觉基本上理解了,不懂的地方我也会看官方文档了,laravel官方文档以前感觉看不太明白,看了你视频后,感觉有些东西都明白了不少,减小了我学习的难度。真心感谢你啊!而且还是免费的,伟大啊,真心的感谢!我会继续关注你的视频,希望新的视频能够尽快做出来。还有,能否有什么问题不懂的时候可否咨询一下你啊?我的邮箱是307958617@qq.com,希望大神能给我你的邮箱。感谢了,因为我是自学的,没有老师,也不是专业的,但是我就是感兴趣,所以有问题的时候也行是个小问题只要提点一下就会懂的,我要到百度很久才能找到答案,才能继续学下去。感觉学习效率太慢了。所以希望能够有空了帮忙指点一下。谢谢了先!