How to send data to view on send email in Laravel - laravel

i try this but it show Undefinded data
public function forgotPassword(Request $req){
$token = rand();
$data = $token;
Mail::to($req->email)->send(new sendPass($data));
}

First up, that code is a little bit inefficient. Why write :
$token = rand();
$data = $token;
Mail::to($req->email)->send(new sendPass($data));
when you could just write :
Mail::to($req->email)->send(new sendPass(rand()));
The issue is almost certainly because you've not declared $data in your SendPass class as a private variable :
class SendPass extends Mailable {
use Queueable, SerializesModels;
private $data;
public function __construct(string $data)
{
$this->data = $data;
}
public function build()
{
$data = $this->$data;
... rest of your code goes here.
}

Related

How Can I Integrate Shippo API With Laravel 8

I'm building an ecommerce website with Laravel 8, and I'm trying to incorporate real time shipping rates with Shippo API. I was able to install Shippo via composer, but I get an error saying too few arguements or undefined variable. I'm borrowing some code from article regarding Shippo, but I keep getting the errors. Are you having a similar problem? Did you solve it? Any help is appreciated. Here is my controller code,
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ShipDivision;
use App\Models\ShipDistrict;
use App\Models\ShipState;
use App\Models\Product;
use App\Services\Shipping_New;
use App\Models\User;
use Auth;
use\App\Http\Controllers\Frontend\CartController;
use Gloudemans\Shoppingcart\Facades\Cart;
use Carbon\Carbon;
class CheckoutController extends Controller
{
/**
* #var Shipping
*/
private $shipping;
/**
* #var UserRepository
*/
private $user;
/**
* CheckoutController constructor.
* #param UserRepository $user
* #param Shipping $shipping
*/
public function __construct(User $user, Shipping_New $shipping)
{
$this->middleware('auth');
$this->shipping = $shipping;
$this->user = $user;
}
public function DistrictGetAjax($division_id) {
$ship = ShipDistrict::where('division_id',$division_id)->orderBy('district_name', 'ASC')->get();
return json_encode($ship);
}
public function StateGetAjax($district_id) {
$ship = ShipState::where('district_id',$district_id)->orderBy('state_name', 'ASC')->get();
return json_encode($ship);
}
public function CheckoutStore(Request $request) {
$data = array();
$data['name'] = $request->name;
$data['shipping_last_name'] = $request->shipping_last_name;
$data['email'] = $request->email;
$data['phone'] = $request->phone;
$data['zip'] = $request->zip;
$data['street1'] = $request->street1;
$data['company'] = $request->company;
$data['city'] = $request->city;
$data['country'] = $request->country;
$data['notes'] = $request->notes;
$data['state_id'] = $request->state_id;
$data['district_id'] = $request->district_id;
$data['division_id'] = $request->division_id;
$cartTotal = Cart::total();
// The rates is a complete object but for this we
// only need the rates_list items and will pass that.
if ($request->payment_method == 'stripe') {
return view('online-boutique-stores.payment.stripe', compact('data','cartTotal'));
}
elseif ($request->payment_method == 'paypal') {
$carts = Cart::content();
$cartQty = Cart::count();
$cartTotal = Cart::total();
return view('online-boutique-stores.payment.paypal', compact('data', 'cartTotal'));
}
else {
return 'cash';
}
}
}
Remove this your construct arguments. Because you don't see any use in these arguments. You can use this way ?
public function __construct()
{
$this->middleware('auth');
}

Laravel generates error while sending lists of posts to users

RegistrationController.php
use App\User;
use App\Post;
use App\Notifications\LatestPosts;
use App\Notifications\WelcomeEmail:
public function store()
{
auth()->login($user);
$allUsers = User::latest()->get();
$posts = Post::latest()->get();
$user->notify(new WelcomeEmail($user));
$allUsers->notify(new LatestPosts($posts));
return redirect(‘/dashboard’);
}
WelcomeEmail.php
use App\User;
class WelcomeEmail extends Notification
{
use Queueable:
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function toMail($notifiable)
{
$user = $this->user;
return (new MailMessage)
->subject(‘Thanks for registering’)
->markdown(‘emails.newusers.welcome’, compact(‘user’));
}
}
LatestPosts.php
use App\Post;
class LatestPosts extends Notification
{
use Queueable;
public $posts;
public function __construct(Post $posts)
{
$this->posts = $posts;
}
public function toMail($notifiable)
{
$posts = $this->posts;
return (new MailMessage)
->subject(‘Latest posts for you’)
->markdown(‘emails.posts.latestposts’, compact(‘posts’));
}
}
New users register successfully, welcome email is sent successfully but it gives me this error for sending latest posts to users.
Argument 1 passed to App\Notifications\LatestPosts::__construct() must be an instance of App\Post, instance of Illuminate\Database\Eloquent\Collection given
Basically, I want to send a list of posts to all users (I know it’s not efficient to send it while new users register but just want to see how it will work out even if I send it while new users register) Someone please help me out in this. What do I do? Thanks in advance.
In registration controller
use App\User;
use App\Post;
use App\Notifications\LatestPosts;
use App\Notifications\WelcomeEmail:
public function store()
{
auth()->login($user);
$allUsers = User::latest()->get();
$posts = Post::latest()->get();
$user->notify(new WelcomeEmail($user));
foreach($allUsers as $u){
$u->notify(new LatestPosts($posts));
}
return redirect(‘/dashboard’);
}
LatestPost
use App\Post;
use Illuminate\Database\Eloquent\Collection;
class LatestPosts extends Notification
{
use Queueable;
public $posts;
public function __construct(Collection $posts)
{
$this->posts = $posts;
}
public function toMail($notifiable)
{
$posts = $this->posts;
return (new MailMessage)
->subject(‘Latest posts for you’)
->markdown(‘emails.posts.latestposts’, compact(‘posts’));
}
}
You should change the signature of your constructor:
use App\Post;
use Illuminate\Database\Eloquent\Collection;
class LatestPosts extends Notification
{
use Queueable;
public $posts;
public function __construct(Collection $posts) // use `Collection`, not `Post`
{
$this->posts = $posts;
}
public function toMail($notifiable)
{
$posts = $this->posts;
return (new MailMessage)
->subject('Latest posts for you')
->markdown('emails.posts.latestposts', compact('posts'));
}
}

Larave 6 l “Creating default object from empty value”

Here, I have setuo CRUD table with laravel, vuetify and vue . I could successfull create and read data from the database. But, for some reason my update and delete are not working. I am getting error like:
{message: "Creating default object from empty value", exception: "ErrorException",…}
exception: "ErrorException"
file: "C:\WinNMP\WWW\chillibiz\app\Sys\Http\Controllers\StageController.php"
line: 53
message: "Creating default object from empty value"
trace: [{file: "C:\WinNMP\WWW\chillibiz\app\Sys\Http\Controllers\StageController.php", line: 53,…},…]
My code are here:
StageController.php
<?php
namespace App\Sys\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Sys\Model\Stage;
class StageController extends Controller
{
public function index(Request $request)
{
$per_page = $request->per_page ? $request->per_page : 5;
$sort_by = $request->sort_by;
$order_by = $request->order_by;
return response()->json(['stages' => Stage::orderBy($sort_by, $order_by)->paginate($per_page)],200);
}
public function store(Request $request)
{
$uuid = Str::uuid()->toString();
$stage= Stage::create([
'id' => $uuid,
'code' =>$request->code,
'name' =>$request->name,
'description' =>$request->description,
]);
return response()->json(['stage'=>$stage],200);
}
public function show($id)
{
$stages = Stage::where('code','LIKE', "%$id%")->orWhere('name','LIKE', "%$id%")->orWhere('description', 'LIKE', "%$id%")->paginate();
return response()->json(['stages' => $stages],200);
}
public function update(Request $request, $id)
{
$stage = Stage::find($id);
$stage->code = $request->code; //line 53
$stage->name = $request->name;
$stage->description = $request->description;
$stage->save();
return response()->json(['stage'=>$stage], 200);
}
public function destroy($id)
{
$stage = Stage::where('id', $id)->delete();
return response()->json(['stage'=> $stage],200);
}
public function deleteAll(Request $request){
Stage::whereIn('id', $request->stages)->delete();
return response()->json(['message', 'Records Deleted Successfully'], 200);
}
}
Stage.php
<?php
namespace App\Sys\Model;
use Illuminate\Database\Eloquent\Model;
class Stage extends Model
{
protected $guarded = [];
}
I just found they you are using uuid as id not increment. that why you get error like that:
to solve your problem you need to add the field to your model;
<?php
namespace App\Sys\Model;
use Illuminate\Database\Eloquent\Model;
class Stage extends Model
{
public $incrementing = false;
protected $keyType = 'string';
protected $guarded = [];
}
I hope this time you can solve your problem. happy coding.
Edit you can read docs for more info

Laravel queue to store data

I wanna try to store data into database via database queue Laravel.
But I always get this error "Undefined offset: 0"
this is my controller :
public function store(Request $request)
{
$order = new Order;
$order->code = $request->code;
$order->created_at = $request->created_at;
$this->dispatch(new SalesOrder($order));
}
and this is my SalesOrder Jobs :
protected $order;
public function __construct(Order $order)
{
$this->order= $order;
}
public function handle()
{
$this->order->save();
}
is there something wrong in my code? Please somebody help me fix this issue. Than's anyway.
Instead of pass Order object pass in job order data, And the in job save order.
Controller code.
public function store(Request $request)
{
$data['code'] = $request->code;
$data['created_at'] = $request->created_at;
$this->dispatch(new SalesOrder($data));
}
Job code
protected $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function handle(Order $order)
{
if (!$order->craete($this->data)) {
// when not saved try again
$this->release();
}
return true;
}
Try running
$order = new Order;
$order->code = $request->code;
$order->created_at = $request->created_at;
inside your job so it looks like this
protected $order;
protected $request
public function __construct($request)
{
$this->order = new Order;
$this->request = $request;
}
public function handle()
{
$this->order->code = $request->code;
$this->order->created_at = $request->created_at;
$this->order->save();
}
and in your controller
public function store(Request $request)
{
$this->dispatch(new SalesOrder($request));
}
This should work. Did not test this am writing on my phone.

pass data to laravel markdown with events

Im using events to trigger an event to send emails to the user, but im getting an error when i tried to pass extra data.
Order.php
protected $events = [
'created' => Events\NewOrder::class
];
NewOrder.php
use App\Order;
class NewOrder
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
the Listener:
public function handle(NewOrder $event)
{
Mail::to(Auth::user()->email)->send( new UserOrder( $event->pedido));
}
UserOrder.php
public function __construct(Order $order)
{
$this->order = $order;
$id_order = $order->id;
$details = DB::table('order_product')
->where('order_id', '=', $id_order)
->get();
$this->$details = $details;
}
public function build()
{
return $this->markdown('emails.userorder')->with('details', $this->details);
}
Markdown mail: userorder
#foreach($details as $detail)
{{ $detail->description }}
#endforeach
gave me this error:
Invalid argument supplied for foreach()
Change
$this->$details = $details;
to
$this->details = $details;
Also be sure that variable is declared above the __construct method, like so
public $details; //or protected $details;

Resources