Caio Kawasaki
Caio Kawasaki

Reputation: 2950

How to store the relation between tables

I created a table in relation to two other tables, however I do not know how to relate them when creating new projects, how could I do that?

I'll create the categories in a separated page in my admin, and when the user create's a new project he will be able to select an array of categories coming from the table.

My question is, how can I store the relation when POST the data? I've never done this before.

Project model

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);
    }
}

Category model

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);
    }
}

My actual Post create

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);
}

Upvotes: 1

Views: 50

Answers (2)

assem afify
assem afify

Reputation: 1

First: Make sure you have the following table in the database in order for the many to many relationship to work

table name : category_project

columns: category_id, project_id

Second: class Project needs only the following function for categories relation to work:

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

Third: class Category needs only the following function for projects relation to work:

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

Fourth: postCreate function should be as follow:

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);
}

Upvotes: 0

aimme
aimme

Reputation: 6773

use attach or sync for many to many relationships reference : 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);
}

Upvotes: 1

Related Questions