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

ajsmith_codes's avatar

Syntax error or access violation: 1066 Not unique table/alias

Not sure what I'm missing here.

Many to many relationship.

Tables:

  1. orders, 2. contacts, 3. order_contacts

Models:

OrderContact Model

public function orders(){

        return $this->belongsToMany('App\Models\Order', 'order_contacts', 'order_id', 'contact_id');

    }

Order Model

   public function contact()
    {

        return $this->belongsToMany('App\Models\OrderContact', 'order_contacts', 'order_id', 'contact_id');

    }

When trying to do this in the view, I get the error:

$order->contact

Here is the full error:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'order_contacts' (SQL: select order_contacts.*, order_contacts.order_id as pivot_order_id, order_contacts.contact_id as pivot_contact_id from order_contacts inner join order_contacts on order_contacts.id = order_contacts.contact_id where order_contacts.order_id = 8 and order_contacts.deleted_at is null) (View: C:\xampp\php\base-laravel\resources\views\salesorders\show.blade.php)

0 likes
22 replies
automica's avatar

@ajsmith_codes are you sure you've got your eloquent relationships correct?

Surely:

  • Order belongsTo Contact
  • Contact hasMany Order

how would Order belongToMany Contact? Only one contact creates an order?

Also, if you are following Laravel conventions (and please do, they make life much easier), your join table would be 'contact_order'. // alphabetical singular verbs

You don't need a model for a join table.

if Order belongsToMany Contact then the relationship would be plural

  public function contacts()
    {
        return $this->belongsToMany(App\Models\Contact::class, 'contact_order', 'order_id', 'contact_id');
    }

but I don't think that you should use that relationship though.

ajsmith_codes's avatar

Hello. Each order can have many contacts. One may be a billing contact, while another could be shipping contact. Only one customer per order though.

automica's avatar

@ajsmith_codes if you order can have many contacts, then you need an additional column in your join table to specific which type of contact it is, otherwise you cant use the same contact for billing and shipping.

join fields would be contact_id, order_id. contact_type_id. You would then make your unique as a combination of the 3 fields.

you still don't need a model for your join table though.

https://laravel.com/docs/8.x/eloquent-relationships#many-to-many

you would pass in the extra field like so to save

  $result =  $order->contacts()->save($contacts, ['contact_type_id' => 'value']);
ajsmith_codes's avatar

@automica I should explain a little further. There are also Customer Contacts. I have a contacts table, a customers table, a customer_contact table (I will fix the name). Then I have the tables I listed previously. Do I stick to a Contacts model instead of breaking into two?

automica's avatar

@ajsmith_codes whats the difference between customers and contacts & where are you storing addresses? or is the contacts table actually full of addresses?

ajsmith_codes's avatar

@automica I was thinking addresses would be a separate table since more than one person can be located at the same address.

Customers are usually a company and the contacts are the customer's employees in this situation.

Each Sales Order has one Customer, but can have more than one Customer Contact. Only a Customer Contact for that Customer can be attached to the Sales Order. Does that help or make it worse? :)

automica's avatar

@ajsmith_codes I think we're getting a bit off piste here

in regards to your original question

if OrderContact belongsToMany Order

then the relationship would be

public function orders(){
        return $this->belongsToMany('App\Models\Order', 'order_contacts', 'contact_id', 'order_id');
    }

and then the reverse would apply for Order belongsToMany OrderContact

public function contacts(){
        return $this->belongsToMany('App\Models\Contact', 'order_contacts', 'order_id', 'contact_id');
    }
automica's avatar
automica
Best Answer
Level 54

@ajsmith_codes also, if your models are made up of 2 verbs can you make them 1?

  • eg OrderContact, can that just be Contact?
  • eg SalesOrder, can that just be Order?

then you can use compound names for join tables

if you are going to call the model OrderContact then reflect this in the field name eg order_contact_id. You would then have a join called order_contact_order to join order_contacts with orders.

Getting naming right is key to ease of use with Laravel. Following conventions saves code and cognitive load.

https://webdevetc.com/blog/laravel-naming-conventions/

ajsmith_codes's avatar

@automica When I use the code you gave me, it still has the same error.

In the blade, I'm using this:

@if(count($order->contacts) < 1)

And that produces the error.

ajsmith_codes's avatar

@automica I would do that, but there are many that have contacts: Vendors, Freight Companies, Customers, Orders, etc.

Regarding SalesOrder, there will also be PurchaseOrder.

automica's avatar

@ajsmith_codes

Regarding SalesOrder, there will also be PurchaseOrder.

why not have these in a single table, along with order_type_id?

Then you can have a SalesOrder model which has global scope to filter by specific order_type_id.

ajsmith_codes's avatar

@automica The columns for each will be very different. Also, each Purchase Order can be related to many Sales Orders.

automica's avatar

@ajsmith_codes ok. keep it granular then.

WRT to:

When I use the code you gave me, it still has the same error.
In the blade, I'm using this:
@if(count($order->contacts) < 1)
And that produces the error.

what method is producing that error?

ajsmith_codes's avatar

As in what is in the controller? (still learning, but not a full newbie thankfully).

I tried both ways:

    $order = Order::where('slug', $slug)
        ->with('department')
        ->with('revisions')
        ->with('contact')
        ->first();

    $order = Order::where('slug', $slug)
        ->with('department')
        ->with('revisions')
        ->first();
ajsmith_codes's avatar

I finally got it. I changed to one model: Contact instead of breaking into multiple.

automica's avatar

@ajsmith_codes your relationship is 'contact' but in your blade you are checking 'contacts'

@if(count($order->contacts) < 1)

if you have belongsToMany then relationship should be plural

ajsmith_codes's avatar

Yes, thank you. I'm not certain which reply to mark as "best answer".

1 like
automica's avatar

:P don't think it really matters so closest one or the one with naming conventions in

automica's avatar

you are welcome. sorry to zoom right in and pick at your model names. hopefully the conventions link will be helpful.

Please or to participate in this conversation.