Laravel 5 - Laracast Easy Auth - Saving an article - php

I was following a tutorial on laracast about easy auth (Easy Auth), but there were some gaps on the video, i had to declare
use Auth;
to be able to get the current user, however, when i save the article i get this error
FatalErrorException in ArticleController.php line 42:
Call to undefined method Illuminate\Database\Eloquent\Collection::save()
where the corresponding code in my ArticleController is
public function store(ArticleRequest $request)
{
$article = new Article($request->all());
Auth::user()->articles->save($article);
return redirect('blog');
}
My Article model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Article extends Model {
protected $fillable = [
'title',
'body',
'published_at',
'user_id'
];
protected $dates = ['published_at'];
public function scopePublished ($query)
{
$query->where('published_at', '<=', Carbon::now());
}
public function scopeUnpublished ($query)
{
$query->where('published_at', '>', Carbon::now());
}
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::parse($date);
}
public function user()
{
return $this-> belongsTo('App\User');
}
}
My User model
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
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 articles()
{
return $this-> hasMany('App\Article');
}
}

try with this
Auth::user()->articles()->save($article);
store action
public function store(ArticleRequest $request)
{
$article = new Article($request->all());
Auth::user()->articles()->save($article);
return redirect('blog');
}

Related

Is there a explanation for my model function can't be used in my controller?

I'm building a laravel project which is a community platform, so it's gonna need a follower logic (pretty similar to twitter, instagram, etc).
I already created the logic for authentication and profile, but, when researching and writing the code for the followers state and check if the user is following someone, i got the functions on my model, which now is something like:
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'phone',
'description',
'profilepicture',
'status',
];
/**
* The attributes that should be hidden for serialization.
*
* #var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* #var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function following()
{
return $this->belongsToMany('App\Models\User', 'followers', 'follower_user_id', 'user_id')->withTimestamps();
}
public function isFollowing(User $user)
{
return !is_null($this->following()->where('user_id', $user->id)->first());
}
}
And on my Profile Controller, I have:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
class Profile extends Controller
{
public function show($id)
{
$user = User::where('id', $id)->firstOrFail();
$me = Auth::user();
$is_edit_profile = (Auth::id() == $user->id);
$is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
}
}
But VSCode says that i have a undefined method isFollowing in my controller, in the line:
$is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
Someone have a clue of why is this happening?
I'm using Laravel 8.
It's one of my first big projects, so previously sorry for any rookie mistake.
Thanks for your time and help!
Auth::user() returns an object of type Illuminate\Contracts\Auth\Authenticatable which does not implement isFollowing
Option 1 : You can add #var annotation to specify the type of your object
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
class Profile extends Controller
{
public function show($id)
{
$user = User::where('id', $id)->firstOrFail();
/** #var User $me */
$me = Auth::user();
$is_edit_profile = (Auth::id() == $user->id);
$is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
}
}
Option 2 : You can extends the Auth facade by creating a new facade with the expected return type :
namespace App\Extensions\Facades;
use App\Models\User;
/**
* #method static User user()
*/
class Auth extends \Illuminate\Support\Facades\Auth
{
}
And then you can use this facade instead of the previous one
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Extensions\Facades\Auth;
use App\Models\User;
class Profile extends Controller
{
public function show($id)
{
$user = User::where('id', $id)->firstOrFail();
$me = Auth::user();
$is_edit_profile = (Auth::id() == $user->id);
$is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
}
}

Class 'app\Models\Phone' not found in Laravel 8

I am trying ORM one to one relationship. I dont know why it cant recognize model class phone.
code as follows.
Phone.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, 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',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function Phone(){
return $this->hasOne('app\Models\Phone');
}
}
Phone.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
use HasFactory;
protected $table = "phones";
public function User(){
return $this->belongsTo('app\Models\User');
}
}
UserController.php
<?php
namespace App\Http\Controllers;
use App\Models\Phone;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function insert(){
$user = new User();
$user->name = 'BBB';
$user->email = 'bbb#mail.com';
$user->password = 'bbb#mail.com';
$user->save();
$phone = new Phone();
$phone->number = "445566";
$user->Phone()->save($phone);
return "RECORDS ADDED";
}
public function show($id){
$phone = User::find($id)->phone;
return $phone;
}
}
web.php
<?php
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('insert',[\App\Http\Controllers\UserController::class,'insert'])->name('user.insert');
Route::get('show/{id}',[\App\Http\Controllers\UserController::class,'show'])->name('user.show');
You need to fix this
public function User(){
return $this->belongsTo('App\Models\User');
}
here fix namespace app to App like that you need to fix all
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, 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',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function Phone(){
return $this->hasOne('App\Models\Phone');
//Better to use ::class notation for greater benefits with IDE
//return $this->hasOne(Phone::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
use HasFactory;
protected $table = "phones";
public function User(){
return $this->belongsTo('App\Models\User');
//Better to use ::class notation for greater benefits with IDE
//return $this->hasOne(User::class);
}
}
Folder/directory is app but the namespace is App
So need to change app\Models\Phone and app\Models\User to App\Models\Phone and App\Models\User
Better yet, use ::class notation for greater benefits with IDE - like easy navigation - not possible with string literals.
So instead of string literal App\Models\Phone import use App\Models\Phone statement and the Phone::class
Similarly for User import use App\Models\User and then use User::class

how do i go getting this eloquent relationship right?

