Tracking clicks on Download button and save in database - laravel

I have simple Download button which is looks like this
<a download="{{ $thumb }}" href="{{ $thumb }}" class="btn btn-success">Download Image</a>
It is appearing on page when the image is found. The button is working perfectly and is downloading the image.
The page has simple input field which user can search the image and if image is found it's showed on page with button download.
I've made a function which saves each string which is searched and now I'm wondering if I can save also if button Download is clicked e.g. the image is downloaded.
Can someone show me an example? I'm using laravel 5.4 here. So maybe I need to pass to the controller click event?
Current controller function
public function getImage(Request $request)
{
$url = get_curl_content_tx('http://example.com/api?url='.$request->input('url'));
$items = json_decode($url, true);
$thumb = $items['thumbnail_url'];
$db_save = new Image();
$ip = $request->ip();
$ip = DB::connection()->getPdo()->quote($ip);
$db_save->url = $request->input('url');
$db_save->ip = DB::raw("inet_aton($ip)");
$db_save->save();
return view('getImage',compact('thumb'));
}

Yes. Make sure that the href is going to a route where you keep track of these records and you can pass on the specific image to update the Record Model of the Image model itself if the counter is there? Something like this:
public function TrackDownload(Image $image){
$imageRecord = Record::where('image_id', '=', $image->id);
$imageRecord->update([
'download_counter' => $imageRecord->download_counter + 1;
]);
return redirect()->route();
}

Related

How can I download pdf before loading another view in laravel?

after completing a registration formular, when user clicks submit button, I want to first download a pdf and then redirect user to a view. Here is my code :
public function formularSave(Request $request) {
if(!isset($_REQUEST['token'])) {
abort(404);
}
$token = $_REQUEST['token'];
$upd_app = Application::where('token', $token)->update([
'status' => 22
]);
$result = "Registration complete.";
$html .= 'Some test code here
<br>
<p>Date: '.date('d.m.Y H:i:s').'</p>
';
$pdf = PDF::loadHTML($html);
$filename = substr(md5(uniqid().time()), 0, 17) . '.pdf';
$pdf->save(storage_path().'/app/public/uploads/rezolutii/'.$filename);
//code for download pdf HERE!!!!
return view('site.pages.registercomplete', compact('result'));
}
How can I download the pdf, after I create it?
It's impossible on Laravel.
This will not work. Your browser operates on simple requests one goes out one comes in.
There is no way for browser to know if user finished downloading the file and saved it somewhere. The final response from browser if file to be downloaded, nothing can follow that as far as I understand.
Now I'm not sure how that can be handled in javascript but in pure html requests it will not work.
Check this https://laracasts.com/discuss/channels/laravel/redirect-after-download

Preview Link Inside Post Body Laravel

I planned on embedding URLs into a post body when the post is been created and also would like if the automatically preview details inside the post body after it has been submitted, URLs like YouTube , Twitter, Instagram I would like to automatically display there details inside a post body
please how can I do this?
using this package
https://github.com/oscarotero/Embed
preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $post->body, $urls);
So you need to do this process before passing the post body to the view also specify a section to show your data, like so
Your PHP Code
public function show (Post $post)
{
// Your pattern to find the URL that you want
$isMatch = preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $post->body, 'matches_urls');
$urlDetails = [];
foreach ($urlsThatYouFind as $url)
{
$embed = new Embed();
// Load any url
$info = $embed->get($url);
$urlDetails['info'] = $info;
// Get content info
$urlDetails['title'] = $info->title; //The page title
}
return view('post.view', compact('post', 'urlDetails'));
}
Your view
<p>{{ $post->body }}</p>
<div>{{ $urlDetails['title'] }}</div>

how to create button with two function save and displaying or print in pdf in laravel

i'd like to create button to:
saving the transaction.
print the transaction
how to do this using button in laravel. below is the transaction page
this is my code to saving the transaction
public function store(Request $request)
{
$detailtrans = new DetailTrans;
$detailtrans->cde_detailtrans = $request->cde_detailtrans;
$detailtrans->cde_customer = $request->cde_customer;
$detailtrans->cde_layanan = $request->cde_services;
$detailtrans->weight_laundry = $request->weight_laundry;
$detailtrans->tot_charge = $request->tot_charge;
$detailtrans->id = Auth::user()->id;
$detailtrans->save();
return Redirect::Route('detailtrans.create');
}
above code work fine to saving the transaction, but i do not know how to write code to print the transaction receipt.
you could just pass it to a view and show it with table or something and add a print button under that like this:
<div style="text-align:center">
<button onclick="window.print();">print</button>
</div>
this js code onclick="window.print();" prints the page to everything user wants.

