Laravel 5.7 - Access Notifiable inside view template - php

What is the correct way to access $notifiable inside my view template?
I understand $notifiable is the user but when I have the following
public $abc;
public function __construct($abc)
{
$this->abc = $abc;
}
public function toMail($notifiable)
{
$mailMessage = (new MailMessage)
->from('xyz#xyz.com', 'xyz company')
->subject('xyz')
->markdown('emails.news-alert');
return $mailMessage;
}
Inside my blade template:
Hello {{ $notifiable->first_name }}
{{ $abc }}
The above throws an error because it doesn't recognize $notifiable
But if I pass it in as follows then it works:
$mailMessage = (new MailMessage)
->from('xyz#xyz.com', 'xyz company')
->subject('xyz')
->markdown('emails.news-alert', ['notifiable' => $notifiable);
Is $notifiable not a public property - I thought it was available to the view as default without needing to pass it through?

Yes you need to send the variables to use in the view
$mail->markdown(
'emails.news-alert', [
'notificable' => $notificable,
'abc' => $this->abc
]
);
some time you can use compact() helper but only when you have named variables (not $this->)
$abc = $this->abc;
$mail->markdown('emails.news-alert', compact(['notificable','abc']));
please try this and let me know how it works :)

Related

Laravel send email no data

I have some problems with my Laravel app.
I'm trying to send an email, but any time it sends it's not sending details that I need to pass to view.
I'm trying like (view)
Hello <strong>{{ $order['title'] }}</strong>,
<p>{{ $order['body'] }}</p>
But title and body are empty.
This is how controller looks like:
$order = [
'title' => 'title',
'body' => 'test body'
];
\Mail::to($user->email)->send(new OrderCreated($order));
And this is in mail
public $order;
public function build()
{
return $this->subject('Order Created')->view('emails.order');
}
What is wrong here?
Your problem is that you are not setting the $order property on your mailable.
You passing the order in when you do \Mail::to($user->email)->send(new OrderCreated($order)); so you just need to accept and set it in your mailable:
public function __construct($order)
{
$this->order = $order;
}
From there, the order will actually be accessible in your view as it is a public property.

Use variable inside another variable in a mail blade view

I'm using the Mail library in Laravel to send html email with custom data passed to a blade view.
The problem born when the mail has to render the html fetched from a row in the database which include a variable that i pass through the view.
This is my build function in my mailable class
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => $this->parameters->text,
]);
}
Then in the blade view:
<div>
{!! $text !!}
</div>
This is what the $text variable looks like:
<p>
<span>This is my text for the mail</span>
Click here to compile
</p>
The link href shoul contain the url variable value instead of not passing the variable name itself
A simple solution would be formating with php:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'text' => str_replace('{{ $url }}','https://google.com',$this->parameters->text)
]);
}
I did not try by myself but you could make an attempt with Blade::compileString(), i.e.:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => \Blade::compileString($this->parameters->text),
]);
}

How to select an id in route pass it into the controller and fetch data against that id and redirect it view again in laravel 5.6

How to pass id from veiw into controller in then into another view again??
First of all this a basic question you should be able to search it anywhere on the internet. any how below is the solution.
web/routes.php:
Route::get('user/{id}','USerController#find')->name('user.get');
Or by passing user object in the route:
Route::get('user/{user}','USerController#find')->name('user.get');
UserController:
the below function accepts user object as a parameter in the route we defined above.
public function find(user $user)
{
return view('user.detail',compact('user'))
}
or
public function find($id)
{
$user = USer::find($id);
return view('user.detail',compact('user'))
}
resources/view/user/detail.blade.php:
{{ $user -> name }}
{{ $user -> email }}
and in order visit the route user.find use the below line:
View Detail
No you can use this code as reference to your project.
first solution is .
change route
Route::get('/show_vedio/{$id}', 'VedioController#show')->name(show_videos);
and use this {{ route('show_vedios', $vedio->id )}} in href
it's not entirely clear what you are trying to do, maybe i will help you:
public function show(int $id): View
{
$video = Video::find($id);
return view('video.show', ['video' => $video]);
}

How to use variables in routes in laravel?

I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......

Laravel 5.1 passing data to mail view doesn't work

So, I'm trying to make an e-mail view, using data the user posted. The problem is, that specific data is unreachable. I don't know how I'm supposed to get that data.
Here is my controller:
public function PostSignupForm(Request $request)
{
// Make's messages of faults
$messages = [
//removed them to save space
];
//Validation rules
$rules = [
//removed them to save space
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return Redirect::back()->withInput()->withErrors($validator);
}
DB::table('rittensport')->insert([
'note' => $request->get('note'),
//standard instert
]);
/**
* Sending the e-mails to the pilot and co-pilot
*
* #return none
*/
Mail::send('emails.rittensport_signup', $request->all(), function ($message) {
$message->from(env('APP_MAIL'), 'RallyPodium & Reporting');
$message->sender(env('APP_MAIL'), 'RallyPodium & Reporting');
$message->to($request->get('piloot_email'), strtoupper($request->get('piloot_lastname')).' '.$request->get('piloot_firstname'));
$message->to($request->get('navigator_email'), strtoupper($request->get('navigator_lastname')).' '.$request->get('navigator_firstname'));
$message->subject('Uw inschrijving voor de RPR Gapersrit '. date('Y'));
$message->priority(1);//Highest priority (5 is lowest).
});
return Redirect::back();
Well, the view exists and the error I'm facing to is:
Undefined variable: request.
This is how I try to get the data in the e-mail view: {{ $request->get('note') }} I already tried things like {{ $message->note }}, $message['note'] And so on.
Try this:
Mail::send('emails.rittensport_signup', array("request" => $request), function (...

Categories