3

So, currently I am passing values stored in Database MySQL to View (using Controller). I do simple querying ModelName::where()->first();.

I have my data right now in View. I want to use that data in Ajax or Javascript code that I am writing.

I can have 46 values and one way to do this is to have <div id="field1"></div> for 46 times set div style to display:none in css and in Javascript use document.getElementById('field1'); to access the values and finally, do whatever I want to do with it.

But I find this quite long and un-necessary to do as there is no point of printing all the values in html first and then accessing it. How can I directly get {{$data}} in Javascript?

myCode

public function index(Request $request){

        $cattfs = Cattf::all();
        $cattts = Cattt::all();
        $cattos = Catto::all();

        return view('/index',compact('cattfs'));
}

View

Nothing in the view. and I prefer it to be none.

Javascript and Ajax

$(document).ready(function()
{
    init(); 
});

function init(){
    my_Date = new Date();
    var feedback = $.ajax({
        url:url,
        dataType: "JSON",
        type: "GET",
    }).success(function(data){
   console.log(data);
  //I have some data called data from url
  //I want some data from controller like: cattf,cattt,catto
  //I will combine url data and cattf and do simple arithmetic to it
  //finally output to the view.
  }).responseText;
}
8
  • Do you want to append elements after an ajax call or in the initial view setup? You may need to use a for loop in javascript. Get all the elements with classes and for loop them. While looping, set each one to .show() Commented Apr 8, 2017 at 22:21
  • @senty I don't seem to understand your question, can you rephrase it? If you mean appending html elements then no. Commented Apr 8, 2017 at 22:24
  • @senty Can you show what you mean? I get the part of using while loop so no repetition but then I dont want any {{$fields}} in html as there is no role those values in html View. Commented Apr 8, 2017 at 22:26
  • @foreach ($data) your html @endfoeach is what you need to do in the blade html. Assign them classes. Then depending on how you want to show them, you can do it in javascript Commented Apr 8, 2017 at 22:26
  • Ahhh.. I see what you mean. Let me check that Commented Apr 8, 2017 at 22:27

2 Answers 2

2

One good way would be to actually make a small API to get your data. Let's say you wanted to retrieve users.

In the api.php file in your route folder:

Route::get('/posts', function () {
    return Post::all();
});

and then you just need to use http://yourUrl.dev/api/posts as your URL sent in your .ajax() call to work with what you need.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks will try this.
0

I found best solution use this: https://github.com/laracasts/PHP-Vars-To-Js-Transformer

It takes values from controller directly to Javascript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.