Cannot get thumbnail in rss feed in laravel - laravel

I am using willvincent feed reader to parse rss feeds, But i cannot seem to get the thumbail of the images,
Here is my code
Route::get('feed', function(Request $request) {
$f = FeedsFacade::make('http://www.cbn.com/cbnnews/us/feed/');
// $results = [
// 'image' => $f->get_image_url(),
// ];
foreach($f->get_items(0, $f->get_item_quantity()) as $item) {
$i['title'] = $item->get_title();
$i['thumbnail'] = $item->get_thumbnail();
$i['description'] = $item->get_description();
$i['content'] = $item->get_content();
$i['link'] = $item->get_link();
$i['date'] = $item->get_date();
$results['items'][] = $i;
}
dd($results);
})->name('feed');
Thumbnail always return null, will appreciate anyone's help

Here is how we call image and other contents from rss feed
NOTE: To get image, it is important that the site you fetching rss feeds, use to provide image too in their feed. Else won't get image in your fetched feed (there might be some trick to still fetch image but I am not aware of it for now). For example, google news feed don't provide image so you won't get image from google news feed.
Here is example of one site who use to provide image.
//In your CONTROLLER file
$feed = Feeds::make('https://globalnews.ca/feed/');
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return view('view_file', $data)
//OR in case you are calling array value to show on view then it would be like this
return view('view_file', array(
'name' => $var
), $data) //notice "$data" at end of array.
Now after done on controller part, this is how you will call in view file
#foreach ($items as $item)
//GET IMAGE
#if($enclosure = $item->get_enclosure())
<img src="{{$enclosure->get_thumbnail()}}">
#endif
//GET TITLE
<div class="news-title">{{ $item->get_title() }}</div>
//GET DESCRIPTION
{{$item->get_description()}}
//NOTE: this will bring html. TO show as output instead of html you can either use {!! !!) or "strip_tags" function. And "substr" function to show first 100 characters only as small description
{{ substr(strip_tags($item->get_description()), 0, 100) }}
//GET LINK/URL OF NEWS
Read More
#endforeach
Feed which whose example I shown and also reference
https://packagist.org/packages/willvincent/feeds

Related

Second page of paginate won't display the filtered data

I'm want to use paginate in laravel but didn't work perfectly. When i paginate the filtered data it worked fine, but only for the first page.The problem is when i moved to the second page, it will display the second page of all data not the filtered data. i want it so the second page will display the second page of filtered data
in blade.php I'm using links to call the paginate(for moving page)
<div class="paginate">
{{ $data->links() }}
</div>
Here's the filter controller
function filter(Request $request){
$data = profiles::when($request->has('pNamaLengkap'), function($query) use ($request){
$query->where('pNamaLengkap','like','%'.$request->pNamaLengkap.'%');
});
if($request->pJobDescription != 'All'){
$data = $data->where('pJobDescription','like','%'.$request->pJobDescription.'%');
}
if($request->pUnitKerja != 'All'){
$data = $data->where('pUnitKerja','like','%'.$request->pUnitKerja.'%');
}
if($request->pDirectorate != 'All'){
$data = $data->where('pDirectorate','like','%'.$request->pDirectorate.'%');
}
if($request->pRank != 'All'){
$data = $data->where('pRank','like','%'.$request->pRank.'%');
}
return view('export', [
'data' => $data->paginate(5),
'jobdescription' => jobdes::all(),
'unitkerja' => unitkerja::all(),
'direktorat' => direktorat::all(),
'rank' => rank::all(),
'oldjob' => $request->pJobDescription,
'oldunit' => $request->pUnitKerja,
'olddir' => $request->pDirectorate,
'oldrank' => $request->pRank,
'oldname' => $request->pNamaLengkap
]);
// return redirect('export')->with([
// 'data' => $data,
// 'jobdescription' => jobdes::all(),
// 'unitkerja' => unitkerja::all(),
// 'direktorat' => direktorat::all()
// ]);
}
I'll make an example from a picture
Filtered JobDescription Page 1
Page 2 that reset the filter and display all data
in the URL parameter it change it from
http://127.0.0.1:8000/filterexport?pNamaLengkap=&pJobDescription=Full+Stack+Developer&pUnitKerja=All&pDirectorate=All&pRank=All&export=Filter
into
http://127.0.0.1:8000/filterexport?page=2
for extra note, the paginate is working but just didn't work when you want to view the second page because somehow it reset the input filter
Thank you for reading this page, i really need help in this one
$data->paginate(15)->withQueryString();
use withQueryStrings() method with paginated data.
another solution is replacing
{{ $data->links() }}
to the
{{ $data->appends(Request::except('page'))->links() }}
For lower versions of Laravel, you can use this:
{!! str_replace('/?', '?', $data->appends(Input::except('page'))->render()) !!}

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.

How to display multiple images that stored as array in laravel?

please help me to display image that stored as array in database
my controller:
( filename is my image column name in database)
public function store(Request $request)
{
$this->validate($request, [
'filename' => 'required',
'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
// Start multiple image upload code
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
// End multiple image upload code
$houses= new House();
$houses->division = $request->input('division');
$houses->city = $request->input('city');
$houses->area = $request->input('area');
$houses->owner_name = $request->input('owner_name');
$houses->house_name = $request->input('house_name');
$houses->type = $request->input('type');
$houses->from = $request->input('from');
$houses->rent = $request->input('rent');
$houses->phone = $request->input('phone');
$houses->address = $request->input('address');
$houses->description = $request->input('description');
$houses->filename=json_encode($data); **// This for image upload**
$houses->save();
return back()->with('success', 'Your House has been successfully');
}
image column
First array cast the filename column in your model. For more: Array & JSON Casting
protected $casts = [
'filename' => 'array',
];
After that, whenever you fetch filename from House model, laravel will automatically convert the json data to array. Loop through the array and display the images.
$house = House::find(1);
foreach($house->filename as $filename) {
echo public_path().'/images/', $filename;
}
If you want to retrieve then display the images stored as an array what I'd suggest is casting the filename attribute to json/array in the model.
You can do that in your House model like this:
class House extends Model
{
protected $casts = [
'filename' => 'array',
];
}
To display it in a view you can loop through the casted array:
#foreach($house->filename as $image)
<img src="{{ url('link/to/assets/') . $image }}"
#endforeach
See more in the docs about casting here:
https://laravel.com/docs/5.7/eloquent-mutators#array-and-json-casting
A side point on image uploads, you may want to give uploaded files a unique name rather than using the user's filename so there's no conflict if a user uploads a document with the same name twice.
Laravel handles this all for you in the store() method seen here: https://laravel.com/docs/5.7/requests#storing-uploaded-files. You can create your own names if preferred using storeAs().

Youtube video in laravel

I'm using Laravel Video Embed(https://github.com/Merujan99/laravel-video-embed) so i can get the embeded code from a youtube url.It works just fine but i want to know is there any possibility to change the width and height properties of the embeded video in the view ?
my code in the controller:
public static function getvideo($id)
{
$url = Blog::where('id',$id)->value('video_link');
return LaravelVideoEmbed::parse($url);
}
my view:
<div>
{!!App\Http\Controllers\HomeController::getvideo($blog->id)!!}
</div>
You can pass attributes to the embed like so:
$params = [];
$attributes = [
'height' => 100px,
'width' => 200px,
];
LaravelVideoEmbed::parse($url, ['YouTube'], $params, $attributes)
Which will work dynamically.

Goutte Crawler convert JSON

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

Resources