Laravel 5.7 SendEmail is not working properly - laravel

I am sending email using laravel 5.7 but i found following error " (1/1) FatalErrorException
Class 'App\Mail\UserRequest' not found" Need help
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
//use \App\Mail\SendMail;
use App\Mail\UserRequest;
class ContactusController extends Controller
{
public function index()
{
return view('contactus');
}
public function sendmail(Request $req)
{
$msgdata = array('subject'=>$req->subject,'email'=>$req->email,
'name'=>$req->name,'body'=>$req->message);
Mail::to('dddddddd#dddsdsf.com')->send(new UserRequest($msgdata));
return view('contactus');
}
}

Related

Class "App\Http\Controllers\Auth\Mail" not found

How can I resolve this error? I am trying to customize the default email template on laravel. This is the code for the controller that sends the email.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use App\Models\User;
use Illumunate\Auth;
class EmailVerificationNotificationController extends Controller
{
public function store(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->intended(RouteServiceProvider::HOME);
}
Mail::send('email.template', $request->user(), function($mail) use($data){
$mail->to($request->user()->email, 'no-reply')->subject("Verify Email Address");
$mail->from('admin#raketlist.com','testing');
});
$request->user()->sendEmailVerificationNotification();
return back()->with('status', 'verification-link-sent');
}
}
Add use Illuminate\Support\Facades\Mail; to other uses.

Undefined variable:title

I added migrate in database, but why getting error I don't know, I am pretty new to Laravel, I don't know how can I fix this.
Error is:
Undefined variable: title
My code:
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$aboutus = new Abouts();
$aboutus->$title = $request->input('title');
$aboutus->$subtitle = $request->input('subtitle');
$aboutus->$description = $request->input('description');
$aboutus->save();
return redirect('/abouts')->with('success','nice');
}
}
you can write this way also.
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$input = $request->all();
Abouts::create($input); //here About us your model
return redirect('/abouts')->with('success','nice');
}
}

Laravel 5.3 How to use Braintree\WebhookNotification

I am using Laravel 5.3 and trying to add new webhook event handler in WebhookController
Here is my controller
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Log;
use App\Models\BraintreeMerchant;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(WebhookNotification $notification)
{
if( isset($_POST["bt_signature"]) && isset($_POST["bt_payload"]))
{
$notification = Braintree_WebhookNotification::parse($_POST["bt_signature"], $_POST["bt_payload"]);
$notification->kind == Braintree_WebhookNotification::SUB_MERCHANT_ACCOUNT_APPROVED;
// true
$notification->merchantAccount->status;
// "active"
$notification->merchantAccount->id;
// "blue_ladders_store"
$notification->merchantAccount->masterMerchantAccount->id;
// "14ladders_marketplace"
$notification->merchantAccount->masterMerchantAccount->status;
}
}
}
but getting the following error message:
BindingResolutionException in Container.php line 763:
Target [Braintree\WebhookNotification] is not instantiable.
I've found the answer. this is how to implement Braintree webhook controller.
<?php
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Illuminate\Http\Request;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(Request $request)
{
$notification = WebhookNotification::parse($request->bt_signature, $request->bt_payload);
$merchantId = $notification->merchantAccount->id;
$result_merchant_status = $notification->merchantAccount->status;
}
}

Call to undefined method Illuminate\Notifications\Notification::send()

I am trying to make a notification system in my project.
These are the steps i have done:
1-php artisan notifications:table
2-php artisan migrate
3-php artisan make:notification AddPost
In my AddPost.php file i wrote this code:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class AddPost extends Notification
{
use Queueable;
protected $post;
public function __construct(Post $post)
{
$this->post=$post;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data'=>'We have a new notification '.$this->post->title ."Added By" .auth()->user()->name
];
}
}
In my controller I am trying to save the data in a table and every thing was perfect.
This is my code in my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
//use App\Notifications\Compose;
use Illuminate\Notifications\Notification;
use DB;
use Route;
class PostNot extends Controller
{
public function index(){
$posts =DB::table('_notification')->get();
$users =DB::table('users')->get();
return view('pages.chat',compact('posts','users'));
}
public function create(){
return view('pages.chat');
}
public function store(Request $request){
$post=new Post();
//dd($request->all());
$post->title=$request->title;
$post->description=$request->description;
$post->view=0;
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
return redirect()->route('chat');
}
}
Everything was good until I changed this code:
$post->save();
to this :
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
It started to show an error which is:
FatalThrowableError in PostNot.php line 41: Call to undefined method
Illuminate\Notifications\Notification::send()
How can i fix this one please??
Thanks.
Instead of:
use Illuminate\Notifications\Notification;
you should use
use Notification;
Now you are using Illuminate\Notifications\Notification and it doesn't have send method and Notification facade uses Illuminate\Notifications\ChannelManager which has send method.
Using this
use Illuminate\Support\Facades\Notification;
instead of this
use Illuminate\Notifications\Notification;
solved the problem for me.
Hope this helps someone.
using this is better
use Notification
Instead of
use Illuminate\Support\Facades\Notification
this makes the send() not accessible [#Notification Databse]

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model

I am pretty new to Laravel and I am trying to add the post, create by a user into the database. But when I do so, following error comes:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save()
must be an
instance of Illuminate\Database\Eloquent\Model, string given,
called in C:\xampp\htdocs\lar\app\Http\Controllers\PostController.php on line
25 and defined
User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('App\Post');
}
}
Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User') ;
}
}
PostController:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
$post->$request['body'];
$request->user()->posts()->save('$post');
return redirect()->route('dashboard');
}
}
Post Route:
Route::post('/createpost',[
'uses' => 'PostController#postCreatePost',
'as'=>'post.create'
]);
Form action:
<form action="{{route('post.create')}}" method="post">
Please tell me how to fix this.. How to fix this?
Thank you in advance.. :)
I think what you want is this:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
// here you set the body of the post like that
$post->body = $request->body;
// here you pass the $post object not as string
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
You need to pass the $post object as an object to the save method. You was doing this: $user->posts()->save('$post') when you need to do this: $user->posts()->save($post).
Hope it helps.

Resources