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;
}
Related
This is an API to upload files. In this, admin as well as other users can upload files. so, i need to find the person who is uploading it and want to store it to the column uploadedBy. when tried with the below code , i get the error Trying to get property 'id' of non-object . I'm not ssure whether i can get the id like this. Pls help me with ur suggestions.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Files;
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
public function upload(Request $req)
{
$file=$req->file('file_name');
$dataToInsert = array();
$filePath= uniqid().'-'.now()->timestamp;
$dataToInsert['FilePath'] = $filePath;
$dataToInsert['uploadedBy'] = Auth::user()->id;
$dataToInsert['Userid'] = $req->bearerToken();
Files::create($dataToInsert);
}
}
EDIT: Added the middleware . Now the error is that API simply shows the msg unauthenticated.
You should put your route on auth middleware, this route can be accessed after authenticated only, so that you can get user info by auth()->user().
To access id only, just auth()->id(), this can return null if the application is not authenticated.
Please note that you pass gate name in auth() helper, eg: auth('api')->id().
You can get the id of the authenticated user by auth()->id()
Edit: You need to be logged in first, or it will throw an error
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
I want to make a settings screen for my users, and I want the URL to be easy an memorable: domain.com/user/settings
I want this route to point use the UserController#edit method, however, the edit() method requires ID parameter.
Is there someway I can use user/settings URL without specifying the ID in the URL, but still use the same edit() method in the UserController?
The edit URL could be use without any parameter. In that case you can mention the user in controller.
Route
Route::get('user/settings');
Controller
public function edit()
{
$user = Auth::user();
}
Seems like I managed to solve it?
web.php:
Route::get('user/settings', 'UserController#edit')->name('user.settings');
Route::resource('user', 'UserController');
UserController.php:
public function edit(User $user = null)
{
$user = Auth::user();
}
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);
}
In my application I have a page it called index.blade, with route /index. In its URL, it has some get parameter like ?order and ?type.
I want to pass these $_get parameter to my route controller action, query from DB and pass its result data to the index page. What should I do?
If you want to access the data sent from get or post request use
public function store(Request $request)
{
$order = $request->input('order');
$type = $request->input('type');
return view('whatever')->with('order', $order)->with('type', $type);
}
you can also use wildcards.
Exemple link
website.dev/user/potato
Route
Route::put('user/{name}', 'UserController#show');
Controller
public function update($name)
{
User::where('name', $name)->first();
return view('test')->with('user', $user);
}
Check the Laravel Docs Requests.
For those who need to pass part of a url as a parameter (tested in laravel 6.x, maybe it works on laravel 5.x):
Route
Route::get('foo/{bar}', 'FooController#getFoo')->where('bar', '(.*)');
Controller:
class FooController extends Controller
{
public function getFoo($url){
return $url;
}
}
Test 1:
localhost/api/foo/path1/path2/file.gif will send to controller and return:
path1/path2/file.gif
Test 2:
localhost/api/foo/path1/path2/path3/file.doc will send to controller and return:
path1/path2/path3/file.doc
and so on...