When I try to authenticate the user with using facebook so I am able to store data into the user table but the failure to create data into the soical_accounts.So after that errors with come up "call to a member funtion create() on null". Can anyone provide me solution that where I am wrong.
In my SocialAccountControll, I have the following method
public function handleProviderCallback($provider)
{
try{
$user = Socialite::driver($provider)->user();
} catch (Exception $e) {
return redirect('/login');
}
$authUser = $this->findOrCreateUser($user, $provider);
Auth::login($authUser, true);
// redirectTo, so that way we use the same redirect location that the rest of our authentication uses.
//This is a normal protected function that you can add in your users table to redirect a user wherever
// you want to set that redirect to.
//return redirect($this->redirectTo);
return redirect('/home');
}
public function findOrCreateUser($socialUser, $provider)
{
$account = SocialAccount::where('provider_name', $provider)->where('provider_id',$socialUser->getId())->first();
if($account)
{
return $account->user;
}
else
{
$user = User::where('email', $socialUser->getEmail())->first();
if(! $user)
{
$user = User::create([
'email' => $socialUser->getEmail(),
'name' => $socialUser ->getName()
]);
}
$user->accounts()->create([
'provider_name' => $provider,
'provider_id' => $socialUser->getId()
]);
return $user;
}
}
In my database migration, I have users and social_accounts and user have one to many relationship with social_accounts.
user table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
Social_accounts table:
public function up()
{
Schema::create('social_accounts', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id');
$table->string('provider_name')->nullable();
$table->string('provider_id')->unique()->nullable();
$table->timestamps();
});
}
User Model
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function accounts()
{
$this->hasMany('App\SocialAccount');
}
}
SocialAccount Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class SocialAccount extends Model
{
protected $fillable = [
'provider_name', 'provider_id'
];
public function user() {
return $this->belongsTo('App\User');
}
}
You are not returning anything here so change:
public function accounts()
{
$this->hasMany('App\SocialAccount');
}
To
public function accounts()
{
return $this->hasMany('App\SocialAccount');
}
Related
I want that a user can create e.g. a football club. When the logged in user creates the club (with an Input-field) the foreignkey should automatically appear in the table of the club.
User
User Modell
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name',
'email',
'password',
];
public function wgGroup()
{
return $this->hasOne('WgGroup','user_id');
}
}
WgGroup e.g. for example to create a club
WgGroup Controller
class WGController extends Controller
{
public function dashboard()
{
return view('verified.dashboard');
}
public function createWG(Request $request)
{
$wg = new WgGroup();
$wg->wg_name = $request->wg_name;
$wg->user_id = User::find($request['id']);
$wg->save();
if($wg != null){
return redirect()->back()->with(session()->flash('alert-success', 'Your wg are createt'));
}
return redirect()->back()->with(session()->flash('alert-danger', 'Something went wrong!'));
}
}
WgGrup Model
class WgGroup extends Model
{
use HasFactory;
protected $table = 'wg_groups';
protected $fillable = [
'wg_name', 'user_id'
];
public function user() {
return $this->belongsTo('User');
}
}
WgGroup Database
class CreateWgGroups extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('wg_groups', function (Blueprint $table) {
$table->id();
$table->string('wg_name');
$table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('wg_groups');
}
}
I don't know exactly why the FK user_id is not added directly to the database.
public function user() {
return $this->belongsTo('User');
}
I thought through this function would happen.
I just made a delete function of accounts, but I'm stuck on a problem.I need to also delete posts of user, at delete account.How can I make that? I have table Users, where I have all details from users, and table Posts, where also have user_id and id,caption and image of post.
public function delete($id)
public function delete($id)
{
$profile = User::find($id);
$profile->delete();
Session::flash('remove', "The profile was successfully deleted!");
return redirect('login');
}
Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $guarded = [];
public function profileImage(){
$imagePath = ($this->image) ? $this->image : 'profile/vx2k9TEhkcgaRdOWKvs4lsxqOVmuzwumtwySEnvH.png';
return '' . $imagePath;
}
public function user(){
return $this->belongsTo(User::class);
}
public function followers(){
return $this->belongsToMany(User::class);
}
}
User.php
<?php
namespace App;
use App\Mail\NewUserWelcomeMail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Mail;
use Actuallymab\LaravelComment\CanComment;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
protected static function boot()
{
parent::boot();
static::created(function ($user) {
$user->profile()->create([
'title' => $user->username,
]);
});
}
public function posts()
{
return $this->hasMany(Post::class)->orderBy('created_at', 'DESC');
}
public function profile()
{
return $this->hasOne(Profile::class);
}
}
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $guarded = [];
public function user(){
return $this->belongsTo(User::class);
}
public function likes()
{
return $this->hasMany('App\Like');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
public function tags()
{
return $this->belongsToMany('App\Tag');
}
}
Migration posts table
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
I think you could try to delete related model in the controller before the user, i.e.:
public function delete($id)
{
$profile = User::find($id);
$profile->posts()->delete();
$profile->delete();
Session::flash('remove', "The profile was successfully deleted!");
return redirect('login');
}
Or you can go with model event, i.e.:
protected static function boot() {
parent::boot();
static::created(function ($user) {
$user->profile()->create([
'title' => $user->username,
]);
});
static::deleting(function($user) {
$user->posts()->delete();
});
}
Hello I am learning Laravel.
I have a picture gallery tha shows all pictures paginated but I want to only show on another page the users photos
So this is the controller for the only users photos to show
public function __construct()
{
$this->middleware('auth');
}
public function show(Photos $photos)
{
$user = auth()->user();
$images = Photos::all();
if($images->user->user_id==auth()->id())
{
return view('profile',['images'=>$images,
'user'=>$user]);
}
}
//This is my photos table
public function up()
{
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->unsignedinteger('user_id')->index();
$table->string('title');
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('photos');
}
public function up()
{
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->unsignedinteger('user_id')->index();
$table->string('title');
$table->string('image');
$table->timestamps();
});
}
'
And this is the route:
//users gallery
Route::get('/profile','ProfileController#show')->name('profile');
This is my photos model:
// protected $guarded = [];
protected $fillable = [
'user_id','title','image'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
And this is my user model:
protected $fillable = [
'username','firstname','lastname','phone', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function photos()
{
return $this->hasMany(Photos::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function favorite()
{
return $this->hasOne(Favorites::class);
}
The problem is that is still shows all the photos not only the user's one. Please tell me what is that I am doing wrong?
your show method in your controller should be
public function show()
{
return view('profile',['images' => Photo::where('user_id', $user->id)->paginate(10),'user' => $user]);
}
I am trying to authenticate users in my Laravel application.
I am encountering the following problem:
using driver database in auth.php: I can login using auth::attempt(), and auth::check is working, but I can't validate if the logged in user has a certain role.
using driver eloquent in auth.php: I can login using auth::attempt(), but auth::check is not working. I can however check the role of the logged in user.
edit (question): How can I fix this so that with only one of the drivers, i can do a complete authentication and role check?
Migration tables:
Schema::create('users', function ($table) {
$table->increments('id');
$table->integer('group_id')->unsigned();
$table->string('name', 64);
$table->string('email', 64)->unique();
$table->string('username', 64)->unique();
$table->string('phone', 13);
$table->string('address', 64);
$table->boolean('isresponsible');
$table->string('password', 64);
$table->rememberToken()->nullable();
});
Schema::create('roles', function ($table) {
$table->increments('id');
$table->string('name');
});
Schema::create('users_roles', function ($table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
}
);
Schema::table('users_roles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles');
});
model class User
<?php
use Illuminate\Auth\UserTrait;`
use Illuminate\Auth\UserInterface;`
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
public $timestamps = false;
public static $rules = ['name' => 'required', 'group_id' => 'required', 'email' => 'required', 'phone' => 'required'];
protected $fillable = ['name', 'group_id', 'email', 'phone', 'address', 'isresponsible', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
public function group()
{
return $this->belongsTo('Group');
}
public function userroles(){
return $this->hasMany('Userrole');
}
public function roles()
{
return $this->belongsToMany('Role', 'users_roles');
}
public function hasRole($check)
{
dd($this->roles->toArray());
return in_array($check, array_fetch($this->roles->toArray(), 'name'));
}
public function setBasicPassword($id){
$user = User::find($id);
$user->password = Hash::make('changeme');
$user->save();
}
public function isValid()
{
$validation = Validator::make($this->attributes, static::$rules);
if ($validation->passes()) return true;
$this->messages = $validation->messages();
return false;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getReminderEmail()
{
// TODO: Implement getReminderEmail() method.
}
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->email;
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* #return string
*/
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
}
model Class Role
class Role extends Eloquent
{
protected $table = 'roles';
public $timestamps = false;
public static $rules = ['role_id' => 'required', 'name' => 'required'];
protected $fillable = ['name'];
/**
* Get users with a certain role
*/
public function userroles()
{
return $this->belongsToMany('User', 'users_roles');
}
}
HomeController authentication function
public function authenticate(){
$rules = array(
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($userdata, true)) {
return Redirect::action('HomeController#index');
} else {
return Redirect::action('HomeController#login')->withInput();
}
}
}
USING THE DATABASE DRIVER
- auth:attempt() and auth::check are working
$this->beforeFilter('admin', ['only' => ['index']]); //filter in controller
//filter in filters;php
Route::filter('admin', function()
{
if(!Auth::check()) return Redirect::action('HomeController#index');
if(!Auth::user()->hasRole('admin')) return View::make('errors.401');
});
This fails with 'Call to undefined method Illuminate\Auth\GenericUser::hasRole()'
EDIT The database driver return a GenericUser Object, and I need my own User object. Don't know where I can change this.
Workaround:I'd rather not use this, ugly code and filters (or views) should not need to do this
Route::filter('admin', function()
{
if(!Auth::check()) return Redirect::action('HomeController#index');
$user = User::find((Auth::user()->id));
if(!$user->hasRole('admin')){ return View::make('errors.401');}
});
USING THE ELOQUENT DRIVER
auth::attempt() succeeds
auth::check() fails
no error on the filter
The problem is your implementation of getAuthIdentifier(). This method should actually return the primary key of your table and not the username that's used for logging in.
So yours should look like this:
public function getAuthIdentifier(){
return $this->id;
}
Or actually, I recommend you clean up your model a bit more since all of the getSomeAuthStuff methods are implemented in the two traits.
Use the default model on github as a base and add all your custom code (roles methods, rules etc)
Background info
The value returned from getAuthIdentifier() will be stored in the session.
When using check() afterwards, retrieveById will be called on the UserProvider. And the EloquentUserProvider does this:
public function retrieveById($identifier)
{
return $this->createModel()->newQuery()->find($identifier);
}
It uses find() which searches for the model by it's primary key (usually id)
I am trying to authenticate users in my Laravel application.
I am encountering the following problem:
using driver database in auth.php: I can login using auth::attempt(), and auth::check is working, but I can't validate if the logged in user has a certain role.
using driver eloquent in auth.php: I can login using auth::attempt(), but auth::check is not working. I can however check the role of the logged in user.
edit (question): How can I fix this so that with only one of the drivers, i can do a complete authentication and role check?
Migration tables:
Schema::create('users', function ($table) {
$table->increments('id');
$table->integer('group_id')->unsigned();
$table->string('name', 64);
$table->string('email', 64)->unique();
$table->string('username', 64)->unique();
$table->string('phone', 13);
$table->string('address', 64);
$table->boolean('isresponsible');
$table->string('password', 64);
$table->rememberToken()->nullable();
});
Schema::create('roles', function ($table) {
$table->increments('id');
$table->string('name');
});
Schema::create('users_roles', function ($table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
}
);
Schema::table('users_roles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles');
});
model class User
<?php
use Illuminate\Auth\UserTrait;`
use Illuminate\Auth\UserInterface;`
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
public $timestamps = false;
public static $rules = ['name' => 'required', 'group_id' => 'required', 'email' => 'required', 'phone' => 'required'];
protected $fillable = ['name', 'group_id', 'email', 'phone', 'address', 'isresponsible', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
public function group()
{
return $this->belongsTo('Group');
}
public function userroles(){
return $this->hasMany('Userrole');
}
public function roles()
{
return $this->belongsToMany('Role', 'users_roles');
}
public function hasRole($check)
{
dd($this->roles->toArray());
return in_array($check, array_fetch($this->roles->toArray(), 'name'));
}
public function setBasicPassword($id){
$user = User::find($id);
$user->password = Hash::make('changeme');
$user->save();
}
public function isValid()
{
$validation = Validator::make($this->attributes, static::$rules);
if ($validation->passes()) return true;
$this->messages = $validation->messages();
return false;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getReminderEmail()
{
// TODO: Implement getReminderEmail() method.
}
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->email;
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* #return string
*/
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
}
model Class Role
class Role extends Eloquent
{
protected $table = 'roles';
public $timestamps = false;
public static $rules = ['role_id' => 'required', 'name' => 'required'];
protected $fillable = ['name'];
/**
* Get users with a certain role
*/
public function userroles()
{
return $this->belongsToMany('User', 'users_roles');
}
}
HomeController authentication function
public function authenticate(){
$rules = array(
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($userdata, true)) {
return Redirect::action('HomeController#index');
} else {
return Redirect::action('HomeController#login')->withInput();
}
}
}
USING THE DATABASE DRIVER
- auth:attempt() and auth::check are working
$this->beforeFilter('admin', ['only' => ['index']]); //filter in controller
//filter in filters;php
Route::filter('admin', function()
{
if(!Auth::check()) return Redirect::action('HomeController#index');
if(!Auth::user()->hasRole('admin')) return View::make('errors.401');
});
This fails with 'Call to undefined method Illuminate\Auth\GenericUser::hasRole()'
EDIT The database driver return a GenericUser Object, and I need my own User object. Don't know where I can change this.
Workaround:I'd rather not use this, ugly code and filters (or views) should not need to do this
Route::filter('admin', function()
{
if(!Auth::check()) return Redirect::action('HomeController#index');
$user = User::find((Auth::user()->id));
if(!$user->hasRole('admin')){ return View::make('errors.401');}
});
USING THE ELOQUENT DRIVER
auth::attempt() succeeds
auth::check() fails
no error on the filter
The problem is your implementation of getAuthIdentifier(). This method should actually return the primary key of your table and not the username that's used for logging in.
So yours should look like this:
public function getAuthIdentifier(){
return $this->id;
}
Or actually, I recommend you clean up your model a bit more since all of the getSomeAuthStuff methods are implemented in the two traits.
Use the default model on github as a base and add all your custom code (roles methods, rules etc)
Background info
The value returned from getAuthIdentifier() will be stored in the session.
When using check() afterwards, retrieveById will be called on the UserProvider. And the EloquentUserProvider does this:
public function retrieveById($identifier)
{
return $this->createModel()->newQuery()->find($identifier);
}
It uses find() which searches for the model by it's primary key (usually id)