Unable to access property - Laravel - php

I'm trying to send emails in Laravel.
I have my Mailable:
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function build()
{
return $this->subject('Message from B&B Vintage Collectibles')->view('mail.direct');
}
Controller:
namespace App\Http\Controllers;
use App\Contact;
use App\Mail\DirectMailable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
class DirectController extends Controller
{
public function __invoke(Request $request)
{
$user = $request->get('email');
$message = new Contact();
$message->body = $request->get('message');
Mail::to($user)->send(new DirectMailable($message));
return response()->json('Message sent', 200);
}
}
And in direct.blade.php I have this line:
<p>{{ $message->body }}</p>
But when I send the email I get this response:
"Undefined property: Illuminate\Mail\Message::$body..."
I have other email systems setup in almost the exact same way so I can't figure out why this isn't working.
I've tried without the templates and the email sends just fine.
When I dd($message->body) before Mail::to... I get the correct string.
What's happening on the way to my blade file that won't let me access this property?

As discussed on this thread
https://laracasts.com/discuss/channels/laravel/laravel-mailable-not-working?page=1
don't use $message variable, it cause errors in email send in larvel framework.
You can follow this thread for more info.

I have used in the same way and it works. But I do something like this... Passing the actual data to the view
public function build()
{
return $this->subject('Message from B&B Vintage Collectibles')->view('mail.direct',[ 'message' => $this->message ]);
}

Related

Laravel + Pusher Expected parameter 2 to be an array, string given in live chat

I've been following this blog post by pusher in their official site for building basic chat app with laravel + pusher.
I've followed every step thoroughly according to the blog, I'd search here and there as some details were not mentioned there.
I can send the message between users (save in DB table) but not updating in realtime in users end.
The below is the error message I get when I press send message button:
Here are the snippets from my code file that I have written.
Events/MessageSent.php
use App\User;
use App\Message;
class MessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $message;
public function __construct(User $user, Message $message)
{
$this->message = $message;
$this->user = $user;
}
public function broadcastOn()
{
return new PrivateChannel('chat');
}
}
Broutes/channels.php
Broadcast::channel('chat', function ($user) {
return Auth::check();
});
web.php
Auth::routes();
Route::get('/', 'ChatsController#index');
Route::get('messages', 'ChatsController#fetchMessages');
Route::post('messages', 'ChatsController#sendMessage');
ChatsController.php
use App\Events\MessageSent;
use App\Message;
use Illuminate\Http\Request;
use Auth;
class ChatsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('chat');
}
public function fetchMessages()
{
return Message::with('user')->get();
}
public function sendMessage(Request $request)
{
$user = Auth::user();
$message = $user->messages()->create([
'message' => $request->message,
]);
broadcast(new MessageSent($user, $message))->toOthers();
return ['status' => 'Message Sent!'];
}
}
I'm using laravel version 5.5, pusher 6.1.
The blog post is written for laravel 5.4 and pusher 2.6.
Is version difference causing this issue ?
Thanks for the help.
The solution is to downgrade the pusher version.
composer require pusher/pusher-php-server ^4.1
will still be looking for the appropriate answer for latest version.

botman io + laravel not continue conversations

