OK so I just read the docs for accessors and mutators and then watched a video on youtube about it and its not quite what im looking for.
I guess simplifying things isnt working so I'm gonna show entire code blocks now to properly illustrate whats going on and what I need:
function updateProfile(Request $request, SpokenLanguages $spokenLanguages, WorkingHours $workingHours, JobType $jobType, $id){
$user = User::find($id);
//Languages
$setLang = $user->with('spokenLanguages')->find($id)->spokenLanguages; //Find already set values
$spokenLangInput = $request->languages; //Get user input
$spokenLangInput = explode(",",$spokenLangInput); //create array from input
//Remove whitespace from all of the items in array and create new array
$spokenLangInput = removeWhiteSpace($spokenLangInput);
//Function in helpers.php please see this file for further commentary
insertMultiple($setLang,$spokenLangInput,$id,$spokenLanguages,'languages');
//Working Hours
$setHours = $user->with('workingHours')->find($id)->workingHours; //Already set
$hoursInput = $request->preferred_hours; //Input from user
insertMultiple($setHours,$hoursInput,$id,$workingHours,'hours');
//Job type
$jobsInput = $request->job_type; //Jobs Input
$setJobs = $user->with('jobType')->find($id)->jobType; //Already Set values
//Gotta check if the input is greater than 3 and return to the page with an error message if not
$jobsInputCount = 0;
$moreThanThreeJobs = "false";
foreach($jobsInput as $job){
//First check the amount selected, if more than 3 return to the same page with error message else continue
if($jobsInputCount >= 3){
//Mostly copied from function above as these vars are called on editProfile
$setLanguages = $user->spokenLanguages->toArray();
$setLanguages = arrayToString($setLanguages);
$moreThanThreeJobs = "true";
$user->workingHours;
$user->jobType;
$eNoFile = false;
//Adding this so that values that were input are put back.
//Unfortunately it does not work...trips up when workingHours or jobType is required
//Gonna figure it out but not now haha
//$user = $request;
return view('auth.editProfile', compact('eNoFile','moreThanThreeJobs','setLanguages','user'));
}
$jobsInputCount++;
}
//If Jobs input is less than 3 then continue
if($jobsInputCount <= 3){
insertMultiple($setJobs,$jobsInput,$id,$jobType,'job');
}else{
dd('Input count = '.$jobsInputCount);
}
$user->update($request->all());
return view('auth.profile',compact('user'));
}
This is the mega function that started it all...you can ignore most of it but about half way through theres a foreach counting if theres more than 3 selected options and then returns to the edit profile page with an error. Then theres this function:
function updateProfilePic(Request $request, $id){
$user = User::find($id);
if($request->hasFile('profile_pic')){
$profilePic = $request->file('profile_pic');
$filename = $user->first_name.'-'.$user->$id.time().'.'.$profilePic->getClientOriginalExtension();
$rebuild = Image::make($profilePic);
$rebuild->save(public_path('/uploads/profile-pics/'.$filename));
$user->profile_picture = $filename;
$user->save();
}else{
$eNoFile = true;
//Mostly copied from function above as these vars are called on editProfile
$setLanguages = $user->spokenLanguages->toArray();
$setLanguages = arrayToString($setLanguages);
$moreThanThreeJobs = "true";
$user->workingHours;
$user->jobType;
return view('auth.editProfile',compact('eNoFile','moreThanThreeJobs','setLanguages','user'));
}
return view('auth.profile',compact('user'));
}
Again this is checking if anything is wrong and redirect to the edit profile page if it is but now I have to pass in the extra variables that i created in the previous function so that the view in laravel is happy.
And finally:
function editProfile($id){
$user = User::find($id);
$user->jobType;
$user->workingHours;
$setLanguages = $user->spokenLanguages->toArray();
$setLanguages = arrayToString($setLanguages);
$moreThanThreeJobs = "false";
$eNoFile = false;
return view('auth.editProfile', compact('eNoFile','moreThanThreeJobs','setLanguages','user'));
}
This is the main function that loads the edit profile page and all I really wanna do here is convert the languages array to a string and send that back but I have to re-declare all of the variables and pass them through otherwise it breaks...
I hope that clarifies things...
Matt