Passing variable from DOMPDF controller to view - laravel

I want create a DOMPDF with laravel, and I must passing my variable to view. I've been try passing variable like below, but it still not working yet.
here my Laravel Controller
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$data = [$salesorder, $detailemployee, $detailservice];
$pdf = PDF::loadView('summary.invoice', $data);
return $pdf->download('invoice.pdf');
}
the error on my view is :
Undefined variable: salesorder
How to passing some variable from Laravel controller to DOMPDF ?
PS : dd($data) result is correctly

You have to pass the data as below
$data = [
'salesorder' => $salesorder,
'detailemployee' => $detailemployee,
'detailservice' => $detailservice
];
or try using compact
$data = compact('salesorder', 'detailemployee', 'detailservice');

You may try this following
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$pdf = PDF::loadView('summary.invoice', array('salesorder' => $salesorder,'detailemployee'=>$detailemployee,'detailservice'=>$detailservice));
return $pdf->download('invoice.pdf');
}

You can use library function to do that easily.
$pdf_text = "It will be the text you want to load";
PDF::loadHTML($pdf_text)->save('public/you-file-name.pdf');
You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)
PDF::loadHTML($pdf_text)->setPaper('a4', 'landscape')->setWarnings(false)->save('public/you-file-name.pdf')

You may try the following.
$html = view('summary.invoice', ['salesorder' => $salesorder, 'detailemployee' => $detailemployee, 'detailservice' => $detailservice])->render();
$pdf = App::make('dompdf.wrapper');
$invPDF = $pdf->loadHTML($html);
return $pdf->download('invoice.pdf');

I know this is old but this may help others. you need to use array key in your view.
Controller:
$data = [
'foo' => $bar,
];
$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('doc.pdf');
View:
<p>{{$foo}}</p>

Related

Laravel 5.8 voyager translator not working when paginated

When I use without paginate, the Voyager Translatable trait works fine.
$data = Post::where('status', 1)->get()->translate();
But, when I change it to paginate:
$data = Post::where('status', 1)->paginate(15)->translate();
in view:
$data->links()
then it gives an error in blade view:
Method TCG\Voyager\Translator\Collection::links does not exist
How can I solve this issue?
i did it that:
public function getTranslated(Builder $query, string $lang) : array {
$data = $query->paginate();
$dataArray = $data->toArray();
$translatedData = $data->translate($lang);
$dataArray['data'] = $translatedData->toArray();
return $dataArray;
}

Make request (pass data) from Command File to Controller in Lumen/Laravel

i get data (query) in command file and want to pass to controller via API (route)
here my request code in command file :
$request = Request::create('/create_data_account', 'post', ['data'=>$data]);
$create = $app->dispatch($request);
this is the route :
$router->post('/create_data_account', 'APIController#create_account_data_api');
and my controller :
public function create_account_data_api(Request $request)
{
$count = 0;
foreach ($data as $key => $value) {
$insert = Account::create([
'account_name' => $value->account_name,
'type' => $value->type,
'role_id' => $value->role_id
]);
if($insert){
$count++;
}else{
return $response = ['result'=>false, 'count'=>$count, 'message'=>'Internal error. Failed to save data.'];
}
}
return $response = ['result'=>true, 'count'=>$count, 'message'=>'Account data saved Successfully'];
}
i'm confused why passing data to controller not working with that code. anyone can give me solution ? Thanks
As you are making the request with ['data'=>$data]. That means all the data is contained on the key data of your array. So before the foreach loop, you need to add a declaration statement $data = $request->input('data'); to get data in $data variable.

How to pass whole url as params?