I am trying to start a conversation in laravel 7 and botman 2 with telegram. All works fine as but unable to continue the conversations. When I am trying to answer any previous question of the conversation it assuming to start the new conversation and not asking the 2nd question of the conversation thread.
The telegram webhook url i set is :
/api/telegram-bot
My routes/api.php
Route::post('/telegram-bot', 'TelegramController#bot');
TelegramController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Template
use App\Channel;
use Config;
use BotMan\BotMan\BotMan;
use BotMan\BotMan\BotManFactory;
use BotMan\BotMan\Drivers\DriverManager;
use App\Conversations\BankingConversation;
class TelegramController extends Controller{
public function bot(){
$config = [
"telegram" => [
"token" => "{MY_BOT_TOKEN}",
'conversation_cache_time' => 30
]
];
DriverManager::loadDriver(\BotMan\Drivers\Telegram\TelegramDriver::class);
$botman = BotManFactory::create($config);
$botman->hears('(hi|hello|start)', function (BotMan $bot) {
$bot->startConversation(new BankingConversation , \BotMan\Drivers\Telegram\TelegramDriver::class );
});
$botman->fallback(function($bot) {
$bot->reply('Sorry, I did not understand you. Just type start to continue.');
});
$botman->listen();
}
}
and finally the BankingConversation
<?php
namespace App\Conversations;
use Illuminate\Foundation\Inspiring;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Outgoing\Question;
use BotMan\BotMan\Messages\Outgoing\Actions\Button;
use BotMan\BotMan\Messages\Conversations\Conversation;
class BankingConversation extends Conversation
{
protected $fullname;
protected $email;
public function askFullname()
{
$welcome = 'Hey ';
$this->ask($welcome.' What is your name ?', function(Answer $answer) {
// Save result
$this->fullname = $answer->getText();
$this->say('Nice to meet you '.$this->fullname);
$this->askEmail();
});
}
public function askEmail()
{
$this->ask('One more thing - what is your email?', function(Answer $answer) {
// Save result
$this->email = $answer->getText();
$this->say('Great - that is all we need, '.$this->firstname);
});
}
public function run()
{
// This will be called immediately
$this->askFullname();
}
}
Whenever is am typing hi/hello/start it's asking me first question of conversation "Hey What is your name ?"
But after replying to the question it's going to fallback and returning "Sorry, I did not understand you. Just type start to continue."
What is the mistake i am doing here ?
You have not specified the cache driver.
update section of your code.
DriverManager::loadDriver(\BotMan\Drivers\Telegram\TelegramDriver::class);
$botman = BotManFactory::create($config, new \BotMan\BotMan\Cache\LaravelCache());
Botman uses cache to maintain the conversation.

How to pass data to mail view using queue?

I have my job class ProductPublish method handle() I am trying to send email.
public function handle()
{
//
Mail::to('i******o#gmail.com')->send(new SendEmail());
}
In the ProductController controller I am calling that job class as like below
ProductPublish::dispatch();
In the SendEmail class which is mailable I am trying to pass data to view as like below
public $message;
public function __construct($message)
{
$this->message = 'This is test message';
}
public function build()
{
return $this->view('email.product.product-publish')->with('message' => $this->message);
}
But it does not works. I also tried with no attaching with() method but still does getting result. In the email view I am calling data as like below
{{ $message }}
Can someone kindly guide me what can be issue that it is not working. Also I want to pass data actually from ProductController but since I am failed to pass from sendEmail that's I didn't tried yet from controller.
Kindly guide me how can I fix it.
In laravel,
The arguments passed to the dispatch method will be given to the job's constructor
So when you are calling dispatch, you can pass message :
ProductPublish::dispatch($message);
Then inside your job you can add a property message and a constructor to get it from dispatch and assign it :
private $message;
public function __construct($message)
{
$this->message = $message;
}
public function handle()
{
// Use the message using $this->messge
Mail::to('i******o#gmail.com')->send(new SendEmail($this->message));
}
Also you can directly queue emails. Check documentation
Try this:
public $message;
public function __construct($message)
{
$this->message= $message;
}
public function build()
{
// Array for passing template
$input = array(
'message' => $this->message
);
return $this->view('email.product.product-publish')
->with([
'inputs' => $input,
]);
}
Check Docs

Laravel new error- Undefined variable:donor, method 'table' not found in \Illuminate\..\DB

