Display JSON Data store in JSON in blade (Laravel 8) - php

I have produce notifications through email and manage to send the email.
Now I want to change the template provided by Laravel so I create a new customize blade that will be displayed.
I have a problem on transferring the data to the blade view.
Below is my code:
public function __construct($offerData)
{
$this->offerData = $offerData;
}
public function toMail($notifiable)
{
/*This is the original code with default template */
// return (new MailMessage)
// ->greeting($this->offerData['name'])
// ->line($this->offerData['body'])
// ->action($this->offerData['offerText'], $this->offerData['offerUrl'])
// ->line($this->offerData['thanks']);
/*This is the code use to display the customize template*/
return (new MailMessage)->view(
'email_notification',
['data' => $this->offerData]
);
Below is How I display the code:
<!DOCTYPE html>
<html>
<head>
<title>Hi Awak</title>
</head>
<body>
#foreach($data as $data_)
<p>{{ $data_ }}</p>
#endforeach
</body>
</html>
This is the result:
I cannot do any specific modification for each data as you can see it only repeat the p tag here.

Thank you for help....
I manage to found the solution...
I just store array in array...
public function toMail($notifiable)
{
return (new MailMessage)->view(
'email_notification',
['name' => [$this->offerData['name']],
'body' => [$this->offerData['body']]
]
);
}

Related

Undefined variable problem on Laravel 9.x

I'm trying to get a my title variable from my control page and display it on the about page.
I don't think I have a typo, but it might me. I'm not sure.
Here is my control page code;
class PagesController extends Controller
{
public function index(){
$title = 'Welcome to Laravel';
return view ('pages.index')->with('title', $title);
}
public function about(){
$title = 'About us';
return view ('pages.about')->with('title', $title);
}
public function services(){
$title = 'The services';
return view ('pages.services')->with('title', $title);
}
}
In this page, the index and services functions work fine, but I can't get the about page.
Here is my display pages;
This is Index page
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the Laravel Application</p>
#endsection
This is the about page:
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the About page</p>
#endsection
The error I have
Do this:
return view ('pages.index', compact('title'));
or:
return view ('pages.index', [
'title' => $title
]);
Since you are returning just the title, there isn't any need to call any verbs. Rather you should directly call the view:
route::view('/about', 'Pagecontroller#about');
or
pass the parameter by compact:
return view ('pages.index', compact('title'));
or
return view ('pages.index', ['title' => $title]);
Since this is a test application from a lesson, I forgot to delete some extra code in my route file.
This is my route file:
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#about');
Route::get('/services', 'App\Http\Controllers\PagesController#services');
The commented area shouldn't be here. That was the whole problem over here...
// Route::get('/about', function(){
// return view ('pages.about');
// });
This form of passing variables is a short-lived entry of a variable into the session. Then accessing the variable on the page should look like this:
{{ session('title') }}
If you want to pass data to the view, then you need to use the method
return view('pages.services', ['title' => $title]);
Laravel views

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),
]);
}

Cannot show data from controller to email blade

