Deepjyoti
Deepjyoti

Reputation: 35

how to print variable from laravel controller?

I am calling a php file using in laravel controller. While calling the file I passing few data using ajax call. I want to print the variable that is passed to the controller file. Below is my code-

function handleJSON(originePlaceID, destPlaceID){
                var elements = document.getElementsByClassName('waypoints'); //getting the values from input field
                var wayptArray = new Array();
                for(var i = 0; i < elements.length; i++){
                    if(elements[i].value != "") {
                        wayptArray.push(elements[i].value);
                        }
                    }
                var JsonWaypt = JSON.stringify(wayptArray);
                $.ajax({
                    type: "GET",
                    url: "placeIdApi",
                    data: {data : JsonWaypt, _token: '{{csrf_token()}}'}, 
                    cache: false,
                    success: function(places){
                        console.log(places);
                    }
                });
                }

My web.php file:

Route::get('/placeIdApi', 'placeId@index');

My controller file(placeId.php)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class placeId extends Controller
{
     public function index(Request $request){
        $values = $request->data; //I want to print this $values variable.

    }
}

Can anyone suggest me how to print this $request or $values variable? Thanks in advance.

Upvotes: 2

Views: 17526

Answers (4)

Jaime Rojas
Jaime Rojas

Reputation: 559

For your purposes, the dd method would definitely be the best, so something like this should work:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class placeId extends Controller
{
    public function index(Request $request){
    $values = $request->data;
    //Print the values
    dd($values);
}

To see the result of this print, open up your Browser's console and go to the Networking section. Check the packet related to your request. The dd content should be printed into the preview for your request packet.

Upvotes: 3

matt
matt

Reputation: 700

If you are trying to output the $values value as JSON to use in your AJAX request, it might be best to do something like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class placeId extends Controller
{
     public function index(Request $request){
        $values = $request->data; //I want to print this $values variable.
        return response()->json($values);
    }
}

That will allow your js function to use the data that is return in the response.

To just print out the value as others have suggested, you can use the dd function:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class placeId extends Controller
{
     public function index(Request $request){
        $values = $request->data; //I want to print this $values variable.
        dd($values);
    }
}

Then you can navigate to your defined route http://example.com/placeIdApi to see the echo'd out data.

Upvotes: 4

Saad Bhutto
Saad Bhutto

Reputation: 604

you can use dump method

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class placeId extends Controller
{
     public function index(Request $request){
        $values = $request->data; //I want to print this $values variable.

        dump($values); //  dump $values
       //OR JSON RESPONSE
        return response()->json($values);


    }
}

Upvotes: 0

Giovanni S
Giovanni S

Reputation: 2110

You can dump $value like this:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class placeId extends Controller
{
     public function index(Request $request){
        $values = $request->data; //I want to print this $values variable.

        dd($values); // die and dump $values

    }
}

Upvotes: 0

Related Questions