0
votes

I am doing this query

$disposes = Disposecollect::create([
    'bag_id' => $request->input('bag_id'),
    'station_id' => $station->id,
    'undisposed_weight' => $weight->weight,
    'disposed_weight' => $weight->weight - $weight->weight,
    'status' => $request->input('status'),
    'campus_id' => Auth::user()->campus_id 
]); 

And it suddenly it produce an error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (ecoteneo.disposecollects, CONSTRAINT disposecollects_campus_id_foreign FOREIGN KEY (campus_id) REFERENCES campuses (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into disposecollects (bag_id, station_id, undisposed_weight, disposed_weight, status, updated_at, created_at) values (6, 1, 5, 0, Undisposed, 2018-09-25 10:02:23, 2018-09-25 10:02:23))

The value of campus id from the user didn't pass in an insert query...

Is it my code in the part of:

'campus_id' => Auth::user()->campus_id

is this correct?

1

1 Answers

1
votes

As you can see in

(SQL: insert into disposecollects (bag_id, station_id, undisposed_weight, disposed_weight, status, updated_at, created_at) values (6, 1, 5, 0, Undisposed, 2018-09-25 10:02:23, 2018-09-25 10:02:23))

There is no campus_id field. In Laravel, this usually means that your campus_id value is null or empty. I'd suggest you try to dump and die (dd) Auth::user()->campus_idso you're able to verify it's value.

dd means 'Dump and Die', thus meaning that Laravel will output the variable name and won't proceed to any other part of the code. If you call dd(Auth::user()) you'll be able to see all Auth::user() attributes. You could also call dd(Auth::user()->campus_id), but I'd suggest you trying to output the most generalized (and still useful) part of the code, as that guarantees a broader vision of the problem.