Let's say i have data coming from my controller through Javascript.
So the data is paginated and i want display pagination result inside the javascript. I mean...I want to use the links() method inside javascript.
Controller
$schedules= DB::table('schedules')
->paginate(4);
Javascript,
success:function(schedules)
{
console.log(schedules);
$('#table').append(' <ul class="pager">'+{{ $schedules>links() }}+'</ul>');
})
But unfortunately not working, any help guys?
Push html paginate results from the controller, Enter the javascript code in the blade file, maybe
//controller
$paginate = '';
$paginate .= $datas->links();
//JS code in the blade file
<script type="text/javascript">
let paginate = '<div>';
paginate += {!! json_encode($paginate) !!}
paginate += '</div>'
consolelog(paginate)
</script>
You have an error in your blade syntax {{ }}:
$('#table').append(' <ul class="pager">'+{{ + $schedules>links() }}+'</ul>');
Remove the extra plus + icon before + $schedules>links() like so:
$('#table').append(' <ul class="pager">'+{{ $schedules>links() }}+'</ul>');
I think you can just convert the collection to a JSON.
Try this one:
Controller
$schedules= DB::table('schedules')
->paginate(4)->toArray();
return response()->json($schedules);
Javascript Ajax
success:function(schedules)
{
console.log(schedules);
$('#table').append(' <ul class="pager">'+schedules.links+'</ul>');
})
But in what I see, you're using the schedules also on your blade. In that case you can have a different function called for the ajax in your controller and different route.
Hope it helps!
Related
I sent a ajax request to get data from controller with json format. something like this-
return response()->json($data);
I received the response from laravel view with a variable like this-
var rpr_data_id = response.id;
Now I want to pass this variable to one of my data. Button is written in this format-
<button>send</button>
Question: how to pass the js variable in laravel blade anchor tag?
You can use the attr() method for that.
Assuming your link can be easily selected:
<a id="your-anchor-id" href="{{ url('rpr/') }}">send</a>
$('#your-anchor-id').attr('href', $('#your-anchor-id').attr('href') + response.id)
On a side note, you shouldn't put a <button> inside an <a>.
I try to pass JavaScript to url_for which is inside innerhtml using the below code:
var data = 'abc';
mydiv.innerHTML =
"<button onclick=window.location.href='{{ url_for('flask_function', filepath="+data+") }}'></button>
But the result when I print out the data in Python is like this:
+data+
So how can I pass my data to Flask using this code?
Would like to put this in comment.
You missed an " at the end.
Also otherwise this will not work because this is rendered in Jinja template python while generating code that has to be sent of the frontend. So it does not recognize js variables.
You will have to use a python variable with jinja template instead
{% set data = 'abc' %}
Or send the data in template and use
{{data}}
you can use like this worked for me
var actualUrl = "/flask_function/" + id + ""
post.innerHTML =''
I have a VUEX store and a blog post that I need to output.
I use CKEditor for the input but I can't output the raw HTML with the #{{ }} in the front-end.
I have already tried to use #{{!! !!}} but apparently these doesn't work together.
This is the line that I output:
<div>#{{ $store.state.item.conteudo }}</div>
And this is the actual output of the code:
<p><strong>Teste Laravel</strong></p>
You're using double bracket so, You should try {!! !!} instead of {{!! !!}}.
So now your variable looks like.
{!! $text !!}
Check more info. displaying data
you can also do like this by using getter in your model
Hope it help!
public function getConteudoAttribute($value) {
return html_entity_decode($value);
}
I'm working on a laravel project and am trying to pass an array from a controller to javascript. The following code is from my controller.
$dicomfilearray = $dicom->pluck('filename')->toArray();
return view('xray.view')->withDicomfilearray($dicomfilearray);
And below is the Javascript in that's in the blade file I'm trying to pass it to.
var dicomarray = '{{ json_encode($dicomfilearray) }}';
console.log(dicomarray);
And the following is a log result from the Javascript.
["storage/uploads/storeid1/27/10/dicom/c4p4Oco3rU.dcm","storage/uploads/storeid1/27/10/dicom/RNil0NPPzQ.dcm"]
I would like to get a list from this array. Any advice or guidance on this would be greatly appreciated, Thanks.
You can make ajax call in frotend, and backend do like this
$dicomfilearray = json_encode($dicom->pluck('filename'))->toArray());
return view('xray.view')->withDicomfilearray($dicomfilearray);
when you working in javascript and need data in javascript then why you need view part. Actually, I just read your comment.
If in Ajax
so I suggest send array with json_encode and populate that data into view with javascript.
simply right below in controller
response()->json(['status'=>200,'data'=>$dicomfilearray])
Update
So ,you not sending ajax request so, simply. do like below.
controller:-
$data = json_encode($dicomfilearray);
return view('your-view',compact('data'));
javascript
var dicomarray = '{{ $data }}';
You can do something like this and this even works if you want to pass the variable to external javascript file. All you have to do is to call the init function with passed parameters.
<script>
$(function () {
init({{ json_encode($dicomfilearray) }} });
function init(dicomfilearray){
//use your variable here
}
</script>
How I can get the current user in JS/Jquery? In the blade we can do like
{{Auth::user()}} But it wont work in the .js file.
As per looking at the standard and the way most javascript templates engine work, I would prefer to do something like this:
Install laracasts/utilities by using composer require laracasts/utilities
In the controller method, from where you are returning view, I would make it look like this:
public function returnUser(){
$user = Auth::user();
Javascript::put([ 'user.name' => $user->name, 'email' => $user->email ]);
return view('my.user.js');
}
In the blade file, I would simply do,
<script>alert("Hi " + user.name + ". Your email is " + user.email)</script>
And yeah, I would even prefer the way, #Robbin said. And yeah just one more edit to his answer, in laravel 5.1, Auth::user() should not be used. It should be used as auth()->user() //->name or ->email or ->anything.
You have to build an API and get it with AJAX. You cannot use blade in javascript files.
Or, in the <head> of your template, you place.
<script>
var user = {!! json_encode((array)auth()->user()) !!};
</script>
<!-- include your js file here-->
And use the user var in your js file.
EDIT:
As per Mark's comment this is indeed cleaner:
<script>
var user = {!! auth()->user()->toJson() !!};
</script>
<!-- include your js file here-->
simply =>
add any input or any tag to inject auth-user inside it, in my case:
< meta name="auth-check" content="{{ (Auth::check()) ? 'true' : 'false' }}">
in JS file u can get a value from your tag using jquery!
var authcheck = $('meta[name="auth-check"]').attr('content');
I hope my idea is useful, my best wishes