laravel link href click to save some data - laravel

Preview Your File
here link click to open pdf its fine but i want to insert some data when click this link how to insert and how to pass data laravel

You can do this in two methods
1: Using Ajax:
First insert your data then in the success redirect to your file .
2: using controller to redirect you to the file
use form to submit the data you want to controller then via controller redirect it to your file after adding data to database
something like this
public function AddDataToDatabase(Request $request)
{
$datas=New DataModal();
$datas->name = $request->name;
$datas->address = $request->address;
$datas->lastName = $request->lastName;
$datas->email = $request->email;
$data->save();
$items=Attachments::where('id',1)->get();
return redirect('Attachments/'.$items[0]->attachmentName);
}
or Using Ajax
$.ajax({
type: "POST",
url: your_URL,
data: {
'name':'your_name',
'id':'your_id',
'email':'your_email',
},
success: function(data) {
window.location = 'https://localhost/sample.pdf';
},
error: function(e) {
console.log(e);
}
});
then in your controller create a new method like this:
public function AddDataToDatabase(Request $request)
{
$datas=New DataModal();
$datas->name = $request->name;
$datas->address = $request->address;
$datas->lastName = $request->lastName;
$datas->email = $request->email;
$data->save();
return response()->json(['msg' => 'added successfully'],200);
}

Instead of a link, create form that contains the fields that you want to store in database. Make the fields hidden if they won't be filled by the user. Then submit the form to a controller. Store the data, redirect to a new path that will display the pdf.

Related

How do I pass a variable from blade file to controller in laravel?

