Goutte Crawler convert JSON - laravel

https://api.cinelist.co.uk/get/times/cinema/10565
This link gives me a json list of title and times that the cinema is playing that specific film. I want to get that information and convert it to string however how is that possible? I have used json_decode and it says that node list is empty.
Here's my code:
function odeon(){
$client = new Client();
$crawler = $client->request('GET', 'https://api.cinelist.co.uk/get/times/cinema/10565');
//print $crawler->text()."\n";
json_decode($crawler);
print_r($crawler);
}

file_get_contents() does the trick for simple things like these.
function odeon(){
$data = file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data = json_decode($data);
return view()->make('odeon')->with(['listings' => $data->listings]);
}
and then in your blade simply do something like this:
#foreach($listings as $listing)
<strong>{{$listing->title}} </strong>:
#foreach($listing->times as $time)
<p>{{$time}}</p>
#endforeach
#endforeach

Related

Laravel Mutator for append url for JSON array

I have a JSON field in my MySQL table column which has an JSON array with part of URLs.
["products/1.jpg", "products/2.jpg", "products/3.jpg"]
I want to get the array with appending a Base URL for each of the values of the array.
["www.example.com/images/products/1.jpg", "www.example.com/images/products/2.jpg", "www.example.com/images/products/3.jpg"]
I have tried with getAttribute() function like nelow code. But was not succeeded.
public function getImagesAttribute(){
$images = json_decode($this->attributes['images']);
$imageP = [];
foreach ($images as $image) {
$imageP[] = "www.example.com/images/" . $image;
}
return $imageP;
}
can you help me.
You probably want to use $this->images instead of $this->attributes['images'].
In this case, I would use Collections like so:
public function getImagesAttribute(){
return collect(json_decode($this->images))
->map(function ($image) {
return "www.example.com/images/" . $image;
})
->all();
}

How to post and receive data on my Laravel API with Guzzle

I have a very awkward issue here:
I am trying to call my own Laravel API like this:
$api_url `enter code here`= env("API_URL")."login";
$data= json_encode($data);
$api_url = env("API_URL")."login";
$client = new \GuzzleHttp\Client();
$headers = array('Accept: application/json','Content-Type: application/json', 'X-XSRF-TOKEN' => csrf_token());
$response = $client->request('POST', $api_url , ['headers'=>$headers,'json' => $data]);
print_r($response);
Then my API end point has code like this:
public function login(Request $request)
{
//This line gives me NULL
dump($request->all());
//the lines below print this "{"email":"dev#gumption.pk","password":"password01"}"
//but I cannot get this POSTED data in variables even with $request->input('email')
$content = $request->getContent();
print_r($content);
}
I am NOT able to retrieve variables from this POSTED data. I have tried $request->input('email');
$request->json('email');
Nothing is working. It simply says NULL.
I am not sure what is wrong here.

How to display data from all tables in sidebar?

I have a problem with displaying data from tables, and more accurately displays me a json object.
How can I fix this because I need to display only the titles.
I tried json_decode, but throws out an error that a string was expected, an array was passed
AppServiceProvider:
view()->composer('*', function($view){
$results = [];
$results['zone'] = Zone::all()->last();
$results['tourism'] = Tourism::all()->last();
$results['business'] = Business::all()->last();
$results['culture'] = Culture::all()->last();
$results['education'] = Education::all()->last();
$results['inhabitant'] = Inhabitant::all()->last();
$results['investor'] = Investor::all()->last();
$results['senior'] = Senior::all()->last();
$results['sport'] = Sport::all()->last();
$view->with('sidebar', $results);
});
Sidebar.blade.php:
#foreach($sidebar as $type => $list)
#include('pages.sidebar-components.'. $type, ['items' => $list])
#endforeach
Forexample one sidebar-component view (Business.blade.php):
{{ $list }}
Results:
{"id":1,"title":"test","description":"test","path":"dsadsa"}
I need:
{id:1,title:"test",description:"test",path:"dsadsa"}
I've tried to decode json, but I get the error of the expected string, passed array
O my God...
I just removed ->last(); from Sport::all()->last(); and it worked. Solved, to close.

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>

How to pass a view and a json from a function in laravel?

This is my function
if(isset($_POST['franchisesIds'])) {
$id_array = array();
foreach($_POST['franchisesIds'] as $data) {
array_push($id_array, (int)$data['id']);
}
$results = DB::table('franchises')->whereIn('id', $id_array)->get();
}
return Response::json(array($id_array));
return View::make('frontend.stores')->with('franchisesAll', $results);
So I am a little bit confused on how to pass all this data. I need to pass the json just to make sure everything worked. And at the same time I need to pass a list of ids to the view.
How can I do this??
Hopefully this is what you wanted :
Please don't use directly $_POST or $_GET instead use Input
$franchisesIds = Input::get('franchisesIds');
$id_array = array();
if($franchisesIds) {
foreach( $franchisesIds as $data) {
array_push($id_array, (int)$data['id']);
}
$results = DB::table('franchises')->whereIn('id', $id_array)->get();
}
$jsonArray = json_encode($id_array);
return View::make('frontend.stores')->with(array('franchisesAll'=>$results,'idArrays'=>$jsonArray));
In order to pass multiple values to the view, please read more about it in the official Laravel documentation
First of all you should use Input::get('franchisesIds') instead of $_POST['franchisesIds'], also there is no reason to do this foreach loop:
foreach($_POST['franchisesIds'] as $data) {
array_push($id_array, (int)$data['id']);
}
Because this is already an array and you are bulding another array from this array, makes no sense. So you may try this instead:
if($franchisesIds = Input::get('franchisesIds')) {
$franchises = DB::table('franchises')->whereIn('id', $franchisesIds)->get();
}
Then to pass both $franchisesIds and result to your view you may use this:
return View::make('frontend.stores')
->with('franchises', $franchises)
->with('franchisesIds', $franchisesIds);
You can also use something like this (compact):
return View::make('frontend.stores', compact('franchises', 'franchisesIds'));
There is no reason to use json_encode to encode your $franchisesIds.
You could also use
$results = DB::table('franchises')
->whereIn('id', $id_array)
->get()
->toJson();

Resources