Storing data in customise table after google authentication - laravel-5.8

by default this data is stored in "users" table but i need to store data in "new_users" table what and where i need to update ???
SocialGoogleAccount extends Model
{
protected $fillable = ['user_id', 'provider_user_id', 'provider'];
public function newuser()
{
return $this->belongsTo(User::class);
}
}
my controller is
public function redirect()
{
return Socialite::driver('google')->redirect();
}
/**
* Return a callback method from google api.
*
* #return callback URL from google
*/
public function callback(SocialGoogleAccountService $service)
{
$user = $service->createOrGetUser(Socialite::driver('google')->user());
auth()->login($user);
return redirect()->to('/');
}

Related

Laravel 6 - Accessor appends not called in relationships

I use Laravel 6 and I want access to avatar attribute from user when I use posts() relation.
User model:
/**
* #var array
*/
protected $appends = [
'avatar',
];
/**
* #return HasMany
*/
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
/**
* #return string
*/
public function getAvatarAttribute(): string
{
return sprintf('https://secure.gravatar.com/avatar/%s?s=500', md5($this->email));
}
The code of my controller:
$topic = Topic::where('slug', $slug)->firstOrFail();
foreach ($topic->posts()->get() as $post) {
dd($post->user->avatar); // return null
}
Post model:
/**
* #return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
I get the name of user with $post->user->name but avatar attribute is not called.

Multiple findForPassport ? e.g Login with username and phone number

I have tried findForPassport. It is good if there is just one field instead of the default but I want to log in either with username or phone number.
For the basic login what I did is
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->username = $this->findUsernameOrPhone();
}
public function findUsernameOrPhone()
{
$login = request()->input('username');
$fieldType = Str::startsWith($login, '07') ? 'phone' : 'username';
request()->merge([$fieldType => $login]);
return $fieldType;
}
/**
* Get phone property.
*
* #return string
*/
public function phone()
{
return $this->username;
}
/**
* Get username property.
*
* #return string
*/
public function username()
{
return $this->username;
}
Just add this into t=your user model. here $username can be username or email
public function findForPassport($username) {
return $this->orWhere('username', $username)
->orWhere('email',$username)->first();
}

Implemented a sidebar based on user with most posts and comments

i want to implement a sidebar based on user with most posts and comments in laravel 5.2 app,
i want to show the top 5 users with most posts and comment.
Exemple Like this:
User Name: "Mohcin", Post:30, answer:20
Thanks in advance
User model:
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function questions(){
return $this->hasMany('\App\Question');
}
public function answers(){
return $this->hasMany('\App\Answer');
}
Question model: (posts model)
public function User(){
return $this->belongsTo('\App\User');
}
public function answers(){
return $this->hasMany('\App\Answer');
}
public static function your_questions(){
return static::where ('user_id','=',Auth::user()->id)->paginate(50);
}
public static function unsolved(){
return static::where ('solved','=',0)->orderby('id','DESC')->latest()->paginate(50);
}
public static function unsolvedbar(){
return static::where ('solved','=',0)->orderby('id','DESC')->latest()->take(3)->get();;
}
public function category()
{
return $this->belongsTo('App\Category');
}
public function tags()
{
return $this->belongsToMany('App\Tag');
}
Steps:
Open app/Providers/AppServiceProvider.php
In this file you have an empty (probably) boot method.
Inside this method, you can create a view composer to pass data for a specific view.
Like this:
//Assuming you want to pass the var to the view: sidebar.blade.php inside resources/views/layout
view()->composer('layout.sidebar', function($view){
$topUsers = User::topUsers(); //Create a query or scope to do this...
//After populate topUsers, you have to pass it to the view using:
$view->with('topUsers', $topUsers);
});
In your view: `resources/views/layout/sidebar.blade.php, you can access the users with a simple $topUsers like this:
foreach($topUsers as $user) {
var_dump($user);
}

Laravel Trying to get property of non-object

I am struggling to understand how laravel works and I have a very difficult time with it
Model - User.php the User model
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable = array('email' , 'username' , 'password', 'code');
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password');
public function Characters()
{
return $this->hasMany('Character');
}
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* #return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Model - Character.php the character model
<?php
class Character extends Eloquent {
protected $table = 'characters';
protected $fillable = array('lord_id','char_name', 'char_dynasty', 'picture');
public function user()
{
return $this->belongsTo('User');
}
public function Titles()
{
return $this->hasMany('Title');
}
}
?>
routes.php
Route::group(array('prefix' => 'user'), function()
{
Route::get("/{user}", array(
'as' => 'user-profile',
'uses' => 'ProfileController#user'));
});
ProfileController.php
<?php
class ProfileController extends BaseController{
public function user($user) {
$user = User::where('username', '=', Session::get('theuser') );
$char = DB::table('characters')
->join('users', function($join)
{
$join->on('users.id', '=', 'characters.user_id')
->where('characters.id', '=', 'characters.lord_id');
})
->get();
if($user->count()) {
$user = $user->first();
return View::make('layout.profile')
->with('user', $user)
->with('char', $char);
}
return App::abort(404);
}
}
In my code I will redirect to this route with the following:
return Redirect::route('user-profile', Session::get('theuser'));
In the view I just want to do:
Welcome back, {{ $user->username }}, your main character is {{ $char->char_name }}
My problem is that I will receive this error: Trying to get property of non-object in my view. I am sure it is referring to $char->char_name. What's going wrong? I have a very difficult time understanding Laravel. I don't know why. Thanks in advance!
You should be using the Auth class to get the session information for the logged in user.
$user = Auth::user();
$welcome_message = "Welcome back, $user->username, your main character is $user->Character->char_name";
You don't need to pass anything to that route either. Simply check if the user is logged in then retrieve the data. You have access to this data from anywhere in your application.
if (Auth::check())
{
//the user is logged in
$user = Auth::user();
To answer your question in the comments, reading the documentation would solve all of these problems, however:
public function user()
{
if (Auth::check())
{
$user = Auth::user();
return View::make('rtfm', compact('user'));
}
else
{
return "The documentation explains all of this very clearly.";
}
}

Laravel basic-auth

I want to use basic.auth for my web page but authentication donst work
routes.php
admin - authentication
Route::get('admin', array('before' => 'auth.basic', function()
{
return 'Top secret';
}));
create - create test user
Route::get('create', function()
{
$user = new User;
$user->email = 'test#test.com';
$user->username = 'test';
$user->password = Hash::make('password');
$user->save();
});
config
app/config/app - has defined key (that created Laravel installation)
app/config/auth - has defined model (User) and table (users)
filters.php
auth.basic
Route::filter('auth.basic', function()
{
return Auth::basic();
});
test
I call /create to create User test#test.com:password
Here is users table after:
Then I call /admin to login
But it doesnt let me in. After Login - it just clear inputs. After Cancel - it return Invalid credentials..
User model
I tried implement UserInterface
<?php
use Illuminate\Auth\UserInterface;
class User extends Eloquent implements UserInterface {
protected $table = 'users';
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->passsword;
}
}
Problem solved
I had typo in User model return $this->passsword; There is 3 s.
Now I use default Laravel User model.
Ensure that in app/config/auth.php - driver is set to eloquent.
You may also need to implement the UserInterface interface (class User extends Eloquent implements UserInterface) - then you'll need to include the methods in your model:
/**
* Get the unique identifier for the user.
*
* #return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* #return string
*/
public function getAuthPassword()
{
return $this->password;
}

Resources