Mujahid
Mujahid

Reputation: 1237

Many to many relationship in Laravel 5.1

In one of my current projects, I have a scenario where a company can have many tags and a tag can be belonged to many companies. To achieve this, In my model I have defined as follows

// Company model
    use Illuminate\Database\Eloquent\Model;
    
    class Company extends Model {
        public function tags(){
            return $this->belongsToMany('App\Tag')->withTimestamps();
        } }

and

// Tag model

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    public function companies(){
        return $this->belongsToMany('App\Company', 'company_tag', 'company_id', 'tag_id')->withTimestamps();   
    }
}

In my CompanyController Store function, after the company and tags are saved I have added the following line

$company->tags()->attach(1);

I have an AngularJS front end separate project and the backend as a separate project. When I added the above line in the controller, it gives me an access origin error. When I removed that, data get saved properly.

Upvotes: 0

Views: 57

Answers (1)

Zoe Blair
Zoe Blair

Reputation: 556

This sounds like an angular issue. Is your server returning the header:

Access-Control-Allow-Origin: http://yoursite.com

Found in the accepted answer on this related (duplicate?) question: Access-Control-Allow-Origin error but request goes through

Upvotes: 1

Related Questions