So, I've been trying to wrap my head around many-to-many relations. I think I've setup the database correctly and have the tables the way they should. As of now, saving works, but no relations are added, so my pivot tables are empty.
Here's a picture of my database structure.
http://i.imgur.com/2dx3j26.jpg
I'm using Laravels' built-in user auth system, obviously I've left the password table off of that pic because it's not relevant. What's important is to note that almost all relations here are many-to-many. An artist can have multiple records, and a record can have multiple, aka various Artist(s). Same for format (CD, LP or Digital). A user can make multiple "purchases", which also serves as his/her collection. Any registered user can add artists or records to the database, and then mark it purchased.
Why like this? Because, if the records were separate for each users I'd have 100 entries of the same album.
Anyway, here's some code, and what I need help with.
First, models.
Artist.php
protected $table = 'artists';
protected $fillable = [ 'name', 'country ];
public function records()
{
return $this->belongsToMany('Record');
}
Record.php
protected $table = 'records';
protected $fillable = ['title', 'year'];
public function artist()
{
return $this->belongsToMany('Artist');
}
public function user()
{
return $this->belongsToMany('User');
}
public function formats()
{
return $this->belongsToMany('Format');
}
Purchase.php
protected $table = 'purchases';
protected $fillable = [ 'price', 'bought_from ];
public function user()
{
return $this->belongsTo('User');
}
And here's record_user table
create_record_user_table.php
Schema::create('record_user', function(Blueprint $table)
{
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('record_id')->unsigned();
$table->foreign('record_id')->references('id')->on('records');
$table->integer('purchase_id')->unsigned();
$table->foreign('purchase_id')->references('id')->on('purchases');
$table->timestamps();
});
The other pivot tables work the same way (except for there being one less foreign key, of course). But I think my problem lies in the next piece of code.
I have a form for adding a new record. It has two dropdown menus, one for format and one for artist. Then plain text fields for everything else needed. I know I could probably use validator or something here, but this was just the easiest way to get it working. I figured once I get the adding working correctly I can tweak the rules later.
So what happens when I run it? Well, the record is added, and so is the purchase, but there's nothing in the pivot tables. I read up on pivot tables from three different sources, but couldn't get the hang of it. I would love to get some help with this.
I know this post is long and messy, but please, PLEASE help me with this. If there's any info missing or something, just ask and I shall deliver. Thanks in advance!
Here's the codes I tried using.
$record->artist()->save($artist);
$record->formats()->save($format);
RecordsController.php
public function confirm(PrepareRecordRequest $request)
{
$data = $request->all();
$exists = Record::where('title',$data['title'])
->where('year', $data['year'])
->first();
if ($exists)
{
flash()->error('Record already exists!');
return Redirect('records/create')->withInput();
} else {
$record = Record::create(array('title' => Input::get('title'),
'year' => Input::get('year')));
$purchase = Purchase::create(array('price' => Input::get('price'),
'bought_from' => Input::get('bought_from')));
$artist = Input::get('artist');
$format = Input::get('format');
flash()->success('Record successfully added!');
return Redirect()->action('PagesController@home');
}