What format is the data that is returned with $subjects = Input::get('subjects'); ?
When you do the foreach($subjects as $sub_id)... $subject->subject_id = $sub_id; I'm curious to confirm that is providing the correct data.
Also you might try using DB::getQueryLog() to verify what data is being passed to the query builder.
It returning an array data.because i have my input names as subject[].so it saves all value of the inputs as array.and when i dd(Input::get('subjects')) i get all expected data as array.
A few suggestion,
if (!$subject->save()) {
dd("Failed to save!");
}
Or a try,... catch around the foreach...
I don't see anything in what you have provided, so it has to be something wrong with the saving or something related
Are you doing anything special in the PivotSubject model?
Only other thing i can think of is a dd ( DB::getQueryLog() ); after the foreach, to look at what is being send to the database.
Maybe it is a failed relationship, confirm the user_id and class_id exist?
Hope that helps
Yeah Thanks.Am proceeding.If the subject_user table is empty all the data in the array is been inserted.Afterwards if i try to insert another array of data only one data in the array is inserted.And when i dd ( DB::getQueryLog() ); I get this:
array (size=5)
0 =>
array (size=3)
'query' => string 'select * from users
where users
.id
= ? limit 1' (length=52)
'bindings' =>
array (size=1)
0 => int 1
'time' => float 2.99
1 =>
array (size=3)
'query' => string 'select roles
., assigned_roles
.user_id
as pivot_user_id
, assigned_roles
.role_id
as pivot_role_id
from roles
inner join assigned_roles
on roles
.id
= assigned_roles
.role_id
where assigned_roles
.user_id
= ?' (length=233)
'bindings' =>
array (size=1)
0 => int 1
'time' => float 0.99
2 =>
array (size=3)
'query' => string 'select permissions
., permission_role
.role_id
as pivot_role_id
, permission_role
.permission_id
as pivot_permission_id
from permissions
inner join permission_role
on permissions
.id
= permission_role
.permission_id
where permission_role
.role_id
= ?' (length=274)
'bindings' =>
array (size=1)
0 => int 1
'time' => float 2.3
3 =>
array (size=3)
'query' => string 'select count(*) as aggregate from subjects
where subject
= ?' (length=64)
'bindings' =>
array (size=1)
0 => string 'Current Affairs' (length=15)
'time' => float 0.71
4 =>
array (size=3)
'query' => string 'insert into subjects
(class_id
, subject
, subject_slug
, updated_at
, created_at
) values (?, ?, ?, ?, ?)' (length=113)
'bindings' =>
array (size=5)
0 => string '1' (length=1)
1 => string 'Current Affairs' (length=15)
2 => string 'current-affairs' (length=15)
3 => string '2014-11-25 14:59:26' (length=19)
4 => string '2014-11-25 14:59:26' (length=19)
'time' => float 74.48
My array consist of two values ['current affairs','civic education].but above i see one,i now strongly believe its something with my relationship,because it insert all data in array if table is empty but on insert one afterward.Please what do you think might be the possible problem and solution.
I'm not being able to match the queries to the code listed.
$registration = RegistrationCheck::where('class_id',$class_id)->where('user_id',$id)->pluck('status');
maybe this query => 'select * from users where users.id = 1 limit 1'
I'm not sure where these three occur in the code.
'query' => 'select roles.*, assigned_roles.user_id as pivot_user_id, assigned_roles.role_id as pivot_role_id from roles inner join assigned_roles on roles.id = assigned_roles.role_id where assigned_roles.user_id = 1'
'query' => 'select permissions.*, permission_role.role_id as pivot_role_id, permission_role.permission_id as pivot_permission_id from permissions inner join permission_role on permissions.id = permission_role.permission_id where permission_role.role_id = 1'
'query' => 'select count(*) as aggregate from subjects where subject = "Current Affairs"'
This is the PivotSubject model ?
'query' => string 'insert into subjects (class_id, subject, subject_slug, updated_at, created_at) values (?, ?, ?, ?, ?)' (length=113)
'bindings' => array (size=5)
0 => string '1' (length=1)
1 => string 'Current Affairs' (length=15)
2 => string 'current-affairs' (length=15)
3 => string '2014-11-25 14:59:26' (length=19)
4 => string '2014-11-25 14:59:26' (length=19)
but it doesn't match ...
$subject = new PivotSubject;
$subject->user_id = $id;
$subject->subject_id = $sub_id;
$subject->class_id = $class_id;
$subject->save();
I'm not sure where you put the dd ( DB::getQueryLog() );
You might try,
foreach($subjects as $sub_id)
{
$subject = new PivotSubject;
// i'm guessing these values should exist, if there is a db relation tie
$subject->user_id = $id;
$subject->subject_id = $sub_id;
$subject->class_id = $class_id;
// maybe var_dump the data, verify they are the right data
var_dump("user_id: $id, subject_id: $sub_id, class_id: $class_id");
if (!$subject->save()) {
// die if it fails to save for some reason
dd ( DB::getQueryLog() );
}
} // end foreach
// die here to verify the queries
dd ( DB::getQueryLog() );
Also the $subject->subject_id = $sub_id;
If your array is ['current affairs','civic education], then the $sub_id is a string? Most relationships are integers though not impossible to use a sting but just checking to make sure.
I'm not sure what else to offer as suggestions.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community