如何存储表之间的关系

时间:2016-07-18 17:48:55

标签: php laravel laravel-5.1

我创建了一个与其他两个表相关的表,但是在创建新项目时我不知道如何关联它们,我怎么能这样做?

我会在管理员的单独页面中创建类别,当用户创建新项目时,他将能够从表格中选择一个类别数组。

我的问题是,如何在POST数据时存储关系?我以前从未这样做过。

项目模型

class Project extends Model
{
    protected $table = 'projects';

    protected $fillable = [
        'name',
        'slug',
        'header',
        'desc',
        'about',
        'url',
        'status'
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function category()
    {
        return $this->belongsToMany(Category::class);
    }

    public function categories()
    {
        return $this->hasMany(Category::class);
    }
}

类别模型

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [
        'name',
        'status'
    ];

    public function subCategory()
    {
        return $this->hasMany(SubCategory::class);
    }

    public function projects()
    {
        return $this->belongsToMany(Project::class);
    }
}

我的实际帖子创建

public function postCreate(ProjectCreateRequest $request, Customer $customer)
{

    //Array
    $categories = $request->categories;

    $customer->projects()->create([
        'name' => $request->name,
        'header' => $request->header,
        'desc' => $request->desc,
        'about' => $request->about,
        'url' => $request->url,
    ]);

    //How do I store the relation?

    return redirect('admin/clientes/editar/' . $customer->id);
}

2 个答案:

答案 0 :(得分:1)

对多对多关系使用attachsync 参考:https://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models

public function postCreate(ProjectCreateRequest $request, Customer $customer)
{
    //Array
    $categories = $request->categories;

    $projects = $customer->projects()->create([
        'name' => $request->name,
        'header' => $request->header,
        'desc' => $request->desc,
        'about' => $request->about,
        'url' => $request->url,
    ]);

    //suppose here the $categories is an array of ids or a single integer variable id of the category that are related
    $projects->categories()->attach($categories);

    return redirect('admin/clientes/editar/' . $customer->id);
}

答案 1 :(得分:0)

首先:确保数据库中包含以下表格,以便多对多关系正常工作

表名:category_project

列:category_id,project_id

第二:类Project只需要以下函数即可使类别关系起作用:

public function categories()
{
    return $this->belongsToMany(Category::class);
}

第三:类类别只需要以下函数即可实现项目关系:

public function projects()
{
    return $this->belongsToMany(Project::class);
}

第四: postCreate函数应该如下:

public function postCreate(ProjectCreateRequest $request, Customer $customer)
{

//should be an array of categories ids
$categories = $request->categories;

$project = $customer->projects()->create([
    'name' => $request->name,
    'header' => $request->header,
    'desc' => $request->desc,
    'about' => $request->about,
    'url' => $request->url,

]);

//How do I store the relation? you can use attach or sync
$project->categories()->sync($categories);

return redirect('admin/clientes/editar/' . $customer->id);
}