Laravel 5.8 voyager translator not working when paginated - laravel-5.8

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;
}

Related

How to apply a pagination in laravel API?

I developed one API(displayBlogs) which is responsible for getting data from database ,i want to apply pagination for displayBlogs api,for that i installed require package but i am unable to apply a pagination on that query(if i don't use get() method then pagination working) ,please help me to fix this issue..
UserController.php
public function displayBlogs(){
$user = new BlogModel();
$token = JWTAuth::getToken();
$id = JWTAuth::getPayload($token)->toArray();
$user->user_id = $id["sub"];
return DB::table('blogs_table')->where('user_id', $user->user_id)->get()->paginate(3);
}
just use paginate only
public function displayBlogs(){
$user = new BlogModel();
$token = JWTAuth::getToken();
$id = JWTAuth::getPayload($token)->toArray();
$user->user_id = $id["sub"];
return DB::table('blogs_table')->where('user_id', $user->user_id)->paginate(3);
}

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>

Passing variable from DOMPDF controller to view

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>

Input array loop on controller laravel 5

I have inputs array and i need to make a foreach but laravel $request->all() only return last one:
url:
http://localhost:8000/api/ofertas?filter_pais=1&filter_pais=2&filter_pais=3
controller:
public function filtroOfertas(Request $request){
return $request->all();
}
result:
{"filter_pais":"3"}
result should return 1, 2 and 3 and i need to make a foreach in filter_pais.
Any solution? Thanks
Use [] at the key of query string.
http://localhost:8000/api/ofertas?filter_pais[]=1&filter_pais[]=2&filter_pais[]=3
It will be parsed as array.
Repeated parameters make no sense and should be avoided without exception.
But looking at other solutions, there are several:
routes.php
Route::get('/api/ofertas/{r}', 'Controller#index');
Controller:
public function index($r)
{
$query = explode('&', $r);
$params = array();
foreach($query as $param)
{
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
// $params contains all parameters
}
Given that the URL has no question marks:
http://localhost:8000/api/ofertas/filter_pais=1&filter_pais=2&filter_pais=3

Knp Paginator doesn't works with Query

I am settting up knp paginator, my composer is set to "knplabs/knp-paginator-bundle": "2.3.*" however It is not working for Query object.
Here is my query:
public function getCommentariesByUser(User $user) {
$qb = $this->createQueryBuilder('c');
$qb->innerJoin('c.item', 'i');
$qb->andWhere($qb->expr()->eq('i.user', ':user'))
->setParameter(':user', $user);
$qb->andWhere($qb->expr()->neq('c.user', ':user1'))
->setParameter(':user1', $user);
return $qb->getQuery();
}
How I am calling it in my action:
public function commentariesAction($page) {
$user = $this->getUser();
$commentaryRepository = $this->getDoctrine()->getRepository('My\MyBundle\Entity\Commentary');
$query = $commentaryRepository->getCommentariesByUser($user);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$page/*page number*/,
15/*limit per page*/
);
return array('pagination' => $pagination);
}
It simply doesn't show results, however If I put $query->execute() It works. What's wrong?
Currently this is possible to use next combination of paginator packages for code above to work:
"knplabs/knp-components": "1.2.1",
"knplabs/knp-paginator-bundle": "~2.3"

Resources