Php 如何在laravel的文章中填充类别数据
类别模型Php 如何在laravel的文章中填充类别数据,php,laravel,laravel-5.5,Php,Laravel,Laravel 5.5,类别模型 <?php namespace App; use Illuminate\Database\Eloquent\Model; use App\Article; class Category extends Model { protected $table = 'categories'; public $timestamps = false; public function articles () { return $this->has
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Article;
class Category extends Model
{
protected $table = 'categories';
public $timestamps = false;
public function articles () {
return $this->hasMany(Article::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Category;
use App\User;
class Article extends Model
{
protected $table = 'articles';
public function category ()
{
return $this->belongsTo(Category::class);
}
public function user () {
return $this->belongsTo(User::class);
}
}
类别表迁移
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('category_id')->nullable()->unsigned();
$table->integer('user_id')->unsigned();
$table->longText('body');
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
下面是用于列出文章的控制器函数
public function index()
{
return Article::simplePaginate();
}
请看上面的截图
查看页面
<template>
<section>
<div class="btn-group" style="margin: 10px auto;">
<router-link to="/articles/new" class="button"><i class="fa fa-plus" style="margin-right: 10px"></i>New</router-link>
<button class="button is-primary" id="prev" @click="prev_page" :disabled="prev == null">Prev</button>
<button class="button is-primary" id="next" @click="next_page" :disabled="next == null">Next</button>
<button class="button is-danger" id="del" @click="delete_posts">
<i class="fa fa-trash" style="margin-right: 10px"></i>
Delete</button>
</div>
<b-field>
<b-input placeholder="search posts" type="search" icon-pack="fa" icon="search"></b-input>
</b-field>
<b-table
:data="articles"
:checked-rows.sync="checkedRows"
checkable >
<template slot-scope="props">
<b-table-column label="Title" centered>
<b-tooltip label="click to edit" position="is-top">
<a :href="'/admin/articles/' + props.row.id + '/edit'">{{ props.row.title }}</a>
</b-tooltip>
</b-table-column>
<b-table-column label="Author" centered>
{{ props.row.user_id }}
</b-table-column>
<b-table-column label="Category" centered>
{{ props.row.category_id }}
</b-table-column>
<b-table-column label="Date" centered>
{{ new Date(props.row.updated_at).toLocaleDateString() }}
</b-table-column>
</template>
</b-table>
</section>
</template>
<script>
export default {
data() {
return {
articles: [],
checkedRows: [],
prev: null,
next: null
}
},
methods: {
fetchArticles (url) {
let _this = this
this.$http.get(url)
.then(res => {
_this.articles = res.body.data
})
},
prev_page () {
this.fetchArticles(this.prev)
},
next_page () {
this.fetchArticles(this.next)
},
delete_posts () {
let _this = this
let id = '';
for(let i = 0; i < this.checkedRows.length; i++) {
id += this.checkedRows[i].id + ','
}
let url = '/api/articles/delete?id=' + id
this.$http.get(url)
.then( () => {
_this.fetchArticles('/api/articles')
_this.checkedRows = []
})
}
},
created () {
this.fetchArticles('/api/articles')
},
}
</script>
新的
上
下一个
删除
{{props.row.user_id}
{{props.row.category_id}
{{新日期(props.row.updated_at).toLocaleDateString()}}
导出默认值{
数据(){
返回{
第[…]条,
checkedRows:[],
prev:null,
下一个:空
}
},
方法:{
获取文章(url){
让这个=这个
这是.$http.get(url)
。然后(res=>{
_this.articles=res.body.data
})
},
上一页(){
this.fetchArticles(this.prev)
},
下一页(){
this.fetchArticles(this.next)
},
删除帖子(){
让这个=这个
设id='';
for(设i=0;i{
_this.fetchArticles(“/api/articles”)
_this.checkedRows=[]
})
}
},
创建(){
this.fetchArticles(“/api/articles”)
},
}
在上面的屏幕截图中,在“作者位置”列中需要类别名称
如何获取类别数据来代替类别id。使用
关系
。您需要加载您的类别
记录
return Article::with('category')->simplePaginate();
显示您的查看页面??当使用simplePaginate()时,它返回一个JSON,我需要所有文章的类别名称,它可以帮助您:)