Support the ongoing development of Laravel.io →
Database Eloquent Forms
Last updated 2 years ago.
0

It only inputs credit_id, because your SQL query only specifies (credit_id). I guess you want to add other fields to each version_author? If so, you must not directly call a DB::insert(), as that just inserts a new row into your database with just one field (the credit_id) filled out.

How are you saving which credit_id belongs to which author? Do you have a many-to-many relationship?

Basically what you probably need is an Author model and a Credit model and have them connected through a relationship. You can then add as many credit_ids to each author as you like. However, you question above does not give enough details to correctly answer this.

A quickfix is maybe to add the author_id to the SQL query:

DB::insert('INSERT INTO version_authors (credit_id, author_id) VALUES (?, ?)', array($credit_id, $author->id));

If you have the models already and have a relationship defined, you are able to do this without the foreach-loop:

$author->credits()->sync($credits);
Last updated 2 years ago.
0

I need to input:

credit_id (which is the relationship)
role_id
biography_id
bio_name_id
created_by (these are the same and do not change line by line)
updated_by (these are the same and do not change line by line)

In addition created_at and updated_at need to be set (which doesn't happen with the above).

I've already got Credit and Author models but a tutorial I found only recommended doing it as a DB::insert.

Last updated 10 years ago.
0

So is version_authors a pivot table or a model table? And are authors and credits in a many-to-many relationship?

Last updated 10 years ago.
0

It's a pivot table. Both are in many-to-many relationships. They'll share similar data but remain separate.

0

Ooookeeyy... basically what you need is to define your relationship like so in the Authors model:

public function credits()
{
    return $this->belongsToMany('Credit', 'version_authors')->withTimestamps();
}

Then, in the controller create the pivot table entry with

$author->credits()->attach($credits, array(
    'role_id' => Input::get('role_id'),
    'biography_id' => Input::get('biography_id'),
    ...,
    'created_by' => Auth::user()->id,
    'updated_by' => Auth::user()->id,
));

Because the relationship is defined with ->withTimestamps(), this automatically fills in the created_at and updated_at in the pivot table.

But this requires that the pivot table also has an author_id column!

Last updated 10 years ago.
0

But the question is, how do I insert multiple values? There isn't really a relationship between these two tables. The data is being copied and will be changed independently.

Right now, the code in the original post inserts credit_id. I need it to do all of them. At this point, there is no role_id input, etc.

So I need to know how this data can be inserted into a different table.

0

I think I don't quite get it yet. Can you post your model schema? Like so:

table posts:
field_1
field_2
field_3
...

table version_authors:
credit_id
field_2
...

for all the tables that are relevant here?

Last updated 10 years ago.
0

It's really not a table issue. I also tried this:

	    @foreach($version->of_show->has_credits as $credit)
    	
			<li><label for="{{ $credit->credit_id }}">
				<input type="checkbox" name="credit[{{ $credit->credit_id }}]" id="{{ $credit->credit_id }}" value="{{ $credit->credit_id }}"> <span>{{ $credit->display_name }} ({{ $credit->role_name }})</span>
				
				<input type="hidden" name="role_id[{{ $credit->role_id }}]" id="{{ $credit->role_id }}" value="{{ $credit->role_id }}">
				<input type="hidden" name="bio_name_id[{{ $credit->bio_name_id }}]" id="{{ $credit->bio_name_id }}" value="{{ $credit->bio_name_id }}">
				<input type="hidden" name="biography_id[{{ $credit->biography_id }}]" id="{{ $credit->biography_id }}" value="{{ $credit->biography_id }}">
			</label></li>
    	
    	@endforeach

With this being the controller:

	$credits = Input::get('credit');
	$role_id = Input::get('role_id');
	$biography_id = Input::get('biography_id');
	$bio_name_id = Input::get('bio_name_id');
	$version_id = Input::get('version_id');
	
	foreach ($credits as $credit_id)
	{
        DB::insert('INSERT INTO version_authors (credit_id, version_id, role_id, biography_id, bio_name_id) VALUES (?,?)', array($credit_id, $version_id, $role_id, $biography_id, $bio_name_id));
    }

It gave me the error:

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

So, as you can see. It's trying to get multiple columns copied instead of just one.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jerauf jerauf Joined 16 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.