I am working on api part in Laravel 5.2 and trying to fetch the details from Strava. As mentioned in this link http://socialiteproviders.github.io/providers/strava I have done all the steps. In the controller I wrote a function to pass the access token of the user. This is the function which I am using public function
<?php
namespace App\Http\Controllers\Api\v1;
use Illuminate\Http\Request;
use Auth;
use Socialite;
use App\Models\UserTrackerSettings;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StravaController extends Controller
{
/**
* Get the user by token
*
* #return \Illuminate\Http\Response
*/
public function getTrackerAccess()
{
$trackerDriver = Socialite::driver('strava');
$getToken = UserTrackerSettings::where('tracker_source_name', 'strava')
->where('user_id', Auth::user()->id)->first();
$access_token = $getToken->access_token;
return Socialite::getUserByToken($access_token);
}
}
But when I run the the link in postman I am getting this error
FatalErrorException in StravaController.php line 23:
Call to undefined method SocialiteProviders\Manager\ServiceProvider::driver()
It would be great if someone could help me. Thanks in advance!
Make sure that the actual driver is in there. Afterwards, since I don't see any class inclusions in there, you might need to call that class like this \App\Socialite::driver('strava') or just \Socialite::driver('strava'), I just don't know if that is the full code for your controller or not.
Related
i am trying to create a notes(with title and body) that should be store in database for that i write some api (only valid user can able to create notes based on the token),i am able to get the token when successfully logged in , When i try to create a note it's throwing some error ,How to rectify that error please help me to fix this issue...
Error
Symfony\Component\Debug\Exception\FatalThrowableError Class 'Notes' not found in file /home/payarc/Desktop/newLaravel/app/Http/Controllers/NotesController.php on line 20
NotesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Notes;
use App\Http\Controllers\Controller;
class NotesController extends Controller
{
public function create_Note(Request $request)
{
$note=new Notes();
$note->title=$request->input('title');
$note->body=$request->input('body');
$note->user_id = Auth()->id();
$note->save();
return $note;
}
}
Try to replace
use Notes;
with full path
use App\Models\Notes;
You simply use for creating object of notes model like this
$note=new AppNotes();
Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.
I'm using Laravel and trying to do an SQL query from my Controller in a public function, but I'm really confused where I would put my table in the argument and if quotes go around the argument. Here is my code
public function selectMethod(){
$results = DB::select('select firstname from people where id = 1');
print_r($results);
return view('pages.selectMethod');
}
table is called people
My .env is configured to my database correctly and I get this error
FatalErrorException in AboutController.php line 90:
Class 'App\Http\Controllers\DB' not found
Thanks !
you should add use Illuminate\Support\Facades\DB;
at the top of your page
for example :
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show a list of all of the application's users.
*
* #return Response
*/
public function index()
{
$users = DB::table('users')->get();
return view('user.index', ['users' => $users]);
}
}
Your error clearly states: Class 'App\Http\Controllers\DB' not found
Hence just use DB in your class. Add:
use DB;
At the top of the file just below the namespace line.
Also, I would suggest you to use Eloquent for your queries. It will make your life a lot easier and your code a lot beautiful.
im learning about laravel and im following some videos from laracasts, but im having a issue in displaying the data from the controller, i made all right, but still appears me empty array, the instance Card isnt working, here is my code:
Model:
Card.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
//
}
route:
Route::get('cards/{card}', 'CardsController#show');
CardsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Card;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CardsController extends Controller
{
public function show(Card $card)
{
return $card;
//$card = Card::find($card);
//return view('cards.show', compact('card'));
}
}
What you are attempting is called "Route Model Binding" and it seems to me that you are using Laravel 5.1 or lower (where route model binding is not implicit).
If you are using Laravel 5.2 or higher that code should just work. https://laravel.com/docs/5.3/routing#route-model-binding
But, if you are in Laravel 5.1 you need to do an additional step: https://laravel.com/docs/5.1/routing#route-model-binding
In the provider class RouteServiceProvider, in the boot method, you need to bind which route name {card} should bind to which Model, in this case Card.
So, you do something like this:
public function boot(Router $router)
{
parent::boot($router);
$router->model('card', \App\Card::class);
}
If you add that, the router will know that when it finds {card} it should get that number and do the Card::findOrFail with the ID automatically and if the model is found it will be passed down to your controller.
First of all, you will need to add the fillable protected property to your Eloquent model! Now, on to the good part.
In your routes file you have
Route::get('cards/{card}', 'CardsController#show');
This code, in a nutshell, will pass the card ID to your show function in your CardsController class. Eg. for this route: https://example.com/cards/5, it will in essence call the function show like this: show(5). In your code, you have that the show parameter is typehinted to be a Card. This is wrong. This is going to be an integer.
Thus, what you really need to do is check whether this ID exists and then pass the relevant information to your view. Something like this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Card;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CardsController extends Controller
{
/**
* Show the relevant card information
*
* #param $card This is the card ID (its an integer)
*/
public function show($card)
{
$card = Card::findOrFail($card);
return view('cards.show')->with(compact('card'));
}
}
I am trying to use the built in registration of Laravel 5 to upload a photo for each user.
I have made the changes that i needed in:
views/auth/register.blade.php
app/User.php
app/services/Registrar.php
And registration works fine.
But when I try to add the file upload logic, my problem is in Registrar.php.
I have added:
use Illuminate\Http\Request;
but in the create method using:
Request::hasFile()
returns an error of can't use as static.
use dependancy injection.
use Illuminate\Http\Request;
class FooController extends Controller {
public function __construct(Request $request)
{
$this->request = $request;
}
public function bar()
{
dd($this->request->hasFile('key'));
}
}
With laravel 5.*
please comment the first line and use the second line.
line 1 :
//use Illuminate\Http\Request;
line 2 :
use Request;