I have ProjectController that fetches data from the database and passes it to a blade file. One of the data items is the project_id. I want to pass the project _id from the blade file to another controller BidController.
ProjectController.php
public function show($id)
{
$project = Project::find($id);
return view('project.show',['project'=>$project]);
}
show.blade.php
div class="card-header">PROJECT <p>{!! $project->id !!}</p></div>
BidController.php
public function store(Request $request)
{
$bid = new Bid;
$bid->project_id = $project_id;
dd($project_id);
}
The dd(); does not output the project_id. I need help in passing the project_id from the blade file to the BidController method.
You can't directly set a model's id like you're doing in the line $bid->id = $project_id;. Are you trying to set up a relationship? That should be more like $bid->project_id = $request->project_id;.
Blade templates can't really pass things back to controllers, once they're in the browser your app is sort-of finished running. You need to create an HTML link/request on the page (like a form post request) that'll request the next thing from your app when the user clicks it.
If you want to create a button that creates a new bid for an existing project, you could do something like set up a form with a hidden field of 'project_id' that posts back to '/bids' which goes to the route 'bids.store'. You'll find 'project_id' under $request->project-id'.
You can send an AJAX request from Javascript:
View
<script type="text/javascript">
var project_id= {!! json_encode($project->id) !!}
$.ajax({
type: 'POST',
url: url, //Your bidController route
data: {project_id: project_id},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown)
},
success: function()
{
console.log('successful')
}
});
</script>
This will sent the data to the controller asynchronously so the user experience doesn't get affected.
One extra point: In your Bid controller, as project_id is coming from the request, you'll have to use:
$bid->id = $request->project_id;
I hope it helps!
PS: I'm using JQuery for this, so you'll have to include it if you don't already have.
I think this will solve your problem :
ProjectController.php
public function show($id)
{
$project = Project::findOrFail($id);
return view('project.show',compact('project');
}
web.php
Route::post('/bids/store/{id}' , 'BidController#store')->name('bids.store');
show.blade.php
div class="card-header">PROJECT <p>{{$project->id}}</p></div>
<form action="{{route('bids.store', $project->id)}}" method="post">
BidController.php
public function store(Request $request, $id)
{
$bid = new Bid;
$bid->id = $id;
$bid->save();
dd($id);
}

Getting value from request in Laravel using ajax

I have this ajax method in PostsController
public function ajax(Request $request)
{
//dd($request);
$this->authorize('view', Post::class);
$posts = Post::orderBy("created_at","desc")->paginate(5);
$comments = Comment::all();
return response()->json(array("posts"=> $posts, "comments"=> $comments), 200);
}
which works great when you just getting data and sending it.
So i tried besides requesting data by ajax, to send some data alongside ajax request. How can i access that data inside controller?
Here is a method which resides inside certain blade:
function ajax(){
let var1 = "gg";
let var2 = "bruh";
let token = document.querySelector("meta[name='csrf-token']").getAttribute("content");
let url = '/posts';
$.ajax({
type: "POST",
url: url,
headers:
{
'X-CSRF-TOKEN': token
},
data: {
'var1': var1,
'var2': var2
},
success: function(data) {
console.log(data);
}
});
}
To simplify: How can i, dd() or dump(), given data(var1 & var2) by ajax function from blade in PostsController?
Here is route:
Route::post('/posts', "PostsController#ajax");
And here is some "gibberish" when i try to dd() it:
dd() is a laravel function and dump()for php. so you cannot use them from javaScript.
You cannot dd() or dump() from direct ajax request or JavaScript.
What you can do is, console log your data, or check from browser developer portion, network tab to see which data you are getting from the ajax response. You can find browser developer portion in,
for chrome:
Insepect > Network
for mozila:
Insepect Element > Network
If you are telling about get var1 and var2 on controller, you can just get them by $request->var1 and $request->var2.
Hasan05 was right. Just needed to know right direction. So to get data parameter of ajax request i modified ajax controller method:
public function ajax(Request $request)
{
$var1 = $request->input('var1');
$var2 = $request->input('var2');
$this->authorize('view', Post::class);
$posts = Post::orderBy("created_at","desc")->paginate(5);
$comments = Comment::all();
return response()->json(array("posts"=> $posts, "comments"=> $comments, "var1"=> $var1, "var2"=> $var2), 200);
}

Ajax data not getting into controller

I am using an ajax request to show some information, on my local development version it works perfectly, but on the production server (Ubuntu 16.04 LEMP) it fails in validation, because there is no data in the request.
Checks
The url is correctly showing (e.g. example.com/employeeInfo?employeeId=1)
Ajax itself is working: when I hard-code the controller's response everything is fine.
I cannot figure out why this happens in production, but not on the local version... Huge thanks for any clues!
View
<script>
(function ($) {
$(document).ready(function() {
$(".team-pic").off("click").on("click", function() {
var employeeId = $(this).data('id');
// Get data
$.ajax({
type: "GET",
url: "employeeInfo",
data: {employeeId:employeeId},
success: function(data){
var obj=$.parseJSON(data);
$('#team-info-title').html(obj.output_name);
$('#team-info-subtitle').html(obj.output_role);
$('#resume').html(obj.output_resume);
$('#linkedin').html(obj.output_linkedin);
$("#team-info-background").show();
$("#team-info").show();
}
});
});
});
}(jQuery));
</script>
Route
Route::get('/employeeInfo', 'EmployeeController#getInfo');
Controller
public function getInfo(Request $request) {
if($request->ajax()) {
$this->validate($request, [
'employeeId' => 'required|integer',
]);
$employee = Employee::find($request->employeeId);
$output_linkedin = '<i class="fab fa-linkedin"></i>';
$data = array("output_resume"=>$employee->resume,"output_linkedin"=>$output_linkedin, "output_name"=>$employee->name, "output_role"=>$employee->role);
echo json_encode($data);
}
}
If you want to pass a get data employeeId you have to pass a slug through your route either you should pass the data by POST method.
Route::get('/employeeInfo/{slug}', 'EmployeeController#getInfo');
And Get the slug on your function on controller .
public function getInfo($employeeId)

vuejs form data sent via api not visible on laravel controller

have a look
vuejs
data() {
return {
url: '/api/client/document/upload',
}
},
computed
attachment() {
return {
slug: 'testing',
test_type[enter image description here][1]: (this.individual === 1)? 'transfer_individual': 'transfer_corporate',
application_id: this.client_investment_id
};
upload method
upload: function () {
if(!this.validateForm()) {
Message.warning({ message: 'Complete the form and proceed' });
return;
}
if(!this.$refs.upload.uploadFiles[0]) {
Message.warning({ message: 'Upload the form and proceed' });
return;
}
console.log('data', this.attachment);
this.$refs.upload.submit();
},
controller side laravel
public function uploadDocument()
{
$input = request()->all();
dd($input);
}
there is a file am uploading to the given url;
when i dd from the controller i get an application_id of null but if i console.log before the submittion am able to view my data. what may i be doing wrong.
console.log output
dd output
Pass the request as parameter to the function and call the all() method on the request instance.
public function uploadDocument(Request $request)
{
$input = $request->all();
dd($input);
}
To take and save file with its name concatenated with a timestamp in public/images folder try
//Assuming you have the file input name as `photo`
if ($file = $request->file('photo')) {
$name = time() . $file->getClientOriginalName();
$file->move('images/', $name);
}
this is the form ...note its taking with it the attachment data from computed property..
<el-upload
class="upload-demo"
drag
:action=url
ref="upload"
:data=attachment
:headers="defaultHeaders"
:on-error="errorOnUpload"
:on-success="showUploadSuccess"
:accept=extensions
:thumbnail-mode="false"
:auto-upload="false">
</el-upload>
<el-button type="success" size="mini" #click="upload" class="el-icon-upload">upload</el-button>
i have noted that i set the application_id to be set to the store when client applies, but now am dealing with edit, its taking the default state of the application_id which is null

Dynamic data from an input text Laravel 5.4

I want to make the same action as the image below in one of my create forms:
I want to write the Id of the user and bring the name of it dynamically to put it in an input below, as is shown in the image.
How can I do that? I'm using laravel 5.4
use ajax request like this
$("#id_of_input").keyup(function(){
$.ajax({url: "http://example.com/api/name/" + $(this).val(), success: function(result){
$("#user_name").html(result);
}});
});
and add in web.php
Route::get('/api/name/{id}', function ($id) {
$user = User::find($id);
return $user ? $user->name : '';
});

Resources