Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jmacdiarmid's avatar

Laravel/Eloquent MySQL relationship 1:n and 1:1 help needed

I'm building an app for a digital marketing agency to automate advertising campaign submission and tracking for clients. I'm having trouble wrapping my head around what relationships I need based on the current manual process. I'm the only developer working on this project so I feel like I'm sitting on an island out in the ocean. lol The way the process works now is we have a client prospect, a contract is created, if the prospect/client approves the process goes to the on-boarding/discovery phase followed by the campaign creation and insertion order creation.

The way I have my relationships set up so far is like this:

  1. Client table has a 1:n relationship with the contracts table.

  2. Contracts, discovery and insertion orders table has a 1:1 relationship with the campaigns table.

  3. Contracts table has an foreign key for both the client and campaigns table.

  4. Campaigns table has a foreign key for the Client table.

I created an ERD in MySQL Workbench. It shows identifying and non-identifying 1:1 and 1:n. Should I be concerned with these?

As far as the models go, I currently have:

In the Client model:

  • contracts hasMany(Contract::class)
  • campaigns hasMany(Campaigns::class)

In the Campaign model, I have:

  • client - belongsTo(Client::class)

In the Discovery and Insertion Order models:

  • campaign - belongsTo(Campaign::class)

In the Contract model:

  • client - belongsTo(Client::class)
  • campaign belongsTo(Campaign::class)

If you need additional information, please let me know.

Know of any resources, tutorials, etc that might help shed more light on this?

0 likes
11 replies
jmacdiarmid's avatar

I forgot to mention that the client table has a 1:n relationship with the campaigns table.

jlrdw's avatar

Not only the videos, but also the chapter on eloquent relationships will guide you on a one to many relation.

When I first learned, I actually worked the examples in the documentation, that helped me a lot.

If you don't want to use eloquent, I would suggest getting a good query visual designer.

Another good source of practicing queries is https://www.mysqltutorial.org/

2 likes
lemmon's avatar

Yes @jmacdiarmid, as @jlrdw says work the examples from the documentation in as seperate app using tinker to figure it out

1 like
jmacdiarmid's avatar

@lemmon Thanks for this. I'm definitely considering signing up for that. I need all the help I can get. :)

jmacdiarmid's avatar

Hi guys,

I wish it wasn't so, but I'm still having issues with this. Specifically with associating a campaign package with a contract. I have it working with associating all campaign packages that have been assigned to a campaign duration. Do I need a separate "pivot" table for this to link the campaign package to the contract?

Something like:

        Schema::create('contract_package_pivot', function (Blueprint $table) {
            $table->unsignedBigInteger('contract_id');
            $table->foreign('contract_id', 'contract_id_fk_2405103')->references('id')->on('contracts')->onDelete('cascade');
            $table->unsignedBigInteger('package_id');
            $table->foreign('package_id', 'package_id_fk_2405103')->references('id')->on('campaign_packages')->onDelete('cascade');
        });

Please or to participate in this conversation.