In my controller, I am trying send mail like this
$activationLink = $activation->GetActivationCode->ActivationLink;
\Mail::to($company)->send(new MLink);
I have a variable called activationlink, which I need to send it to the email
Mlink Mail class
public function build()
{
return $this->view('emails.mindbody')->with($activationLink);
}
View file
<h2>Your activation link is : {{ $activationlink }} </h2>
It's not working this way, I get the activationlink is not defined error.
How can I pass the $activationLink from my controller, to the view file (the email that is sent)?
You can add it in the constructor of MLink class like this :
private $activationLink;
public function __construct($activationLink)
{
$this->activationLink = $activationLink;
}
public function build()
{
return $this->view('emails.mindbody')->with($this->activationLink);
}
And in the controller
$activationLink = $activation->GetActivationCode->ActivationLink;
\Mail::to($company)->send(new MLink($activationLink));
Or as mentioned by #Camilo you can set the visibility of $activationLink to public and remove ->with keyword because you will have access to this variable in the view :)
Related
im trying to call a function from another route in laravel php.
I have the route "welcome"
Route::get('/', function () {
return view('welcome');
});
where im calling this extends (sidebar)
#extends('layouts.guestnavbar')
i want to see if my table notifications is empty or not so i made this controller
class GuestNavbarController extends Controller
{
public function isempty(){
$notif = Notification::first();
if(is_null($notif)) {
return view('layouts.guestnavbar')->with("checkempty", "empty");
}else {
return view('layouts.guestnavbar')->with("checkempty", "not empty");
}
}
}
and i called the variable {{ $checkempty }} in my route guestnavbar
Route::get('/guestnavbar', [GuestNavbarController::class, 'isempty']);
and it works when im in the route guestnavbar
but doesnt work when im in the route welcome because i call the function in the route guestnavbar and in welcome he doesnt recognize the variable: checkempty
i need this function to be in the guestnavbar because i have to call it on other pages, not just in welcome page
I appreciate any help.
You don't need isempty inside controller, just add method isempty inside Notification model, you can use something like this inside Notification model:
public static function isEmpty(){
return Notification::first() ? true : false;
}
And then where you need to check if notification table is empty just call Notification::isEmpty()
After setting up a controller and a view to show a specific entry from my database, I wanted to use laravels function of Route Model Binding to fetch the data fromn the database and pass it to the view. However I am getting following error:
Symfony\Component\Debug\Exception\FatalThrowableError thrown with
message "Argument 2 passed to
Symfony\Component\HttpFoundation\RedirectResponse::__construct() must
be of the type integer, array given, called in
C:\xampp\htdocs\laravel\Cyberchess\vendor\laravel\framework\src\Illuminate\Routing\Redirector.php
on line 203"
I've tried to add this line to TrustProxy:
protected $headers = Request::HEADER_X_FORWARDED_ALL;
as the internet recommended, but when I opened the file, I realised it was already in the code.
My create/store works properly, which is why I assume it has something to do with Route Model Binding. I'm currently using a getRouteKeyName() to change the Key to 'AccID' so it should work.
//my controller
public function show(account $account){
//$account = account::where(['AccID' => $account])->get();
//dd($account);
return redirect('user.show', compact('account'));
}
//my model
class account extends Model
{
public function getRouteKeyName() {
return 'AccID';
}
public $timestamps = false;
}
//my view
<h1 class="title">Your Profile</h1>
<p>{{ $account->Nick }}</p>
I expected it to work just fine(duh), but got said error. When I dd(); the data, it has the information I want inside #attributes and in #original.
If if comment the dd() and let the return do it's work, I get the error.
The redirect() helper function is used to send a redirect 301 response from the server, what you want instead is to return a view like so
public function show(account $account)
{
$account = account::where(['AccID' => $account])->get();
return view('user.show', compact('account'));
}
I have in Laravel a view in which already foreach loops have been incorporated. At the bottom of the page is a small form. Now I want to send this form to save the data into the database.
At the same time I want to stay in the same view. If I enter the same page in the form, I get an error message. If I want to go back to the view via the controller, I also get an error.
In this error, the output of the data in the loop that was previously passed by another controller no longer works. - What can I do?
Undefined variable: data (View: /srv/users/smartfinance/apps/smartfinance/public/laravel/resources/views/home.blade.php)
<?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
I hope you understand my problem and can give me a hint how to solve it.
These are the two controller:
class checkDataController extends Controller
{
public function data()
{
$data = DB::table('test')->where('booking_date', '=', $today)->get();
return view('home', compact("data"));
}
}
class AddContractController extends Controller
{
public function addNewData(Request $request)
{
$bez = $request->bez;
return view('home');
}
}
Typically this is done by redirecting back to the same page you submitted from. Here is what that would look like in your controller:
public function addNewData(Request $request)
{
$bez = $request->bez;
return back();
}
For a better user experience, you should also add a message to the view with the form:
#if(session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
And make one small change to your controller:
return back()->with('status', "Successfully submitted {$bez}!");
Your mistake is that you try to display the same view from two different controller methods. Normally, a view is only being used by one controller method. Other methods can redirect to this method in order to (re-)display the same view. This way, the logic of retrieving data for the view is only required in one place.
class CheckDataController extends Controller
{
// route 'check.data'
public function data()
{
$data = DB::table('test')->where('booking_date', '=', $today)->get();
return view('home', compact("data"));
}
}
class AddContractController extends Controller
{
// route 'contract.create'
public function addNewData(Request $request)
{
Contract::create($request->input()); // unsafe, only for demonstration...
return redirect()->route('check.data');
}
}
As you can see, instead of return view('home'), we are returning redirect()->route('check.data'). This will redirect the user to the other controller with the defined route. Of course this means you are actually executing two controller methods within one user action, but that's common practice.
More information about redirects can be found in the official documentation.
An advise for you my friend don't return a view from a post method.
I suppose the addNewData() method is a method that comes from the post route. so when you return a view after a post method you don't provide the data for your page. so laravel throws an error and complains about the missing variable. so what you must do is redirect to the route that views the page. So your method would look something like this:
public function addNewData(Request $request)
{
$bez = $request->bez;
return redirect('YOUR ROUTE TO VIEW THE PAGE (URL)');
}
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
I've tried many solutions that had the same questions like mine. But didn't found a working solution.
I have a controller:
event.php
And two views:
event.phtml
eventList.phtml
I use eventList to get data via ajax call so I want to populate both views with a variable named "eventlist" for example.
Normally I use this code for sending a variable to the view:
$this->view->eventList = $events;
But this variable is only available in event.phtml.
How can I make this available for eventlist.phtml? (without adding a second controller)
Edit:
I get this error now
Call to undefined method Page_Event::render()
Function:
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
$this->eventList = $this->_event;
$this->render();
$this->render('eventlist');
}
If I use $this->view->render('event.phtml') and eventlist.phtml it won't pass the data
I'm using zend version 1
You can pass variables to other views using render()
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
}
Copy and paste this in your controller and rename your controller from event.php to EventController.php
class EventController extends Zend_Controller_Action
{
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
// You're calling the index.phtml here.
$this->eventList = $this->_event;
$this->render('event');
$this->render('eventlist');
}
}
To specify that only written #Daan
In your action:
$this->view->eventList= $events;
$this->render('eventList'); // for call eventList.phtml
In you View use : $this->eventList
You could render it within the view itself (eventList.phtml), rather than within the controller, using the same line of code you used above:
$this->render('event[.phtml]');