This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

billyZduke
billyZduke

I know there are some similar threads about this already, but I've been through every one I could find and I still don't have enough or the right info to make it work in my own custom plugin. I've got Company and Project models. Companies have many Projects, Projects belongTo one Company. With the Builder plugin's assistance, I have the Project portion of my backend working perfectly. You can select from a list of Companies in the relation dropdown/widget and it updates the db. But then, around 12 hours ago now, I had to get curious: Would I be able to update the company_id field in the Project table if I selected a Project while adding or updating a Company record?

It sure seems like this is possible, but I feel like I'm missing pieces... What I know I have:

In my Company model:

    public $hasMany = [
        'projects' => ['QSP\Clients\Models\Project', 'table' => 'qsp_clients_projects' ],
    ];

In my Project model:

    public $belongsTo = [
        'company' => ['QSP\Clients\Models\Company'],
    ];

In my Plugin.php:

    public function boot()
    {
        Company::saved(function($company) {
                $company->projects()->saveMany($company->projects);
        });

I am pretty certain that last one isn't quite right, and it's where I'm stuck. First of all, I've tried both saved() and saving(), and I'm not getting any value coming through for $company->projects, no matter how many times I check the Projects checkbox on the Company form before I submit. Dumping the method gives me an empty object, dumping it without the parentheses gives me this:

object(Illuminate\Database\Eloquent\Collection)#564 (1) { ["items":protected]=> array(0) { } }

So I'm not sure if I've screwed up my YAMLs or what. I feel like I'm missing only a tiny piece of the puzzle now, so if anyone know what it is, I'd be most grateful to share in that knowledge.

1-1 of 1

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.