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.
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.
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.
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
}
}
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);
}
}
Related
I am trying to execute simple CRUD operation using laravel. but it gives an error code 500 , when I try to fetch data from table either by laravel framework as well as with plain PHP.
Here is my controller class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = DB::select('select * from book');
$data = BookModel::all();
echo($data);
return compact('data');
}
}
axiom , which is been used. ---> "https://unpkg.com/axios/dist/axios.min.js"
Model Class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BookModel extends Model {
protected $table = "book";
public $timestamps = false;
}
It is returning no result from the table.
It worked after I changed my controller to
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return $data;
}
}
But it was giving null result when I was doing something like below
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\BookModel;
use \DB;
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin')->withTasks($data);
}
}
Can someone explain why it was not working earlier?
Anyways , Thanks everyone for your help.
Cheeeeeeeeerrsssss!!!!!!!
You need to add response() to return data without view.
Like below the code returns the JSON data from your BookModel that sometimes we need through the ajax request like axios.
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return response()->toJson($data);
}
}
With view you can do the following:
class AdminController extends Controller
{
function getItems()
{
$data = BookModel::all();
return view('admin', compact('data'));
}
}
Where you can access the BookModel data through $data variable in your admin.blade.php
I just starting to learn laravel and i don't know how to pass data on controller
this is my routes.php
Route::get('/', 'view#index');
Route::get('/create', 'view#create');
Route::get('/store', 'view#store');
Route::post('/destroy', 'view#destroy');
and this is my controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Request;
class view extends Controller {
public function index()
{
echo $data = User::all();
}
public function create()
{
$user = User::create(['first_Name'=>'abc']);
echo "id =".$user->id." is created";
}
public function store()
{
echo "store";
}
/* here i want to get id using post request */
public function destroy(Request $request)
{
$id = $request->input('id');
echo $id;
}
}
now I want to pass 'id' using postman in post request. how can i perform this. my get request working perfectly
In Postman, you can fill the request body:
The id key corresponds to the 'id' argument inside $request->input('id');
Note: My example uses DELETE, which is common practice for destroy/delete actions. In your case, it should be POST. I do, however, advice you to use DELETE.
I am trying to fetch data from database in Laravel using Model binding in Controller but it returns empty $task array instead
here is my routes.php :
<?php
Route::get('/tasks','TasksController#index');
Route::get('/tasks/{task}','TasksController#show');
and this the TasksController :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
class TasksController extends Controller
{
public function show(Task $task)
{
return $task;
}
}
Route::get('/tasks/{id}','TasksController#show');
in route model binding you can pass the id in the route and handle it in the show function
public function show($id)
{
$task = Task::find($id);
return $task;
}
This will directly return the task
Without seeing other code it's hard to answer however your function index() should look similar to this:
public function index(Task $task)
{
$tasks = $task->all();
return view('page', compact('tasks'));
}
Now, assuming you're gathering the ID's via a foreach loop your controller code should look like this:
public function show($id, Task $task)
{
$task = $task->find($id);
return view('somePage', compact('task'));
}
This will then pass $task back to a page of your choosing to display the specific task you are getting.
Your Routing is fine providing you're passing the:
Something
back within the blade.
Routes.php
use Illuminate\Http\Request;
Route::get('/age/{val}','AgeController#store');
AgeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class AgeController extends Controller
{
//
public function store(Request $request)
{
$data = $request->input('val');
var_dump($data);
}
}
My url is localhost/lara/public/age/20.
but
output:array(0) { }
When I change $request->input('val',500);
then output: int(500)
How to get 20 (val) in controller? Why array empty?
If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies. Add $val as the 2nd parameter in you store function. In your case it should be
public function store (Request $request,$val){dd($val);}
i use this code for return articles but not run this code and return blank,
how to solve this problem?
namespace App\Http\Controllers;
use App\taxonomy;
use Illuminate\Http\Request;
class newPostController extends Controller {
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
first time:
for next record:
Try this:
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
Update
You're getting empty result seconds time, because first time you're sending info through request, so $request has some data. If you reload the page, you do not send any info, that's why it's empty. It's just how it works.
Usually, when you're using $request data, you want to store it in DB or somehting and then redirect somewhere, for example, return $redirect->back(); instead of your return clause.
Try this
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
add this line in your class
use Illuminate\Http\Request;
You need to inject the Request into your method:
namespace App\Http\Controllers;
use App\taxonomy;
use Illuminate\Http\Request;
class newPostController extends Controller {
public function submitArticle(Request $request){
$name = $request->input();
return $name;
}