Bunnypants's avatar

Implode a new line in an array in laravel

I'm trying to implode a new line in between my array variables but so far nothing have worked. Does anyone know the solution to my problem? `` public function destroy($id,Request $request) { //decline student $user = User::findOrFail($id);

        $error = array('reason' => 'Reason for rejection:');

        if (isset($request['reason1'])) {

           $reason1= $request->input('reason1');
          $error = array_add($error, 'reason1', $reason1);
           }

       if (isset($request['reason2'])) {

           $reason2= $request->input('reason2');
          $error = array_add($error, 'reason2', $reason2);    
           }

         if (isset($request['reason3'])) {

           $reason3= $request->input('reason3');
          $error = array_add($error, 'reason3', $reason3);
           }

       if (isset($request['reason4'])) {

           $reason4= $request->input('reason4');
          $error = array_add($error, 'reason4', $reason4);    
           }
            if (isset($request['reason5'])) {

           $reason5= $request->input('reason5');
          $error = array_add($error, 'reason5', $reason5);
           }

       if (isset($request['reason6'])) {

           $reason6= $request->input('reason6');
          $error = array_add($error, 'reason6', $reason6);    
           }

            if (isset($request['reason7'])) {

           $reason7= $request->input('reason7');
          $error = array_add($error, 'reason7', $reason7);    
           }

           $error = implode('\n', $error);
    






     if(!$user){

        $request->session()->flash('error','You can not delete this the user');
        return redirect(route('admin.users.index'));

    }
    $url = URL::signedRoute('user.profile' , ['id'=> $id]);
    $updatedata =[

        'body'=> 'Your enrolment application  has been denied',
        'error'=> $error,
        'message'=> 'You are allowed to update your form',
        'url'=> $url,
        'thankyou'=> 'You have 3 days to update your form'

    ];

    
    Notification::send($user, new UpdateForm($updatedata));
   

    $request->session()->flash('success','You have deleted the user');
    return redirect(route('admin.users.index'));

}
0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

When using backslash n you need "" instead of '' to parse it as a newline, and not plain text

$error = implode("\n", $error);
//or
$error = implode(PHP_EOL, $error);
1 like
Sinnbeck's avatar

And you probably also want to convert them to br (aka html)

$error = nl2br(implode("\n", $error));
Snapey's avatar

no clue where you are even looking

Snapey's avatar

why newlines? not hoping to see these in the browser are you?

happiness's avatar

I'm trying a similar thing, I want to add a new line to the individual lines of the body of the mail message that is send in a notification. I've tried \n, <br>,<br /> with ' quotes, as well as " quotes and I've tried PHP_EOL. Nothing works.

$message = implode("\n",$notification->data['introLines']);

Please or to participate in this conversation.