I have an user model and a student model which I have created relationship for, but when I try to
$student->user->fullname
I get this error
"trying to get property fullname of non-object"
here is my user model code:
<?php
namespace App;
use App\Assignment;
use App\Model\Quiz;
use App\Model\Course;
use App\Topic;
use App\Model\Guardian;
use App\Model\Student;
use App\Model\Teacher;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, HasRoles, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'fullname',
'email',
'avatar',
'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',
];
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
public function guardian()
{
return $this->belongsTo(Guardian::class);
}
public function teacher()
{
return $this->belongsTo(Teacher::class);
}
public function student()
{
return $this->belongsTo(Student::class);
}
public function assignments()
{
return $this->hasMany(Assignment::class);
}
public function quizzes()
{
return $this->hasMany(Quiz::class);
}
public function courses()
{
return $this->hasMany(Course::class);
}
public function topics()
{
return $this->hasMany(Topic::class);
}
public function levels()
{
return $this->hasMany(Level::class);
}
}
and here is my student model code
<?php
namespace App\Model;
use App\User;
use App\Model\Course;
use App\Assignment;
use App\Level;
use App\Model\DoneQuiz;
use App\Model\Teacher;
use App\Model\Guardian;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $fillable = ['user_id', 'level_id', 'guardian_id'];
public function user()
{
return $this->belongsTo(User::class);
}
public function courses()
{
return $this->hasMany(Course::class);
}
public function assignments()
{
return $this->hasMany(Assignment::class);
}
public function level()
{
return $this->hasOne(Level::class);
}
public function teachers()
{
return $this->hasMany(Teacher::class);
}
public function guardian()
{
return $this->hasOne(Guardian::class);
}
public function donequizzes()
{
return $this->hasMany(DoneQuiz::class);
}
}
and even when I try to use this relationship to get data like
'student_id' => auth()->user()->student()->id
I get this error
"BadMethodCallException Call to undefined method
Illuminate\Database\Eloquent\Relations\BelongsTo::id()"
when you use student() it returns a query builder
Either change it to simple student
'student_id' => auth()->user()->student->id
OR
'student_id' => auth()->user()->student()->first()->id

Laravel policy return unauthorized

I am trying to implement policies in my project. All tries have proven unsuccessful despite following documentation to the letter. And also read numerous posts on SO about it and other media. I did as described in docs, but nonetheless it doesn't work. What gives?
In AuthServiceProvider:
<?php
namespace App\Providers;
use App\User;
use App\Job;
use App\Policies\JobPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
'App\Job' => 'App\Policies\JobPolicy',
//Job::class => JobPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}
In policy:
<?php
namespace App\Policies;
use App\Job;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class JobPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any jobs.
*
* #param \App\User $user
* #return mixed
*/
public function viewAny(User $user,Job $job)
{
//return (($user->isAdmin() || $user->isModerator() || $user->isUser()) && $user->status==1);
//return ($user->isMod());
return true;
}
In controller:
public function index()
{
$this->authorize('viewAny', User::class, Job::class);
return view("jobs.index");
}
My User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Role;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',"role_id"
];
/**
* 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',
];
public function role(){
return $this->belongsTo("App\Role", "role_id");
}
public function isMod()
{
$user = User::find(auth()->user()->id);
$role = $user->role()->first()->name;
if($role==="job board moderator"){
return true;
}
else{
return false;
}
}
}
And Job model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Job extends Model
{
protected $fillable = [
"title", "description", "email"
];
public function user(){
return $this->belongsTo("App\User","user_id");
}
}
In policy:
public function viewAny(User $user)
{
return true;
}
In controller:
public function index()
{
$this->authorize('viewAny', Job::class);
return view("jobs.index");
}
The way to call a model policy method changes depending on the number of parameters it has.
No object
/* In policy*/
public function viewAny(User $user)
/* In controller */
$this->authorize('viewAny', Job::class)`
1 object
/* In policy*/
public function view(User $user, Job $job)
/* In controller */
$this->authorize('view', $job)
More than 1 object
/* In policy*/
public function view(User $user, Job $job, AnotherModel $model)
/* In controller */
$this->authorize('view', [$job, $model])
Source: https://laravel.com/docs/5.8/authorization#creating-policies

where is the attribute in the laravel code comes from?

<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, 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 static function boot()
{
parent::boot();
static::creating(function ($user) {
$user->activation_token = str_random(30);
});
}
public function gravatar($size = '100')
{
$hash = md5(strtolower(trim($this->attributes['email'])));
return "http://www.gravatar.com/avatar/$hash?s=$size";
}
public function statuses()
{
return $this->hasMany(Status::class);
}
public function feed()
{
return $this->statuses()->orderBy('created_at', 'desc');
}
public function followers()
{
return $this->belongsToMany(User::Class, 'followers', 'user_id', 'follower_id');
}
public function followings()
{
return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id');
}
public function follow($user_ids)
{
if (!is_array($user_ids)) {
$user_ids = compact('user_ids');
}
$this->followings()->sync($user_ids, false);
}
public function unfollow($user_ids)
{
if (!is_array($user_ids)) {
$user_ids = compact('user_ids');
}
$this->followings()->detach($user_ids);
}
public function isFollowing($user_id)
{
var_dump($this->followings);die();
return $this->followings->contains($user_id);
}
}
This is a code come from laravel models.
There is a method named $this->followings() .But I don't see any $this->followings attribute assigned in the code.
where is the $this->followings comes from?
thanks
Suggested reading about Laravel model relationships
in particular:
Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. Dynamic properties allow you to access relationship methods as if they were properties defined on the model

Categories