Retrieve json data through route in Laravel 5.3 - laravel

I am using Laravel 5.3 for my API and my frontend is not included in my view of Laravel. My frontend is on 80 port whereas my API is on 8080 port so whenever I want to make a communication in between I will call Laravel API.
I don't know how to retrive the requested JSON data in Laravel using post route and I want to return the same data in response just to check whether it is working fine or not.
so here is my route and controller(please guide me if i went wrong in my code):
Route::group(['middleware' => ['api','cors'],'prefix' => 'api'], function () {
Route::get('inquiry', 'inquiryController#store');
});
and controller is:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class inquiryController extends Controller
{
public function store(Request $request)
{
$data=$requst->json->all();
return response()->json([$data]);
}
}
But this code is not working properly. How can I solve this?

Try to do like this
public function store(Request $request)
{
$data = $request->all();
return response()->json($data);
}

Related

Laravel 8 (version 8.35.1): Target class does not exist

I'm using laravel 8.35.1 version. I have a api-resource controller "ProductController". At my route file api.php. I define the route this:
api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', 'App\Http\Controllers\ProductController');
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{product}/review', 'App\Http\Controllers\ReviewController');
});
NOTE:
It's work fine but, when i remove the complete path of controller like just write Route::apiResource('/products', 'ProductController'); it show error
Target class [ProductController] does not exist.
Before first clearing the cache. I want to get rid of the complete path. and second want to place the controllers in Api folder, so how to define route for that also.
I have also tried ProductController::class but not work fine
Updated
when I use the route according the laravel 8 doc. https://laravel.com/docs/8.x/controllers#resource-controllers it is working fine. but when move the controller file to Api folder then declare the route name space like use App\Http\Controllers\Api\ProductController; show error again
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ReviewController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', ProductController::class);
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{products}/reviews', ReviewController::class);
});
You dont have to use class on declare controller in routing.
You may change 'ProductController' instead ProductController::class
When i try this then it work fine for me.
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReviewController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', 'App\Http\Controllers\ProductController');
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{product}/reviews', [ReviewController::class, 'ReviewController']);
});

How to get User()->id in Controllers (Laravel 8+)

I am trying to select tasks by user('id'), but I can't get it in a Controller, where I selecting data from DB.
I have tried many thing and some of them from stackoverflow, but it isn't working.
I tried:
1. $userId = Auth::check() ? Auth::id() : true;
2. Auth::user()->id;
3. public function getUserId(){
on Model} - and then get this value on Controllers
and some other things
I have the simplest code:
I installed registration: npm artisan ui --auth something like that
I installed vuejs (on Laravel)
I created api on Laravel, and some logic on vue
I didn't touch "app.blade.php" it's the same as it was.
I can get data, user: name, id and all what I want in file "app.blade.php" but I need those data in folder->file: App\Http\Controllers{{SomeController}}, but I don't know how.
Was someone in this situation?
How can I get user id in Controllers?
Thanks guys for earlier.
If you need user id, just use one of this :
auth()->id();
using Auth facade's
\Auth::id();
or, using Request instance
$request->user()->id
Follow this simple controller code, i showed 3 different way here :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class SomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getUserId(Request $request)
{
$user = Auth::user(); // Retrieve the currently authenticated user...
$id = Auth::id(); // Retrieve the currently authenticated user's ID...
$user = $request->user(); // returns an instance of the authenticated user...
$id = $request->user()->id; // Retrieve the currently authenticated user's ID...
$user = auth()->user(); // Retrieve the currently authenticated user...
$id = auth()->id(); // Retrieve the currently authenticated user's ID...
}
}
Auth::user()->id;
This should work if you have Auth middleware on that controller method where you try to get it, please check do you added that middleware.
For checking you can use php arisan route:list command.
Is someone still searching an answer on this question. I have some explanation how can you do this.
Laravel has a Router which routes authorization process through that Controller which you want, so you should redirect that process on your Router and in Controller create constructor which allows you to take user id.
How can you do that?:
1. First of all you should find Controller and Route which responsible for authorization and registration users.
In my case it was:
a)App\Http\Controllers\HomeController
b)routes\web.php
2. Second, you should redirect your authorization Router to the Controller where you trying to get Auth::id();
In my case it was:
App\Http\Controllers\TasksController
so, in routes\web.php I did this:
//was
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
//was
Auth::routes();
//was
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
//added
Auth::routes();
//added
Route::get('/home', [App\Http\Controllers\TasksController::class, 'index'])->name('home');
perhaps you should have index function on that controller
3. Third you should add constructor in your controller where you want to get user id, this constructor I took from HomeController, it already was there.
In my case it was:
public function __construct()
{
$this->middleware('auth');
}
code with function on my TasksController:
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$userId = Auth::check() ? Auth::id() : true;
}
And now I can take user id.
PS: constructor I added on the top in class TasksController in Controller

Laravel api routes not working, it's always redirecting

I am having an issue with laravel doing my backend Api.
What I am trying to do is return logged user data with Auth::user();
I wrote the controller, the route for the controller method in api.php but when I try to go to the /api/route I am redirected to Home.
I also tried to remove the __construct() method from the controller, When I do this and try to go to the /api/route, dd($user) is returning null.
My controller:
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Resources\TeacherResource;
use Illuminate\Support\Facades\Auth;
class teacherApiController extends Controller
{
public function __construct(){
$this->middleware('auth');
}
public function information(){
$user = Auth::user();
dd($user);
}
}
Api.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('information', 'API\teacherApiController#information');
Anything I am doing wrong?
I am using the laravel authentication for register and login users.
I suppose you are not authenticated before calling this route that's why when the __construct was there it redirected you to the home, that's also why Auth::user() returns you null.
You are probably not authenticated before calling this route.
To manage the authentication you used laravel passport or even used JWT

How to pass data to controller

I intend to pass ip received in User.php to my HomeController. How can I do this?
$ip = Request::ip();
Now I'm looking for the way to pass it to the controller. Then I plan to broadcast it.
Request IP in laravel using controller
You should use request facades. Following way:
<?php
use Illuminate\Http\Request;
public function index(Request $request)
{
dd($request->ip());
}
Is the IP received via the URL or is it tied to the User model?
If it's via URL then it'd be as simple as
Route::get('example.com/{ip}', 'Controller#someFunction');
then in the controller
public function someFunction($ip) {
... //$ip can be used in here
}
Or, if say there's an authenticated user with 'ip' as part of the model, you could access it in the controller by grabbing the authenticated user instance:
public function someFunction() {
$user = Auth::user();
//$user->ip;
}

Laravel get input data from POST request in a rest api

i'm trying to get input data which i post them from rest api as an json format, but in laravel i can't get them on controller and that return empty array of request
my api route:
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
$this->post('login', 'ApiController#login');
});
and ApiController:
<?php
namespace App\Http\Controllers\Api\v1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class ApiController extends Controller
{
public function login(Request $request)
{
dd($request->all());
}
}
output:
[]
ScreenShot
Problem resolved by adding this line:
Content-type: text/json
to RestClient header
use Input Facade class In my case it was work
use \Illuminate\Support\Facades\Input;
$request = Input::All();

Resources