I have the following snippet of code, where I am trying to pass the email object to my view.
return response()->view('admin.editEmail')->with('email', $this->template->findTemplateById($id));
This results in the following error:
Call to undefined method Illuminate\Http\Response::with()
How can I fix this?
Just pass it as a second parameter in view():
return response()->view('admin.editEmail', $email);
Here are the docs for view->with(). You have to pass a key and a value for each variable you send. If you have multiple, you need to send an array of keys=>values. compact is useful for this.
$template = $this->template->findTemplateById($id)
response()->view('admin.editEmail')->with(compact('email', 'template'));
There are many different ways of doing this.
I prefer to use this one as you can easily add new variables.
In this way its also posible to have different variable names in your controller and view.
return view('admin.editEmail', [
'email' => $adminEmail,
'anotherVar' => $someValue,
]);
In this example the email in the controller is stored in $adminEmail but in the template we will receive it as $email.
With compact you need to have the same variable name in your controller as in your view.
Related
I have a string inside a view that I need to pass to a second view.
Inside view1 I have
route('admin.olt.add', $id, $resp)
where $resp is the parameter I need to pass to the second view. My route file calls the controller that returns the second view
Route::get('/olt/{id}/add', 'OLTController#config_parameters_onu')->name('olt.add');
Is there any way where I can pass this parameter without adding it to the url?
I don't know if I fully understand your issue but as I got it, you can add this variable to a session and then fetch it whenever you want in any view for instance
to add the variable to the session
session(['resp' => $resp]);
to fetch it back
session('resp')
and here is the full docs for better guidance
Hope that is what you looking for
you should use an array of more than one parameter
route('admin.olt.add', ['id'=>$id, 'resp'=>$resp]);
I'm very new here please.
I'm trying to get some information and pass it to the laravel blade to be displayed on my website. What is for sure is that the call was successful but when I try passing it to the view I get an error that says "undefined variable" in the view page and sometimes the area where information was supposed to be displayed just shows an empty space.
Pass your variables in the view method instead. The with function is for passing individual data, so it doesn't accept an array. Example:
return view('profile.newaddress', [
'address'=>$address
]);
If you want to use with, try it like this instead (assuming $address in a string):
return view('profile.newaddress')->with('address', $address);
I'm trying to send one param ($id) to view layout\html.php using compose() method of mailer component. But i don't know how to get it
The code:
$id = 1;
Yii::$app->mailer->compose('\layouts\html.php', ['id' => $id])
->setFrom('stackfrom#gmail.com')
->setTo('stackto#gmail.com')
->setSubject('Email sent from Yii2-Swiftmailer')
->send();
And in a line of my view layout\html.php
<div><?php echo $id ?></div>
Error is here!
You don't call a view file with compose() method like this, you should use alias like #common, #frontend or any other relevant to the view you are trying to load mostly we place all views related to emails in common/mail so we will use #common alias in example.
You can use 2 ways
You may pass additional view parameters to compose() method, which will be available inside the view files.Yii::$app->mailer->compose('#common/path/to/view', ['id' => $id]);
Render HTML separately
$body =Yii::$app->view->renderFile('#common/path/to/view-file.php',['id'=>$id])
to render the HTML from the php file, pass it any parameters you want to like any normal view file and then attach it to the email body using setHtmlBody($body), use the following way
$body = Yii::$app->view->renderFile ( '#common/mail/account-activation.php' , [
'id' => $id
] );
Yii::$app->mailer->compose()
->setFrom('stackfrom#gmail.com')
->setTo('stackto#gmail.com')
->setSubject('Email sent from Yii2-Swiftmailer')
->setHtmlBody($body)
->send()
For more help see Documentation
Compose method takes special alias in format '#app/mail/layouts/default.php'
So if you are using absolute format, use # and path (yii2 aliases)
Reference to swift mailer you can see here: http://www.yiiframework.com/doc-2.0/yii-swiftmailer-mailer.html
In Laravel controller function you can return view with array of variables you like to share with the current view. Something like this:
public function index() {
return view('index')->with(['name' => 'John doe']);
}
Then you can use the name inside the with() function as $name in your view.
Can someone explain how it been done please? I would like to do something similar by myself.
We can pass individual pieces of data to the view using the 'with' method.
When passing information in this manner, the data should be an array with key / value pairs. Inside your view, we can then access each value using its corresponding key, such as
<?php echo $key; ?>
Laravel include the view file inside a function which makes all defining variables scope within a function.
Just before including files laravel calls PHP's builtin function extract(array) which declare all the variable in the scope.
extract create variables out of array making key as variable name and value as variable value.
I'm looking for a way to edit data before passing them to view.
Quick example (just to demonstrate):
Let's say I am passing variable $name to a view through controller. I would like to use something to pass another variable $message which would contain Hello $name, so for example Hello John, if variable $name would be John.
I don't want to send this second variable in controller, because I'm gonna use a lot of controllers, views and the thing that I want to do with the data is rather complicated.
I need to use this for both variables view("foobar", ["foo" => "bar"]) and sessions view("foobar")->with("foo", "bar").
I've tried to use both Middleware and Service Provider but the problem was I couldn't access the sent data.
The only possible solution I can think of now is to use View layout which I'm going to include into every view and which is gonna transform the variables (using something like <? $message = "Hello $name"; ?> in the view), but this doesn't seem like the right MVC solution for me.
Thank you all for your answers!
If you want to pass session data and multiple variables, do this:
session()->flash('message', 'some message');
return view('foobar', [
'foo' => 'bar',
'second' => 'something'
]);
Update
If I understood you correctly, you want to use view composer.