Issue in displaying the data to view page in laravel, but the value is getting passed through url and its not getting displayed in the view page

Find below the controller code, Here I'm getting the data's passed in the url and I need the data entered through the form to be displayed / printed in the view page..
Controller Code:
public function create(Request $request)
{
$domainname = Input::get('domainname');
$tld = Input::get('tld');
$url='https://httpapi.com/api/domains/available.json?auth-userid=711757&api-key=74miSZPgYkkc108Ptmeo5Q8iDTtKk3xi&domain-name=';
$ch = curl_init($url.$domainname.'&tlds='.$tld);
711757&api-key=74miSZPgYkkc108Ptmeo5Q8iDTtKk3xi&domain-name='.$domainname.'&tlds='.$tld.'&suggest-alternative=true');
$result = curl_exec($ch);
$final_data = json_decode($result,true);
dd($final_data);
return view('clientlayout.main.tld',compact('final_data'));
}
tld.blade.php:
{{print_r($final_data)}}
When I use the above code to print the data in view page, it just prints as "11".
Route code:
Route::get('tld','TldController#create')->name('domain.create');
Kindly help me with this to solve the issue and print the data in the view page.
$dataFromForm = $request->FieldNameFromForm
This will allow you to get the data from the form, assuming you use a POST or GET request.
In the form named "greeting":
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
In controller:
return view('greeting', ['name' => 'James']);

Laravel 5.6 Like Button

I'm using this package here in Laravel 5.6 to add likes system in my project.
I have updated the models as per their documentation. However, I'm confused on how to use this package.
I have added tried the following which adds the logged in user to the particular article likes list when he visits the link.
public function show(ArticleCategory $articlecategory, $slug)
{
$categories = ArticleCategory::all();
$article = Article::where('slug', $slug)->first();
$user = User::first();
$user->addFavorite($article);
return view('articles.show', compact('article', 'categories'));
}
And in my user dashboard, I'm able to pull up all the articles which are liked by the user with
$user = Auth::user();
$favoritearticles = $user->favorite(Article::class);
But I'm looking for a functionality where I have a button on the article page where when a logged user clicks on it, he is added to the likes list. I haven't tried this before so stuck at this point.
I replaced
$user->addFavorite($article);
with
$user->toggleFavorite($article);
but that just toggles the favourite list. I mean when I visit the link once, the logged in user is added to the likes list. When I visit the link for the second time, the logged in user is removed from the likes list. The cycle is repeated.
Could anyone explain to me how to achieve the like functionality with a button?
you're almost there,
You have to add a button and on click you will trigger an AJAX request to the server to perform what you want without refreshing the page, here is an example:
First you'll add a button and give it an ID or class:
<button class="like">Like</button>
Then the moment you click on it, you'll call the url which you need to replace with the route to your function,
Then you have to declare a method like so:
public function like($slug)
{
$article = Article::where('slug', $slug)->first();
$user = \Auth::user(); //to get authenticated user...
$user->toggleFavorite($article); // toggle so if u already like it u remove it from the liked table
return response()->json(['status': 1])
}
And of course add the route to your routes.php:
Router::get('like/{slug}',"ArticleController#like");
then add the function (jQuery is used here) to hook the AJAX call
$('.like').on('click', function(){
$.ajax({
type: "GET",
url: 'wwww.example.com/articles/slug',
data: {slug: 'the slug'},
success: function(data){
alert('its done')
},
});
})
Create a form in you article page with a button
<form action="{{url('favorite/{$post->id}')}}" method="post">
#if($post->isFavorited())
<button type="submit">Remove from favorite</button>
#else
<button type="submit">Add to favorite</button>
#endif
</form>
create the favorite route and controller
Router::post('favorite/{id}',"ArticleController#toggleFavorite");
public function toggleFavorite($id) {
$article = ArticleCategory::find($id);//get the article based on the id
Auth::user()->toggleFavorite($article);//add/remove the user from the favorite list
return Redirect::to('article/{$id}');//redirect back (optionally with a message)
}

Resources