Php 为数据库中的两个新表创建模型和关系
我有两个表Post(id,text,id_标记)和tag(id,name)。Php 为数据库中的两个新表创建模型和关系,php,mysql,database,laravel,laravel-5.3,Php,Mysql,Database,Laravel,Laravel 5.3,我有两个表Post(id,text,id_标记)和tag(id,name)。 如何在此表中创建两个模型的关系,以及如何使用此表为工作框架创建模型。您应该创建两个模型1)标记2)类似以下内容的帖子: 1) 标签 您应该创建两个模型1)标记2)帖子,如: 1) 标签 对于模型php artisan make:Model ModelName在命令中使用并转到模型php artisan make:Model ModelName在命令中使用并转到 <?php namespace App\Model
如何在此表中创建两个模型的关系,以及如何使用此表为工作框架创建模型。您应该创建两个模型1)标记2)类似以下内容的帖子: 1) 标签
您应该创建两个模型1)标记2)帖子,如:
1) 标签
对于模型php artisan make:Model ModelName
在命令中使用并转到模型php artisan make:Model ModelName
在命令中使用并转到
<?php
namespace App\Models\frontend;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'tag';
/**
* The database primary key value.
*
* @var string
*/
protected $guarded = ['id', '_token'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['name'];
/**
* That belong to the Tag.
*/
public function post()
{
return $this->hasMany('App\Models\Post');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'post';
/**
* The database primary key value.
*
* @var string
*/
protected $guarded = ['id', '_token'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['text','id_tag'];
/**
* The roles that belong to the Post.
*/
public function tag()
{
return $this->belongsTo('App\Models\Tag','id_tag');
}
}