I want use spatie to get Google+ avatar, but when I try get it there is an error.
$user = Socialite::driver('google')->user();
$usertest=User::whereEmail($user->getemail())->first();
if(! $usertest){
$usertest=User::create([
'name'=>$user->name,
'email'=>$user->email,
'password'=>bcrypt($user->id)
]);}
$usertest->addMediaFromUrl($user->avatar)->toMediaCollection('avatar');
auth()->loginUsingId($usertest->id);
return redirect('/');
error:
Type error: Argument 1 passed to Spatie\MediaLibrary\FileAdder\FileAdder::processMediaItem() must be an instance of Spatie\MediaLibrary\HasMedia\HasMedia, instance of App\User given,
It looks like you have not added HasMedia interface and HasMediaTrait to User class:
class User extends Authenticatable implements HasMedia {
use HasMediaTrait;
// ...
}
Related
' I am new in Laravel and I am trying to send notification but it shows an error (Call to a member function notify() on string)'
$discussion->user['email']->notify(new newReplyAdded($discussion));
You need to use notify() on a model, not on a string. Like below
$user = User::find(1);
$user->notify(..)
And you have to use Notifiable trait in you model like
use Illuminate\Notifications\Notifiable;
class User extends Model
{
use Notifiable;
..........
}
In your case, you can try this
$discussion->user->notify(new newReplyAdded($discussion));
I have a Laravel 5 model Account which implements an Interface.
I have implemented all the methods of the Interface however when I run the code, Laravel complains that the Model does not implement the interface.
Error below
Account.php (Model)
<?php
namespace Abc\Accounts\Models;
use Abc\Accounts\Contracts\Accountlnterface;
use Illuminate\Database\Eloquent\Model;
class Account extends Model implements Accountlnterface {
....
In my Controller I am doing this
$account = Account::where('something', 'value')->first();
This returns the model just fine.
The problem lies when I pass it into another class controller
$result = new Transaction($account, 5.00);
Transaction file
public function __construct(Accountlnterface $account, float $value = 0.00)
{
$this->account = $account;
The transaction constructor is looking for the Interface however laravel is complaining that Account does not implement it.
Im not sure whats wrong with this code.
Error from Laravel
Type error: Argument 1 passed to
Abc....\Transaction::__construct() must be an instance of
Abc\Accounts\Components\Accountlnterface, instance of
Tymr\Plugins\Accounts\Models\Account given.
Directly after the model is loaded I ran this code
if($account instanceof AccountInterface)
echo "working";
else
echo "fails";
and it certainly fails.
You need to register a service container binding in one of your service providers or better create a new service provider.
It helps Laravel know the implementation to use for your interface.
use Illuminate\Support\ServiceProvider;
class ModelsServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind(
'Abc\Accounts\Contracts\Accountlnterface',
'Abc\Accounts\Models\Account'
);
}
}
In app/config/app.php, register your service provider under the available providers.
'providers' => [
...
'ModelsServiceProvider',
]
In my seeder class, I am calling a method that is defined in a User model:
Like this:
$user = User::where('email', 'user1#teams.com')->get();
$user->test();
My User model:
class User extends Authenticatable
{
public function test()
{
return "!!";
}
}
But, when run the seed, I get this error:
[BadMethodCallException]
Method test does not exist.
the $user contains a collection of users since you are using get(). You can use first() instead.
So the new code should be:
$user = User::where('email', 'user1#teams.com')->first();
User model:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Zizaco\Entrust\HasRole;
class User extends Eloquent implements UserInterface, RemindableInterface {
use HasRole;
Role Model:
<?php
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole
{
}
Permission Model:
<?php
use Zizaco\Entrust\EntrustPermission;
class Permission extends EntrustPermission
{
}
User controller:
public function postSignin(){
if (Auth::attempt(array('email'=>Input::get('email'),
'password'=>Input::get('password')))) {
$id = Auth::user()->id;
$user = User::where('id','=',$id);
$firstname = Auth::user()->firstname;
if ($user->hasRole("User_Not_Approved")) {
return Redirect::intended('/users/dashboard');
}
Error message:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::hasRole()
The error message is presented when the IF statement is running, whilst the user is logging in. I have followed Entrust's instructions, but I am at a loss as to why it isn't picking up the method.
Any help would be hugely appreciated!
Try changing: $user = User::where('id','=',$id); to $user = User::find($id);
User::where would need ->get to return what you want, and even then it would return a collection; you would want something like User::where(etc)->first(); to ensure you got a single instance of User. In reality though, since you are retrieving by id, that is what ->find($id) is designed for, and what you should do.
I have a basic laravel 4 app that allows someone to register and then login. I am trying to make it so that when a user completes their registration successfully they are logged in automatically. I get an error exception 'Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Auth\UserInterface, instance of User given'. I understand that this means that the first argument being passed to the login method is not correct but I don't understand why it is not correct when the laravel documentation says to use
$user = User::find(1);
Auth::login($user);
Here is my controller
<?php
Class UsersController extends BaseController {
public $restful = 'true';
protected $layout = 'layouts.default';
public function post_create()
{
$validation = User::validate(Input::all());
if ($validation->passes()) {
User::create(array(
'username'=>Input::get('username'),
'password'=>Hash::make(Input::get('password'))
));
$user = User::where('username', '=', Input::get('username'))->first();
Auth::login($user);
return Redirect::Route('home')->with('message', 'Thanks for registering! You are now logged in!');
}
else {
return Redirect::Route('register')->withErrors($validation)->withInput();
}
}
}
?>
There's a few scenarios I can think of:
You're not using the User model which comes with a fresh Laravel install (sounds unlikely, but that one implements UserInterface, it's possible yours does not if you've edited it or created a new one).
User::create() isn't successfully being called (isn't created a user successfully)
$user = User::where()->... isn't resulting in a result
Try:
$user = User::create(array(
'username'=>Input::get('username'),
'password'=>Hash::make(Input::get('password'))
));
Auth::login($user);
If you still get errors, it's likely that $user isn't a User object because the user wasn't created successfully.
make sure your User.php begins like..
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {