I'm trying to convert html page to pdf using dompdf wrapper in laravel. If I show the html page, the informations from database are displayed on the page, but when I try to convert that to pdf I have only empty div tags without values from the database.
This is my code for converting pdf:
public function viewUserOrderDetails($id){
$user = User::find($id);
$pdf = PDF::loadView('userOrderDetails',['user' => $user]);
return $pdf->stream("index.pdf");
}
and the route function:
Route::post('/userOrderDetails/{id}',[
'uses' => 'showAllOrders#viewUserOrderDetails',
'as' => 'orderdetail',
]);
View sample code:
<div class="container">
<form class="form-horizontal" method="POST">
<fieldset>
<div class="col-lg-12 form-group margin50">
<label class="col-lg-2" for="Name">User</label>
<div class="col-lg-4">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" id="name" name="Name" placeholder="" class="form-control name" value="{{ $user->name }}" readonly>
{{ csrf_field() }}
</div>
</div>
<div class=" col-lg-12 form-group">
<label class="col-lg-2" for="ProductType">Orders</label>
<div class="col-lg-4">
#foreach($user->orders as $order)
<input type="text" placeholder="" class="form-control name" value="{{ $order->order_name }}" readonly>
#endforeach
</div>
</div>
<div class="col-lg-12 form-group">
<label class="col-lg-2" for="Manufacturer">Price</label>
<div class="col-lg-4">
#foreach($user->orders as $order)
<input type="text" placeholder="" class="form-control name" value="{{ $order->cena }}" readonly>
#endforeach
</div>
</div>
</div>
</form>
Also if i try to change $pdf = PDF::loadView('userOrderDetails',['user' => $user]); to this
$pdf = PDF::loadView('userOrderDetails',compact('user')); it gives me user not found error
Any ideas?
Related
i am making a simple crud system using laravel.all working fine when i going to edit the records ran into the problem with record is not updated i didn't get any error. get the message record updated.when i check the table it is not updated.what i tried so far i attached below
Controller
public function edit(Student $student)
{
return view('students.edit',compact('student'));
}
public function update(Request $request, Student $student)
{
$request->validate([
]);
$student->update($request->all());
return redirect()->route('students.index')
->with('success','Student updated successfully');
}
Edit.blade.php view
<form action="{{ route('students.update',$student->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>StudName:</strong>
<input type="text" name="name" value="{{ $student->studname }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Course:</strong>
<input type="text" name="name" value="{{ $student->course }}" class="form-control" placeholder="course">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Fee</strong>
<input type="text" name="name" value="{{ $student->fee }}" class="form-control" placeholder="fee">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
I think the problem because of your blade file. you have to put the correct name for each input.
<input type="text" name="name" value="{{ $student->studname }}" class="form-control" placeholder="Name">
--------------------------
<input type="text" name="course" value="{{ $student->course }}" class="form-control" placeholder="Course">
I made a function other than resource and I am trying to update my data and the update page is showing error.
I am solving this error I tried to resolve but i am unable to find way to pass second variable
Edit.blade.php
#extends('home')
#section('title', '| Edit User')
#section('dashboard')
<div class='col-lg-4 col-lg-offset-4'>
<h1><i class='fa fa-user-plus'></i> Edit {{$user->name}}</h1>
<hr>
<form action="{{ route('users.updated', ['id' => $user->id]) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="name" class="col-sm-2 col-form-label">Name *</label>
<input type="text" class="form-control col-sm-10" id="name" name="name"
value="{{$user->name}}" />
</div>
<div class="form-group">
<label for="email" class="col-sm-2 col-form-label">Email *</label>
<input type="email" class="form-control col-sm-10" id="email" name="email"
value="{{$user->email}}" />
</div>
<h5><b>Give Role</b></h5>
<div class='form-group'>
#foreach ($roles as $role)
<input class="form-check-input" type="checkbox" name="roles[]" value="{{ $role->id }}">
<label class="form-check-label" for="defaultCheck1">
{{$role->name}}
</label>
#endforeach
</div>
<div class="form-group">
<label for="password" class="col-sm-2 col-form-label">Password *</label>
<input type="password" class="form-control col-sm-10" id="password" name="password"
value="{{$user->password}}" />
</div>
<div class="form-group row" style="margin:5%;">
<button type="submit" class="btn btn-primary col-sm-3 col-sm-offset-3">Edit User</button>
</div>
</form>
</div>
#endsection
Web.php
Route::get('users/{id}/change' ,'UserController#admin_side_edit');
Route::post('users/savechanges/{id}', [
'as' => 'users.updated',
'uses' => 'UserController#admin_side_update'
]);
This is causing me the error if someone would please help me.
After changes
Actually you don't pass the id to the action, in the blade.
For you action in your form you can use this :
{{ route('users.updated', ['id' => $user->id]) }}
And don't forget to add params in the route
Route::post('users/savechanges/{id}', [
'as' => 'users.updated',
'uses' => 'UserController#admin_side_update'
]);
Hope this can help you.
I'm making a step program and therefore I wanted to use sessions. I have a form which allows the user to fill in some information and then the input values will be put in a session array called "basic_settins" like this
public function saveSettings(Request $request, Product $products)
{
$request->session()->push('basic_settings', $request->input());
return redirect('/themes');
}
But how can I get a specific item from that array and display it for instance in my view? By using something like this: {{ Session::get('store_name') }} in an input field.
The view looks like this:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<form method="POST" action="{{ route('saveBasicSettings') }}">
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary mb-3 float-right">Next</button>
</div>
</div>
<div class="card z-depth-2" style="border-radius: 7px;">
<div class="card-body">
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
{{ csrf_field() }}
<div class="form-group">
<label for="store_name">Store name</label>
<input type="text" class="form-control" placeholder="Store name" id="store_name" name="store_name" value="{{ Session::get('store_name') }}" required>
</div>
<div class="form-group">
<label for="company name">Company name</label>
<input type="text" class="form-control" placeholder="Company name" id="company_name" name="company_name" value="{{ Session::get('company_name') }}" required>
</div>
<div class="form-group">
<label for="company_tel">Company phonenumber</label>
<input type="text" class="form-control" placeholder="Company Phonenumber" id="company_tel" name="company_tel" value="{{ Session::get('company_tel') }}" required>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
#endsection
When I dd the request by doing this: $data = $request->session()->all(); and then dd($data);
I get the following result:
How can I make this work? Thanks in advance!
When you're using push(), an array will be created, so you need to do something like this:
{{ session('basic_settings.0.company_name') }}
Or:
{{ session('basic_settings')[0]['company_name'] }}
But I would recommend you to save the data with:
{{ session(['basic_settings' => request()->all()]) }}
Then you'll be able to read it with:
{{ session('basic_settings.company_name') }}
how to display duplicate entry warning error to my view in laravel blade. so when they key in same name the warning will appear when they saved it.
Note: i have already make my Schema $table->string('studentname')->unique();;
Controller
public function store(Request $request)
{
$this->validate($request, [
'studentname'=>'required|max:50',
]);
$students = new Student();
$students->studentname = $request->studentname;
$students->address = $request->address;
$students->religion = $request->religion;
$students->save();
return redirect()->route('students.index')
->with('flash_message', 'Success.');
}
View-Blade
<div class="container">
<h1 class="well">Registration Form</h1>
<div class="col-lg-12 well">
<div class="row">
<form action="{{route('students.store')}}" method="POST">
{{csrf_field()}}
<div class="col-sm-12">
<h3>CHILD'S INFORMATION</h3>
<hr>
<div class="row">
<div class="col-sm-4 form-group">
<label>FULLNAME</label>
<input type="text" name="studentname" value="" placeholder="Enter FULLNAME.." class="form-control" required>
</div>
<div class="col-sm-4 form-group">
<label>RELIGION</label>
<input type="text" name="religion" value="" placeholder="Enter RELIGION.." class="form-control">
</div>
<div class="col-sm-4 form-group">
<label>ADDRESS</label>
<input type="text" name="address" value="" placeholder="Enter ADDRESS.." class="form-control">
</div>
<div>
<button type="submit" class="btn btn-default">SUBMIT</button>
</div>
</div>
</div>
</div>
Add the unique validation which returns a message if it fails.
$this->validate($request, [
'studentname'=>'required|max:50|unique:table_name,studentname',
]);
And then, in your blade template, do this.
<div class="col-sm-4 form-group {{ $errors->get('studentname') ? 'has-error' : '' }}">
<label>FULLNAME</label>
<input type="text" name="studentname" value="" placeholder="Enter FULLNAME.." class="form-control" required>
#foreach($errors->get('studentname') as $error)
<span class="help-block">{{ $error }}</span>
#endforeach
</div>
I have just created a new project in larvel and I am trying to submit form in db but it is not saving data in db.
here is my form
<form id="" method="post" class="form-horizontal" action="{{ route('updateadminprofile')}}"enctype="multipart/form-data" >
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="font-size: 18px;">
<strong>Success: </strong>
{{ Session::get('success') }}
</div>
#endif
<div class="form-group">
<label class="col-sm-4 control-label" for="userName"> Name</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="name" name="name" placeholder="name}" value="name" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="userName"> Description</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="description" name="description" placeholder="Description}" value="description" />
</div>
</div>
<button style="margin-left: 30%" type="submit" class="btn btn-primary" name="signup" value="sumbmit" >Save</button>
My route is
Route::group(['namespace' => 'PrivatePages'], function () {
Route::any('/updateadminprofile', ['as' => 'updateadminprofile',
'uses' => 'ProductController#UpdateAdminProfile']);
});
here is my contoller function
public function UpdateAdminProfile(CreateProductRequest $request)
{
$saveproduct = new Product();
$saveproduct->name = $request->name;
$saveproduct->description = $request->description;
$saveproduct->save();
}
it is not saving record in db. and when i try to submit form it gives me below text,
The page has expired due to inactivity.
Please refresh and try again.
when i added csrf in form it even not going to route specified in the action of form
Try this, you are probably missing the CSRF field in your form. Also keep the Flash message out of your form.
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="font-size: 18px;">
<strong>Success: </strong>
{{ Session::get('success') }}
</div>
#endif
<form id="form" method="post" class="form-horizontal" action="/updateadminprofile">
{{ csrf_field() }}
<div class="form-group">
<label class="col-sm-4 control-label" for="name"> Name</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="name" name="name" placeholder="name" value="name" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="description"> Description</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="description" name="description" placeholder="Description}" value="description" />
</div>
</div>
<button style="margin-left: 30%" type="submit" class="btn btn-primary" name="signup" value="submit">Save</button>
</form>
Edit: cleaned up some minor HTML errors and specified the route directly instead of a blade function. Could you try this?
I solved the issue by just adding following namespaces in the CreateProductRequest and ProductController
In Create CreateProductRequest updated following lines.
use Illuminate\Support\Facades\Request;
class CreateProductRequest extends Request{}
In Product Controller i use following namespace
.
use App\Http\Requests\CreateProductRequest;
Thanks All for your time and help