I use laravel 5.3
My notication laravel like this :
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Messages\BroadcastMessage;
class GuestRegistered extends Notification implements ShouldBroadcast, ShouldQueue
{
use Queueable;
private $data;
public function __construct($data)
{
$this->data = $data;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('test')
->greeting('Hi')
->line('Thanks')
->line('Your password : '.$this->data)
->action('Start Shopping', url('/'));
}
}
I want to add condition in toMail method
So if $this->data not exist then ->line('Your password : '.$this->data) not display or not executed
How can I do it?
Add a default value for the $data parameter in the constructor .
public function __construct($data = null)
{
$this->data = $data;
}
Then you can store the instance of MailMessage in a variable and use the if statement to add the line() you want.
public function toMail($notifiable)
{
$mailMessage = new MailMessage();
$mailMessage
->subject('test')
->greetings('Hi')
->line('Thanks');
if($this->data) {
$mailMessage->line('Your password: ' . $this->data);
}
$mailMessage->action('Start Shopping', url('/'));
return $mailMessage;
}
Related
I am having trouble passing variables from the controller to the mail fucntion I have tried to search it up on google but didn't find anything that works my goal is to get the variable from the form to the welcomemail.blade the current issue is that the variable doesnt get to my wlcomemail.php from the controller. the mailing part itself does work.
Controller code:
$email_data = array(
'first_name'=>'John',
'last_name'=>'Doe',
'email'=>'john#doe.com',
'password'=>'temp',
);
//Customer::create($data);
Mail::to($email_data['email'])->send(new welcomemail($email_data));
welcomemail.php code:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class welcomemail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public $email_data;
public $example;
public function __construct()
{
$this->email_data = $email_data;
$this->example = 'example';
}
public function build()
{
return $this->markdown('emails.welcomemail');
}
}
blade code:
# Introduction
Welcome.
{{$email_data['first_name']}}
{{$example}}
#endcomponent
change this:
public function __construct()
{
$this->email_data = $email_data;
$this->example = 'example';
}
to:
public function __construct($email_data)
{
$this->email_data = $email_data;
$this->example = 'example';
}
now you have it in your mail class
it should work
I want to add user address in address table and want to update the address_id in user table for that i'm using user model and address model, data is being saved in address table but when i use User model in Address Repository
use App\Models\User;
i get
Cannot declare class App\User, because the name is already in use
Here is my code :
<?php
namespace App\Repositories;
use App\Models\Addresses;
use App\Models\User;
use App\Contracts\AddressContract;
use Illuminate\Database\QueryException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
class AddressRepository extends BaseRepository implements AddressContract
{
/**
* AttributeRepository constructor.
* #param Attribute $model
*/
public function __construct(Addresses $model)
{
parent::__construct($model);
$this->model = $model;
}
public function addAddress(array $params)
{
try {
$Addresses = new Addresses($params);
$Addresses->save();
$addressId = $Addresses->id;
$userID=auth()->user()->id;
if($params['is_primary_address']==1)
{
User::where('id',$userID)->update(['address_id'=>$addressId]);
}
return $Addresses;
}
catch (QueryException $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
}
}
ProductController.php
<?php
namespace App\Http\Controllers\Site;
use App\Contracts\AttributeContract;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Contracts\ProductContract;
use App\Contracts\AddressContract;
use Cart;
use Validator;
class ProductController extends Controller
{
protected $productRepository;
protected $attributeRepository;
protected $addressRepository;
public function __construct(ProductContract $productRepository, AttributeContract $attributeRepository, AddressContract $addressRepository)
{
$this->productRepository = $productRepository;
$this->attributeRepository = $attributeRepository;
$this->addressRepository = $addressRepository;
}
public function addUserAddress(Request $request)
{
$customer_name=$request->customer_name;
$customer_address=$request->customer_address;
$country=$request->country;
$city=$request->city;
$zip_code=$request->zip_code;
$state=$request->state;
$address_type=$request->address_type;
$is_primary_address=$request->primary_address;
$userID=auth()->user()->id;
$data=array('name'=>$customer_name,'address'=>$customer_address,'country'=>$country,'state'=>$state,'city'=>$city,'address_type'=>$address_type,'user_id'=>$userID,'is_primary_address'=>$is_primary_address);
$userAddress = $this->addressRepository->addAddress($data);
return redirect()->back()->with('message', 'Address Added');
}
}
Hello I want to use my own repository class in my Laravel 5.8 project
I created my file Repository in the App File and in this file I added A class called ConversationRepository
This is my class:
<?php
namespace App\Repository;
class ConversationRepository{
private $user;
public function __construct(User $user){
$this->user=$user;
}
public function getConversation(int $userId){
return $this->user->newQuery()
->select('name','id')
->where('id','!=',$userId)
->get();
}
}
And then when I use it on my controller :
<?php
namespace App\Http\Controllers;
use App\User;
use Auth;
use Illuminate\Http\Request;
use App\Repository\ConversationRepository;
class ConversationsController extends Controller
{
private $r;
private $auth;
public function __construct(ConversationRepository $conversationRepository,AuthManager $auth){
$this->r = $conversationRepository;
$this->auth = $auth;
}
public function index(){
return view('conversation.index',[
'users'=>$this->r->getConversation($this->auth->user()->id)
]);
}
public function show(User $user){
return view('conversation.show',['users'=>$this->r->getConversation(
$this->auth->user()->id),
'user'=>$user
]);
}
public function store(User $user){
}
}
I get the error
Class App\Repository\User does not exist
Apparently, you forgot to add use App\User; in the class ConversationsController file.
I want to have one public variable $users = User::all(); so i could use it in different methods inside controller and it doesn't work this way:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users = User::all();
public function __construct() {
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
return view('admin.users', compact('users'));
}
}
i get this error: Constant expression contains invalid operations
What am i doing wrong?
Try adding the assignment into the __construct() function instead:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class AdminController extends Controller
{
public $users;
public function __construct() {
$this->users = User::all();
$this->middleware('auth');
}
public function index()
{
return view('admin.index');
}
public function showUsers()
{
$users = $this->users;
return view('admin.users', compact('users'));
}
}
You need to initialize $users in your constructor:
<?php
public $users;
public function __construct() {
$this->middleware('auth');
$this->users = User::all();
}
Im currently having a problem where I can't queue a job inside a websocket server, my objective is to fire a background job in a specified date and time
I'm having an error that says "An error has occurred: Serialization of 'Closure' is not allowed"
here's is my current code
<?php namespace App;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use App\Http\Controllers\WinIt;
use Cache;
use Carbon\Carbon;
use Session;
use Queue;
// Command Classes
use App\Commands\WinItInit;
class WebsocketServer implements MessageComponentInterface {
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
// Fire event when current date and time reaches $datetime
$datetime = '2016-02-10 15:48:08';
$serialization = Queue::later($datetime, new WinItInit($conn, $this->clients));
}
}
// The command to fire
<?php namespace App\Commands\WinIt;
use App\Commands\Command;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldBeQueued;
// Class Aliases
use DB;
use Input;
use Queue;
use Request;
use Response;
use Session;
use URL;
use Illuminate\Http\Exception;
use Illuminate\Support\Facades\Artisan;
// Custom Classes
class WinItInit extends Command implements SelfHandling, ShouldBeQueued {
use InteractsWithQueue, SerializesModels;
private $from = null;
private $clients = null;
/**
* Create a new command instance.
*
* #return void
*/
public function __construct($from = null, $clients = null)
{
$this->from = $from;
$this->clients = $clients;
$this->type = 'sample';
}
/**
* Execute the command.
*
* #return void
*/
public function handle()
{
$data = array();
switch ($this->type)
{
case 'sample':
$datetime = date('Y-m-d H:i:s', time());
break;
}
foreach ($this->clients as $client)
{
$client->send(json_encode($data));
}
}
}