I want to show $data form controller to view but it shows this error message. Do you know why and how to fix that? Thank you.
Here's my code:
UserController.php
public function forgotPassword(Request $request){
$data['user']['email'] = $request->input('user.email');
Mail::send('vendor.notifications.resetpassword', $data, function($message) use($data){
$message->from('bemsadmin#gmail.com');
$message->to($data ['user']['email']);
$message->subject('Your Email');
});
return response()->json($data);
}
resetpassword.blade.php
<!DOCTYPE html>
<html>
<body>
<p>{{$data['user']['email']}}</p>
</body>
</html>
the error happens beacuse you did not pass a variable called $data to the email view,
you pass a variable like this
Mail::send('vendor.notifications.resetpassword', ['data' => $data], function($message) use($data){
but with your code what get passed is
['user' => ['email' => 'email#examle.com']]
so try in your view to use the following code, it should do the job.
{{$user['email']}}

Passing data from controller to view in Laravel

I am new to Laravel and I have been trying to store all records of table 'student' to a variable and then pass that variable to a view so that I can display them.
I have a controller - ProfileController and inside that a function:
public function showstudents() {
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
In my view, I have this code:
<html>
<head>
//---HTML Head Part
</head>
<body>
Hi {{ Auth::user()->fullname }}
#foreach ($students as $student)
{{ $student->name }}
#endforeach
#stop
</body>
</html>
I am receiving this error: Undefined variable: students (View:regprofile.blade.php)
Can you give this a try,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
While, you can set multiple variables something like this,
$instructors="";
$instituitions="";
$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);
return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);
For Passing a single variable to view.
Inside Your controller create a method like:
function sleep()
{
return view('welcome')->with('title','My App');
}
In Your route
Route::get('/sleep', 'TestController#sleep');
In Your View Welcome.blade.php. You can echo your variable like {{ $title }}
For An Array(multiple values) change,sleep method to :
function sleep()
{
$data = array(
'title'=>'My App',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('welcome')->with($data);
}
You can access you variable like {{ $author }}.
The best and easy way to pass single or multiple variables to view from controller is to use compact() method.
For passing single variable to view,
return view("user/regprofile",compact('students'));
For passing multiple variable to view,
return view("user/regprofile",compact('students','teachers','others'));
And in view, you can easily loop through the variable,
#foreach($students as $student)
{{$student}}
#endforeach
You can try this as well:
public function showstudents(){
$students = DB::table('student')->get();
return view("user/regprofile", ['students'=>$students]);
}
Also, use this variable in your view.blade file to get students name and other columns:
{{$students['name']}}
Try with this code:
return View::make('user/regprofile', array
(
'students' => $students
)
);
Or if you want to pass more variables into view:
return View::make('user/regprofile', array
(
'students' => $students,
'variable_1' => $variable_1,
'variable_2' => $variable_2
)
);
In Laravel 5.6:
$variable = model_name::find($id);
return view('view')->with ('variable',$variable);
public function showstudents() {
$students = DB::table('student')->get();
return (View::make("user/regprofile", compact('student')));
}
try with this code :
Controller:
-----------------------------
$fromdate=date('Y-m-d',strtotime(Input::get('fromdate')));
$todate=date('Y-m-d',strtotime(Input::get('todate')));
$datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To
return view('inventoryreport/inventoryreportview', compact('datas'));
View Page :
#foreach($datas as $student)
{{$student}}
#endforeach
[Link here]
$books[] = [
'title' => 'Mytitle',
'author' => 'MyAuthor,
];
//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));
----------------------------------------------------
//to use this on myView.blade.php
<script>
myVariable = {!! json_encode($books) !!};
console.log(myVariable);
</script>
In laravel 8 and above, You can do route binding this way.
public function showstudents() {
$students = DB::table('student')->get();
return view("user/regprofile",['students'=>$students]);
}
In the view file, you can access it like below.
#foreach($students as $student)
{{$student->name}}
#endforeach

Laravel: Undefined variable content

I have setup a Route:
Route::resource('conferences', 'ConferencesController)
Artisan therefore shows me a route:
POST conferences | conferences.store | ConferencesController#store
When I submit a Form from the create View, I get the error that a variable in my layout file has not been defined.
Undefined variable: content is shown, nothing has been posted.
I opened my form like this:
{{ Form::open(array('url' => '/conferences', 'class' => 'conference-form')) }}
And finally, my store method in ConferencesController looks like this:
public function store()
{
$validator = Validator::make(Input::all(), Conference::$rules);
if($validator->passes()){
$conference = new Conference();
$conference->title = Input::get('title');
$conference->description = Input::get('description');
$conference->location = Input::get('location');
$conference->plannedTime = Input::get('plannedTime');
$conference->save();
Mail::pretend();
Mail::send('emails.conference.create', ['title' => Input::get('title'), 'location' => Input::get('location'), 'plannedTime' => Input::get('plannedTime')], function($message){
$message->to('email')->subject('Een nieuw evenement is gemaakt.');
});
Redirect::to('/conferences')->with('message', 'Nieuw event is aangemaakt!');
} else {
Redirect::to('/')->with('message', 'Iets ging mis');
}
}
How do I fix this error?
** EDIT: Added create method **
public function create(){
$this->layout->content = View::make('conferences.create');
}
This should be really straight forward. In your views directory we usually have a folder called layouts where we put the page structure something like:
// default.blade.php
<html>
<head> </head>
<body>
#yield('content'); // this is where your views will be loaded
</body>
</html>
then in your case you should create a conferences folder and then a file create.blade.php in it.
#extends('layouts.default')
#section('content')
// your forms etc
#stop
And in your create method inside ConferencesController
public function create() {
return View::make('conferences.create');
}
And one last thing, when you try to send the email you should be passing an email address inside the to() function and you are passing a string

Categories