1
には、2つのテーブルPost(id、text、id_tag)とTag(id、name)があります。 このテーブルを2つ作成する方法と、このテーブルを使用してワークフレームワークのモデルを作成する方法。DB内の2つの新しいテーブルのモデルと関係を作成
には、2つのテーブルPost(id、text、id_tag)とTag(id、name)があります。 このテーブルを2つ作成する方法と、このテーブルを使用してワークフレームワークのモデルを作成する方法。DB内の2つの新しいテーブルのモデルと関係を作成
次の2つのモデルを作成する必要があります1)タグ2)ポストのような:
1)タグ
<?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');
}
}
2)ポスト
<?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');
}
}
希望あなたのためにこの作品!!!
「php artisan make:model ModelName」のコマンドでは、 と使用してくださいhttps://laravel.com/docs/5.4/queries#joins –