Laravel Mail::send() illegal offet 'name' - php

I'm sure there is a simple solution, but I am having trouble with an error for "illegal string offset" when processing form input for use with the Laravel Mail::send().
This is the code which processes the form input, as it has to be an array.
$msg = array(
'name'=>Input::get('name'),
'email'=>Input::get('email'),
'message'=>Input::get('message')
);
Then I try to send it through Mail::send().
Mail::send('emails.question', $msg, function($message) {
$message->to('myPersonalEmail#domain.com')->subject('Email from your website!');
});
The app/views/emails/question.blade.php template is extremely simple.
{{ $msg['name'] }} <br/>
{{ $msg['email'] }} <br/>
{{ $msg['message'] }}
Yet I still get the following error.
ErrorException (E_UNKNOWN)
Illegal string offset 'name' (View:
[intentionally omitted for privacy]/app/views/emails/question.blade.php)
I believe the error is referring to the $msg['name'] not being present, but if I return the view instead I do not receive an error at all.
return View::make('emails.questions')->with('msg',$msg);
What am I missing?

You have passed $msg as an associative array, so the items of $msg array are available to the view by $key. That's mean in your case like $name, $email and so on. The app/views/emails/question.blade.php now will be like this
{{ $name }} <br/>
{{ $email }} <br/>
{{ $message }}

Related

Laravel 5.6 - Grab message from Controller into view

I'm starting with Laravel and i need to show the output of a post request into the view. My controller file returns an array with a message, like this:
return redirect('/myroute')
->with('message', [
'type' => 'success',
'text' => 'It works'
]);
In my view file, i'm trying to grab the message text, but no success. See my code below
#if(Session::has('message'))
{{ $msg = Session::get('message') }}
<h4>{{ $msg->text }}</h4>
#endif
The point is: The condition works, if i changed the {{$msg->text}} to any text it works, but when i try to get the message text, it returns an error:
htmlspecialchars() expects parameter 1 to be string, array given
So, any help is apreciated. If more information is needed, just ask.
PS: i checked this question, but no success at all
EDITED:
PS2: Can't change controller structure
Try accessing the array as follows:
<h4>{{ $msg['text'] }}</h4>
or just pass an array with the items
->with([
'type' => 'success',
'text' => 'It works'
]);
//in the view
#if(session()->has('text'))
<h4> {{ session('text') }} </h4>
#endif
-- EDIT
iterate than over the session like so:
#foreach (Session::get('message') as $msg)
{{$msg['text']}}
#endforeach
you can read more about that here
Do this instead
return redirect('/myroute')->with('success','It worked');
Then on your view
{{session('success')}}

Echo value in Laravel Blade

How can I get the value 'name' under 'rekenplichtige' in my Blade template? If I do this: {{ $vestiging->rekenplichtige->name }} I get the following error: ErrorException Trying to get property of non-object
That syntax is working to get the 'naam' under 'gemeente'. I have no idea about what's going wrong.
{{ $vestiging->rekenplichtige}}
since "name" inside an array, you should array notation to get name. Like below
{{ $vestiging->rekenplichtige['name'] }}
{{ $vestiging->rekenplichtige()->first()->name }}
This is working.

laravel undefined index error while retrieving notification data from database

I have created notification to store order submit event as
return [
'cart_id'=>$this->cart->id,
'text'=>'order is submitted'
];
when i retrieve notifications data from database as
#foreach (Auth::user()->unreadNotifications as $notification )
{{$notification->data['text'] }}
#endforeach
this gives error as undefined index text
but if i try to access text via
#foreach ($notification->data as $key => $data)
{{$data}}
#endforeach
it works
but why cant i can access data via $notification->data['text']
You should get the text attribute directly like :
#foreach (Auth::user()->unreadNotifications as $notification )
{{ $notification->text }}
//Or use
{{ $notification['text'] }}
#endforeach
Hope this helps.

Laravel 4 PHP: Editing a form with 'unchecked' checkboxes produces an 'undefined index' error

I have an edit form containing several checkboxes. When I try to update the form with any of the checkboxes left 'unchecked', I get an "undefined index" error for that particular checkbox. When I initially store new data, 'unchecked' checkboxes are stored just fine. It's only an issue if I try to edit data and leave checkboxes 'unchecked'.
I've tried using the '{{Form::hidden(fieldname, 0) }}' method but it hasn't been working for me.
edit-album.blade.php(View):
{{ Form::model($album, array('method' => 'PUT', 'route' => array('edit_album2', $album->album_id))) }}
<div class ="form-group">
{{ Form::checkbox('album_application_kitchen', 'Kitchen') }}
{{ Form::label('album_application_kitchen', 'Kitchen') }}
{{ Form::checkbox('album_application_bathroom', 'Bathroom') }}
{{ Form::label('album_application_bathroom', 'Bathroom') }}<br />
</div>
{{ Form::close() }}
EditAlbumsController2.php(Controller):
public function update($id) {
$input = \Input::all();
$validation = new Validators\Album($input);
if ($validation->passes())
{
$album = Album::find($id);
$album->album_application_kitchen = $input['album_application_kitchen'];
$album->album_application_bathroom = $input['album_application_bathroom'];
$album->touch();
$album->save();
return \Redirect::route('gallery.album.show', array('id' => $id));
}
else
{
/* Code for when validation fails */
}
}
Is there a special trick to solve this problem or am I just not using the {{Form::hidden()}} structure properly?
In your controller, when you assign Input::all() to $input, no element gets added to the $input array for unchecked checkboxes, because they don't exist in the Input::all() array (unchecked checkboxes don't get passed in POST.) When updating, use Input::get() instead, which will return null if there is no value for an input, as is the case with any unchecked checkboxes:
$album->album_application_kitchen = Input::get('album_application_kitchen');
$album->album_application_bathroom = Input::get('album_application_bathroom');
Besides, $input = Input::all() is redundant anyway, since Input::all() is already an array. Just pass Input::all() to your validator.
I've had the same problem. I solved it with using the isset function.
if(isset($data['checkbox']))

Phalcon Undefined variable

I am new to phalcon framework. I have set the variable email in my TblUserController's edit action as
$tbl_user = TblUser::findFirstByuser_id($user_id);
$this->view->setVar("email", $tbl_user->email);
and in tbl_user/edit.volt I write like:
{{ tbl_user.email }}
but it gives error
Undefined variable: tbl_user
I'm not familiar with Phalcon but I think you need to do:
$this->view->setVar("email", $tbl_user->email);
And use:
{{ email }}
Or
$this->view->setVar("tbl_user", $tbl_user);
And use:
{{ tbl_user.email }}

Categories