I'm able to get the the data for particular email after entering it. However this can only take place after getting into that page. But now, before I do that I need to enter the user to the database and send him an email. After entering the info and submitting it, I get an error that tells "undefined variable:donor". Nevertheless, the user still gets added into the database but I can't go to the next page and get an error because it also has a function together that sends an email(receipt.blade.php) which is giving the error because "donor" is undefined.
My controller has these:
use App\Donor;
use App\Video;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
The function is as
public function getUserReceipts(Request $data){
$email = $data->email;
$donor = Donor::where('email', $email)->get();
return view('emails.receipt')->with(compact('donor'));
}
The receipt.blade.php page has the function suggested in one of the answers by sandeesh
#foreach($donor as $value)
{{ $value->first_name }}
{{ $value->last_name }}
{{ $value->amount_donated }}
#endforeach
the function that sends the email is:
public function thankyoupage(Request $data, Mailer $mailer){
$donor = Donor::create([
'first_name'=> $data->first_name,
'last_name' => $data->last_name,
'email' => $data->email,
'video_count' => $video_count,
'amount_donated' => $data->amount_donated,
});
$mailer
->to($data->input('email'))
->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
($data->input('amount_donated'))));
return redirect()->action('PagesController#thank1');
}
My mail.php is as follows:
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $first_name, $last_name, $amount_donated;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($first_name,$last_name,$amount_donated)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->amount_donated = $amount_donated;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.receipt');
}
}
Add at the top
use DB;
Or
$donor = Illuminate\Support\Facades\DB::table..
Either use the alias and import it or the full path.This might also happen because your aliases in config/app.php are out of whack.
Replace use Illuminate\Support\Facades\DB; with use DB;
Here's your main problem. You return the view within the loop. This causes the view to be rendered for the first donor and the rest gets ignored. You need to pass the donors to the view and then loop in the view to display them. Do this.
Controller
public function getUserReceipts(Request $data)
{
$email = $data->email;
$donors = Donor::where('email', $email)->get();
return view('emails.receipt')->with(compact('donors'));
}
In your view
#foreach($donors as $donor)
{{ $donor->first_name }}
{{ $donor->last_name }}
{{ $donor->amount_donated }}
#endforeach

Laravel Undefined Variable: donor ErrorException in d03bd104494df21919e458a02c3243e2b323db06.php line 3:

I have a controller which mainly has two functions. First function adds the user to the database and sends an email (receipt.blade.php) to the user.The second function allows the user to get all the receipts for a particular email that a user enters.
Right now, if I directly go to the page where the user can enter the email and get all receipts, its working fine. But, if I try to do it through the process of adding a new user I get the error described in the title after I click submit to adding the user. However, it has added the user to the database, but shows the error because its sending the email which is the receipt.blade.php and has the undefined variable. My controller has these:
use App\Donor;
use App\Video;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
The first Function is:
public function thankyoupage(Request $data, Mailer $mailer){
$donor = Donor::create([
'first_name'=> $data->first_name,
'last_name' => $data->last_name,
'email' => $data->email,
'video_count' => $video_count,
'amount_donated' => $data->amount_donated,
});
$mailer
->to($data->input('email'))
->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
($data->input('amount_donated'))));
return redirect()->action('PagesController#thank1');
}
My mailing file(mailable) is:
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $first_name, $last_name, $amount_donated;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($first_name,$last_name,$amount_donated)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->amount_donated = $amount_donated;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.receipt');
}
}
The second function is:
public function getUserReceipts(Request $data){
$email = $data->email;
$donor = Donor::where('email', $email)->get();
return view('emails.receipt')->with(compact('donor'));
}
The receipt file simply contains:
#foreach($donor as $value)
{{ $value->first_name }}
{{ $value->last_name }}
{{ $value->amount_donated }}
#endforeach
The error I'm getting seems to be because donor in the receipt file is
undefined and I'm not sure how to fix it as it was passed with compact
in the second function.
Would really appreciate the help.
From your last question i can see the problem. The issue is that you're reusing the same view which takes two different set of data. When used with the method getUserReceipts, it generates receipts for multiple donors. But when you send the mail you're using the view as the mail content. In that case you need to use the properites set in the mailable class. The ideal case would be to create a new view for sending a single email receipt and handle it like so.
Change your mailable class to this. With this you can use all the properties of the donor in your view.
<?php
namespace App\Mail;
use App\Donor;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyMail extends Mailable
{
use Queueable, SerializesModels;
public $donor;
public function __construct(Donor $donor)
{
$this->donor = $donor;
}
public function build()
{
return $this->from('myemailid12341#gmail.com')
->view('emails.singlereceipt');
}
}
Change the way you send the email to
\Mail::to($data->input('email'))->send(new \App\Mail\MyMail($donor));
Create a new view singlereceipt.blade.php and use the following code instead.
{{ $donor->first_name }}
{{ $donor->last_name }}
{{ $donor->amount_donated }}
You have an error in sending data to view. If you send data using with() function than syntax should be:
return view('emails.receipt')->with('donor', $donor);
See Passing Data To Views

Categories