I want allow user to scan QR code and go to website. This is search results with params in url address like
http://example.com/qr-code/http://example.org/search/user_id=12&model_id=4&role_id=8
My route is set as
Route::get('qr-code/{data?}/{size?}', 'QrController#qrCode')->name('qr-code');
so i hope my $data will get url:
http://example.org/search/user_id=12&model_id=4&role_id=8
My Controller has
public function qrCode(Request $request, $data = null, $size = 60) {
dd(88);
$data = (empty($data)) ? env('APP_URL') : $data;
return view('qr.code')->with('qr', QrCode::size($size)->generate($data));
}
But i can't even see dd(88) route redirect to main page.
How can i fix that ?
The thing is that Laravel doesn't know that you will provide an entire URL as a parameter. He could interpret everything like that:
http://example.com/qr-code/http:/ /search/user_id=12&model_id=4&role_id=8 . Try encoding the URL diffrently. Your $data will be "http:/" and your size will be 60.
Ok i took i added to helper
function base64url_encode($s) {
return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}
function base64url_decode($s) {
return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s));
}
And then i am getting URI from request on view like
Click me
From JS i send params to QrController
public function qrCode(Request $request, $data = null, $type = null, $size = 200) {
$data = (empty($data)) ? env('APP_URL') : $data;
if ($type == 'url') $data = env('APP_URL') . base64url_decode($data);
return view('qr.code')->with('qr', QrCode::size($size)->generate($data));
}
And works fine. Base64 has + or / sometimes so thats why need to replace both - and _ and second is i add base url from env to make whole address.
Thx

another "can't save multiple checkbox" and "must be of the type array, string given"

I have seen many same question about this problem but I can't figure out how to fix mine
so I'm trying to save multiple checkbox and I think this one will work but now I get "must be of the type array, string given".
By the way this is my controller
public function store(Request $request)
{
// $multi = Multi::create($request->only(['data'])->implode(', '));
// $multi = Multi::select('data')->implode();
$multis = implode(',', $request->get('data'));
$multis = Multi::create(['data' => $request->get('data')]);
return redirect()->route('multi.create')->with('success', 'berhasil.');
and this is my create.blade.php
#foreach($multis as $multi)
{{$multi['data']}}<br>
#endforeach
<br>----------------<br>
{{Form::open(['action'=>'MultiController#store'])}}
{{Form::checkbox('data[]','A')}}A<br>
{{Form::checkbox('data[]','B')}}B<br>
{{Form::checkbox('data[]','C')}}C<br>
{{Form::submit('TAMBAH')}}
{{Form::close()}}
Try this
$multis = implode(',', $request->data));
$multis = Multi::create(['data' => $multis]);
You have to add fields name as a key of array and define all the data into different variables like this this will work.
$multis1 = implode(',', $request->d);
$multis2 = implode(',', $request->i);
$multis3 = implode(',', $request->s);
$multis4 = implode(',', $request->c);
$multis= Test::create(array('d' => $multis1,'i' => $multis2,'s' => $multis3,'c' => $multis4));

how to use $request->all() on controller and blade view in laravel 5.5

in Controller
public function index(Request $request)
{
$search_cons = $request->all();
$search_con = $search_cons->name; //error place
return $search_cons.$search_con;
}
->name this place has the error
Trying to get property of non-object
Or in blade.view
<p>{{$search_cons->name}}</p> has the error
Trying to get property of non-object
But if I use
$search_cons=$request->input('name');
on controller
the blade view
<p>{{$search_cons}}</p> will work ok!
I want the $search=request->all() so I can freely use $search->name on my blade view
How can I fix the question?
PS: I tried $resquest('name') still not to work
Request::all() ->tell me the
When you do $request->all() it returns all the inputs submitted in array format. So in your case, you can do
$search_cons = $request->all(); // dd($search_cons) so you can see its structure
$search_con = $search_cons['name']; // instead of ->name since it's not an object anymore
And anyway, you can skip the $request->all() thing - you can actually just do this directly:
$request->name.
EDIT:
You can cast the array as an object using (object)
$search_cons = (object) $request->all();
this will still let you use $search_cons->name
$search_cons is an array, not an object:
$search_con = $search_cons['name']
public function index(Request $request)
{
$search_cons = $request->all(); //returns array
$search_con = $search_cons['name']; //error place
return $search_cons.$search_con;
}
Or you can do like this
request()->name //request() isa global helper function
Try
In Controller
public function index(Request $request)
{
$search = $request->all();
return ['search' => $search];
}
In Blade
<p>Name : {{$search['name'] ? $search['name'] : ''}} </p>

Resources