I'm trying to post a comment to my news post using the Laravelista\Comments but I got this error: Target [Laravelista\Comments\CommentControllerInterface] is not instantiable.
It was working just fine before, and I didn't change anything except the interface design. Not long after I couldn't post a comments anymore. This is the package I'm using https://github.com/laravelista/comments
Tried adding Laravelista\Comments\ServiceProvider::class, to the config\app.php but it didn't do any change.
Is there any solutions?
So on Laravel 9 there seems to be an issue getting with $this->middleware('auth'); in the constructor in the vendor CommentController. A workaround that is working for me is to basically create a custom controller and move the middleware('auth') to the actual routes instead.
extend the vendor CommentController
Copy the original constructor from there into your new comment controller but remove $this->middleware('auth');
Copy/paste the store, update, destroy, and reply functions from CommentService in the vendor folder into your new controller.
update the config (config\comments.php) to point to your new controller, example controller' => 'App\Http\Controllers\CustomCommentsController',
copy the routes from vendor into your routes file and chain the middleware('auth') to each route there.
Note: I have not tested this with guest commenting enable.
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravelista\Comments\Comment;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Config;
use Spatie\Honeypot\ProtectAgainstSpam;
use Illuminate\Support\Facades\Validator;
use Laravelista\Comments\CommentController;
class CustomCommentsController extends CommentController
{
public function __construct()
{
if (Config::get('comments.guest_commenting') == true) {
$this->middleware('auth')->except('store');
$this->middleware(ProtectAgainstSpam::class)->only('store');
} else {
// $this->middleware('auth');
}
}
public function store(Request $request)
{
// If guest commenting is turned off, authorize this action.
if (Config::get('comments.guest_commenting') == false) {
Gate::authorize('create-comment', Comment::class);
}
// Define guest rules if user is not logged in.
if (!Auth::check()) {
$guest_rules = [
'guest_name' => 'required|string|max:255',
'guest_email' => 'required|string|email|max:255',
];
}
// Merge guest rules, if any, with normal validation rules.
Validator::make($request->all(), array_merge($guest_rules ?? [], [
'commentable_type' => 'required|string',
'commentable_id' => 'required|string|min:1',
'message' => 'required|string'
]))->validate();
$model = $request->commentable_type::findOrFail($request->commentable_id);
$commentClass = Config::get('comments.model');
$comment = new $commentClass;
if (!Auth::check()) {
$comment->guest_name = $request->guest_name;
$comment->guest_email = $request->guest_email;
} else {
$comment->commenter()->associate(Auth::user());
}
$comment->commentable()->associate($model);
$comment->comment = $request->message;
$comment->approved = !Config::get('comments.approval_required');
$comment->save();
return $comment;
}
/**
* Handles updating the message of the comment.
* #return mixed the configured comment-model
*/
public function update(Request $request, Comment $comment)
{
Gate::authorize('edit-comment', $comment);
Validator::make($request->all(), [
'message' => 'required|string'
])->validate();
$comment->update([
'comment' => $request->message
]);
return $comment;
}
/**
* Handles deleting a comment.
* #return mixed the configured comment-model
*/
public function destroy(Comment $comment): void
{
Gate::authorize('delete-comment', $comment);
if (Config::get('comments.soft_deletes') == true) {
$comment->delete();
} else {
$comment->forceDelete();
}
}
/**
* Handles creating a reply "comment" to a comment.
* #return mixed the configured comment-model
*/
public function reply(Request $request, Comment $comment)
{
Gate::authorize('reply-to-comment', $comment);
Validator::make($request->all(), [
'message' => 'required|string'
])->validate();
$commentClass = Config::get('comments.model');
$reply = new $commentClass;
$reply->commenter()->associate(Auth::user());
$reply->commentable()->associate($comment->commentable);
$reply->parent()->associate($comment);
$reply->comment = $request->message;
$reply->approved = !Config::get('comments.approval_required');
$reply->save();
return $reply;
}
}
Routes:
Route::post('comments', [Config::get('comments.controller'), 'store'])->middleware('auth')->name('comments.store');
Route::post('comments/{comment}', Config::get('comments.controller') . '#reply')->middleware('auth')->name('comments.reply');
Route::delete('comments/{comment}', Config::get('comments.controller') . '#destroy')->middleware('auth')->name('comments.destroy');
Route::put('comments/{comment}', Config::get('comments.controller') . '#update')->middleware('auth')->name('comments.update');
Related
Observer
class FileLogObserver
{
public function updated(FileLogs $fileLogs)
{
$fileChangeLogs = FileChangeLogs::firstWhere('fileId', $fileLogs->filedId);
if ( !empty($fileChangeLogs)) {
$fileChangeLogs->save([
'logDetails' => '1 file updated',
]);
}
}
}
Controller
class FileLogController extends Controller
{
public function update(Request $request,$id){
$validator = Validator::make(
$request->all(),
[
'orderId' => 'required|integer',
'fileId' => 'required|integer',
'status' => 'required|string'
]
);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$data = FileLogs::find($id);
if($data){
$data->orderId=$request->orderId;
$data->fileId=$request->fileId;
$data->status=$request->status;
$data->update();
return response()->json(['status'=>'success','StatusCode'=> 200,'message'=>'Successfully Updated','data'=>$data]);
}
else{
return response()->json(['status'=>'Failed','message'=>'Update Failed'],400);
}
}
}
The created & retrieved methods are being properly triggered. However, the updated & deleted methods not triggered. Gone through many links & read that a reason can be becoz the update is not directly on the model. so, i tried like below in my controller. But update function is not working this method. I'm using Laravel-8 version. Any help is much appreciated.
$data = FileLogs::find($id);
if($data){
$data->update(['$data->orderId'=>'$request->orderId','$data->fileId'=>'$request->fileId','$data->status'=>'$request->status']);
you need to register those observer in App\Providers\EventServiceProvider
like
/**
* Register any events for your application.
*
* #return void
*/
public function boot()
{
FileLogs::observe(FileLogObserver::class);
}
ref link https://laravel.com/docs/8.x/eloquent#observers
Beware with me for a second as I try to lay the background to my issue.
So I having using the python web framework Flask close to a year now and it has a wonderful extension called Flask-Login that helps provide user session management kind of like this in laravel.
Having said all that, there is a certain feature in Flask-Login that provides the functionality that when a user is not logged or signed in and tries to access that a page that requires one to be authenticated for example /create_post, they will be redirected back to the login page with that page encoded in the query string like /login?next=%2Fcreate_post.
Am trying to implement the same feature in a laravel project that am working on so I can redirect the user to the page they probably wanted to go to in the first place or to a different route in case that query string doesn't exist and I cannot seem to find where to put my code to do just that and I don't want to mess with anything in the vendor directory(because of the obvious issues that come with that), and I have tried manipulating the file app/Http/Middleware/RedirectIfAuthenticated.php by doing what is below but with no success.
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/');
}
$previous_url = url()->previous(); // how do I insert this in query string
return $next($request);
}
Will I have to create my own middleware or is there another way of implementing this kind of feature in laravel?
NOTE: I am not using the default laravel authentication system. I have created my own controller SessionsController to handle logins which contains the below code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class SessionsController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except(['create', 'login']);
}
public function create()
{
$data = [
'title' => 'Login',
'body_class' => 'hold-transition login-page',
];
return view('auth.login', $data);
}
public function login(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
$user = User::checkCredentials($request->username, $request->password);
if (!$user) {
return back()->with([
'class' => 'alert-danger',
'message' => 'Please check your credentials',
]);
}
// set session active flag to true
$user->session_active = true;
$user->save();
auth()->login($user);
return redirect()->route('dashboard');
}
public function destroy()
{
$user = auth()->user();
$user->last_login = date('Y-m-d H:i:s');
$user->session_active = false;
$user->save();
auth()->logout();
return redirect()->route('login')->with([
'class' => 'alert-success',
'message' => 'You logged out successfully',
]);
}
}
Thank you.
I managed to somewhat solve my issue even though I didn't use query strings as I had wanted.
I create a helper function get_previous_url as shown below
/**
* Gets the previous url
*
* #return null|string
*/
function get_previous_url()
{
$host = $_SERVER['HTTP_HOST'];
$previous_url = url()->previous();
// check if previous url is from the same host
if (!str_contains($previous_url, $host)) {
return null;
}
// get the previous url route
list(, $route) = explode($host, $previous_url);
// make sure the route is not the index, login or logout route
if (in_array(substr($route, 1), ['', 'login', 'logout'])) {
$route = '';
}
return $route;
}
And then I called the same function in my SessionsController class in the create method by doing this
public function create()
{
$previous_url = get_previous_url();
if ($previous_url) {
session(['previous_url' => $previous_url]);
}
...
}
And then I changed my login method to
public function login(Request $request)
{
...
$redirect = redirect()->route('dashboard'); // '/'
if (session()->has('previous_url')) {
$redirect = redirect(session()->pull('previous_url'));
}
return $redirect;
}
I am totally new in laravel.
I install laravel 5.2 . I have done with CRUD in laravel. Now i want to integrate laravel authentication package. so i choose zizaco\entrust.
I follow each steps from doc link. but i don't understand what is wrong. In doc there is not mentioned that in which file i have to add following code.
$owner = new Role();
$owner->name = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description = 'User is the owner of a given project'; // optional
$owner->save();
$admin = new Role();
$admin->name = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description = 'User is allowed to manage and edit other users'; // optional
$admin->save();
and other below code in document.
Even
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
Not mentioned about implements class.
i do
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
but i got error
Trait 'App\Authenticatable' not found
New learner can't get where to place code. i search alot but i can't get perfect document which give right direction.
Where to create role,permissions? Anyone please help me.
1.Install "zizaco/entrust": "5.2.x-dev"
2.add provider and aliases in app.php
3.php artisan vendor:publish
4.php artisan migrate
Now add in user.php
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;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, EntrustUserTrait;
}
Next Create a model for Role
use Zizaco\Entrust\EntrustRole
class Role extends EntrustRole
{
protected $table = 'roles';
protected $fillable = ['name', 'display_name', 'description']
}
Now create a model for permmission
use Zizaco\Entrust\EntrustPermission;
class Permission extends EntrustPermission
{
protected $table = 'permissions';
protected $fillable = ['name', 'display_name', 'description'];
}
Now create a seeding table for roles and permissions
UserTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Permission;
use App\Role;
use App\User;
class UserTableSeeder extends seeder
{
public function run()
{
Permission::truncate();
Role::truncate();
User::truncate();
\DB::table('role_user')->delete();
\DB::table('permission_role')->delete();
//create a user
$veeru = User::create([
'name' => 'veeru',
'email' => 'something#something.com',
'password' => bcrypt('qwerty'),
]);
//create a role of admin
$admin = Role::create([
'name' => 'admin',
'display_name' => 'Admin',
'description' => 'Only one and only admin',
]);
//create a permission for role
$manage_users = Permission::create([
'name' => 'manage-users-roles-and-permissions',
'display_name' => 'Manage Users,Roles and Permissions',
'description' => 'Can manage users,roles and permission"s',
]);
//here attaching permission for admin role
$admin->attachPermission($manage_users);
//here attaching role for user
$veeru->attachRole($admin);
//here iam creating another role and permisssion
$application = Role::create([
'name' => 'appapirequestlogs',
'display_name' => 'AppApiRequestLogs',
'description' => 'This has full control on Application Core Request logs',
]);
$corereq = Permission::create([
'name' => 'appapireqlogindex',
'display_name' => 'AppApiReqLogIndex',
'description' => 'This has control on Application Core Request Logs index only',
]);
here attaching roles and permissions
$application->attachPermission($corereq);
$veeru->attachRole($application);
}
After seeding that create a routes file and controller for roles and permissions
routes.php
Route::group(['middleware' => 'web'], function () {
Route::group(['prefix' => 'admin'], function () {
Route::controller('roles', 'RolesController');
Route::controller('permissions', 'PermissionsController');
Route::controller('users', 'UsersController');
});
});
RolesController.php
use App\Permission;
use App\Role;
use Illuminate\Http\Request;
use App\Http\Requests;
class RolesController extends Controller
{
function __construct()
{
$this->middleware('auth');
$this->middleware('role:admin');
$this->middleware('permission:manage-users-roles-and-permissions');
}
public function getIndex(Request $request)
{
$roles = Role::with('perms')->get();
return view('roles.index', ['roles' => $roles]);
}
public function getCreate()
{
return view('roles.create');
}
public function postCreate(Request $request)
{
$role = Role::create([
'name' => $request->name,
'display_name' => $request->display_name,
'description' => $request->description
]);
return redirect()->to('admin/roles/index');
}
public function getUpdate($id)
{
$role = Role::findOrFail($id);
return view('roles.update', ['role' => $role]);
}
public function postUpdate(Request $request)
{
$role = Role::findOrFail($request->get('id'));
$role->name = $request->get('name');
$role->display_name = $request->get('display_name');
$role->description = $request->get('description');
$role->save();
return redirect();
}
public function getDelete($id)
{
$role = Role::findOrFail($id);
$role->delete();
return redirect();
}
public function getAttach(Request $request)
{
$role = Role::where('id', '=', $request->id)->with('perms')->first();
$permissions_id = $role->perms->pluck('id')->toArray();
$permissionsNotAttached = Permission::whereNotIn('id', $permissions_id)->get();
return view('roles.attach', compact('role', 'permissionsNotAttached'));
}
public function postAttach(Request $request)
{
$role = Role::findOrFail($request->id);
$permission = Permission::findOrFail($request->permission);
$role->attachPermission($permission);
return redirect();
}
public function getDetach(Request $request)
{
$role = Role::findOrFail($request->role_id);
$permission = Permission::findOrFail($request->permission_id);
$role->detachPermission($permission);
return redirect()->to('/admin/roles/index');
}
}
Similarly create PermissionsController.php
use App\Http\Requests\PermissionRequest;
use App\Permission;
use App\Http\Requests;
class PermissionsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('role:admin');
$this->middleware('permission:manage-users-roles-and-permissions');
}
public function getIndex()
{
$permissions = Permission::all();
return view('permissions.index', ['permissions' => $permissions]);
}
public function getCreate()
{
return view('permissions.create');
}
public function postCreate(Request $request)
{
$permission = Permission::create([
'name' => $request->name,
'display_name' => $request->display_name,
'description' => $request->description
]);
return redirect();
}
public function getUpdate($id)
{
$permission = Permission::findOrFail($id);
return view('permissions.update', ['permission' => $permission]);
}
public function postUpdate(Request $request)
{
$permission = Permission::findOrFail($request->get('id'));
$permission->name = $request->get('name');
$permission->display_name = $request->get('display_name');
$permission->description = $request->get('description');
$permission->save();
return redirect();
}
public function getDelete($id)
{
$permission = Permission::findOrFail($id);
$permission->delete();
return redirect();
}
}
After Creating first given process
create roles middleware example CheckRole
<?php
namespace App\Http\Middleware;
use Closure;
class CheckRole
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param $role
* #return mixed
*/
public function handle($request, Closure $next, $role)
{
if (\Auth::user()->hasRole($role)) {
return $next($request);
} else {
return response()->view('errors.401');
}
}
}
now create Check Permission
<?php namespace App\Http\Middleware;
use Closure;
class CheckPermission
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param $permission
* #return mixed
*/
public function handle($request, Closure $next, $permission)
{
if (\Auth::user()->can($permission)) {
return $next($request);
} else {
return response()->view('errors.401');
}
}
}
add these middlewares in kernal.php
'role' => CheckRole::class,
'permission' => CheckPermission::class
Now create a role with a name and add permissions to it.
example:
1.create a role name festivals.
2.now create multiple permissions for that
->fesindex (only forr index viewing)
->fesedit (only for edit and update)
->fesadd (only for adding)
->fesdelete (only for deleting)
3.now attach which permissions you want to give to that role festivals
4.if you want a user to see only index page then
for role festivals attach only fesindex
5.if you want a user to see index and add a new festival then create a new role for him and add fesindex and fes add
Now created permissions are placed in your controller
function __construct(FestivalRepository $repo)
{
$this->middleware('auth');
$this->middleware('permission:fesindex', ['only' => 'getFestivals']);
$this->middleware('permission:fesedit', ['only' => ['getFestival', 'postUpdateFestival']]);
$this->middleware('permission:fesadd', ['only' => ['getAddFestival', 'postAddFestival']]);
$this->middleware('permission:fesapprove', ['only' => 'getChangeStatus']);
$this->middleware('permission:fesdelete', ['only' => 'getDeleteFestival']);
}
here getFestivals,getAddFestival etc are methods in controller.
I'm new to Laravel 5.
I have a route post /subscribe.
//Subcribe
Route::post('/subscribe','SubscribeController#postSubscribe');
When I goto it, my Laravel application auto redirecting me to : /auth/login
I notice, in my
I have : /app/Http/routes.php
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Auth Controller
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* #param \Illuminate\Contracts\Auth\Guard $auth
* #param \Illuminate\Contracts\Auth\Registrar $registrar
* #return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
}
PasswordController
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller {
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
/**
* Create a new password controller instance.
*
* #param \Illuminate\Contracts\Auth\Guard $auth
* #param \Illuminate\Contracts\Auth\PasswordBroker $passwords
* #return void
*/
public function __construct(Guard $auth, PasswordBroker $passwords)
{
$this->auth = $auth;
$this->passwords = $passwords;
$this->middleware('guest');
}
}
Request by #SW
SubscribeController
<?php
namespace App\Http\Controllers;
use App\Subscribe;
use Input, Validator, Auth, Redirect, Request, Session, Mail, View;
class SubscribeController extends Controller {
public function index()
{
$subscribes = Subscribe::all();
return View::make('subscribes.index')
->with('subscribes',$subscribes);
}
//------------------------------------------------------------------------------------------------- [ Create]
public function create()
{
return View::make('subscribes.create');
}
//------------------------------------------------------------------------------------------------- [ Store ]
public function store()
{
$validator = Subscribe::validator(Input::all());
if ($validator->fails()) {
return Redirect::to('subscribe/create')
->withErrors($validator)->withInput();
} else {
$subscribe = new Subscribe;
$subscribe->email = Input::get('email');
$subscribe->save();
return Redirect::to('/subscribe')
->with('success','The web directory was created succesfully!');
}
}
//------------------------------------------------------------------------------------------------- [ Show ]
public function show($id)
{
$subscribe = Subscribe::findOrFail($id);
return View::make('subscribes.show')
->with('subscribe', $subscribe);
}
//------------------------------------------------------------------------------------------------- [ Edit ]
public function edit($id)
{
$subscribe = Subscribe::findOrFail($id);
return View::make('subscribes.edit')
->with('subscribe', $subscribe );
}
//------------------------------------------------------------------------------------------------- [ Update ]
public function update($id)
{
$validation = Subscribe::validator(Input::all());
if ($validation->fails()) {
return Redirect::to('subscribe/'. $id . '/edit')->withErrors($validation);
} else {
$subscribe = Subscribe::findOrFail($id);
$subscribe->email = Input::get('email');
$subscribe->save();
return Redirect::to('subscribe')
->with('success','The web directory was updated succesfully!');
}
}
//------------------------------------------------------------------------------------------------- [ Destroy ]
public function destroy($id){
$subscribe = Subscribe::find($id);
$subscribe->delete();
return Redirect::to('subscribe')
->with('success','The web directory was deleted succesfully!');
}
public function postSubscribe() {
$subscribe_email = Input::only('subscribe_email');
// Validation
$validator = Validator::make( $subscribe_email ,
array(
'subscribe_email' => 'email|unique:subscribes,email',
)
);
if ($validator->fails()) {
return Redirect::to('/#footer')
->with('subscribe_error', $subscribe_email['subscribe_email']. ' is already an Aveniros subscriber.')
->withErrors($validator)->withInput();
}else{
$subscribe = new Subscribe;
$subscribe->email = Input::get('subscribe_email');
$subscribe->save();
return Redirect::to('/thank-you');
}
}
public function postSubscribeAjax() {
$data = Request::all();
//dd($data); Stuck HERE
// Validation
$validator = Validator::make( $data,
array(
'subscribe_email' => 'email|unique:subscribes,email',
)
);
if ($validator->fails()) {
return Redirect::to('/#footer')
->with('subscribe_error','This email is already subscribed to us.')
->withErrors($validator)->withInput();
}else{
$subscribe = new Subscribe;
$subscribe->email = Input::get('subscribe_email');
$subscribe->save();
return Redirect::to('/thank-you');
}
}
public function thankyou() {
return view('subscribes.thankyou');
}
}
Why is that ? and how do I fix it ? Is this even a normal behavior ?
You need to have a method for your each of your routes, telling Laravel how to treat each route. For example in your route file, there should be Route::get('index#SubscribeController); line. As Staurt Wagner has mentioned, if you just navigate to a path, you are sending get request to the server.
If you are using Laravel 5, your controllers should be in the \app\Http\Controllers path.
Your index method in theSubscribeController can simply be:
public function index(){
return "Hello World!";
}
For a much thorough tutorial, refer to this wonderful series on Laracast.
I currently have a model that has a text field and a slug field.
I validate that the slug is unique in my form request class:
public function rules()
{
return [
'name' => 'required|min:3',
'slug' => 'required|alpha_dash|unique:questions'
];
}
This works fine on create and properly denies the creation of duplicate slugs. However on my update method, it won't let me save a record because the slug already exists. Of course the slug does exist, but it exists on the record being edited, so I would like to continue to allow it to be saved. However, it should not be able to be changed to a slug on ANOTHER record.
Here's what my update ArticlesController method looks like:
public function update(Article $article, ArticleRequest $request)
{
$article->update($request->all());
return redirect('articles');
}
Is there a way to make this work in L5?
Try to modify your rule like following(in form request class):
public function rules()
{
return [
'name' => 'required,min:3',
'slug' => 'required|alpha_dash|unique:categories,slug,'.$this->id')
];
}
It works for me.
In unique rule you may specify id you want to ignore.
You can create 2 separate request (one for create and one for update), but you can do it also this way checking if if is set(I assume your update url looks like /questions/2 ):
public function rules()
{
$rules = [
'name' => 'required|min:3',
'slug' => ['required', 'alpha_dash']
];
$rule = 'unique:questions';
$segments = $this->segments();
$id = intval(end($segments));
if ($id != 0) {
$rule .= ',slug,' . $id;
}
$rules['slug'][] = $rule;
return $rules;
}
}
If you must have the ability to update a slug, projects I've worked on usually require it is not editable after creation, then you can use laravel's built in rule to ignore a certain record on the table by primary key.
$rules['slug'] = "required|unique:questions,slug,{$id}";
http://laravel.com/docs/5.0/validation
see "Forcing a unique rule to ignore a given ID"
In EditArticleRequest:
public function $rules ()
{
$id = $this->id;
return [
'name' => 'required|min:3',
'slug' => "required|alpha_dash|unique:articles,slug,$id",
];
}
Here is how I do it in Laravel 5.3 in details:
1- Create a new Form Request class by executing the next command in your terminal:
php artisan make:request ArticleFormRequest
Where ArticleFormRequest is the name of the form request class. This command will create a file called ArticleFormRequest.php in app/Http/Requests directory.
2- Open that created file and remove its content then place the next content in it:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Article;
class ArticleFormRequest extends FormRequest
{
protected $rules = [
'name' => 'required|min:3',
'slug' => 'required|alpha_dash|unique:articles,slug',
];
// protected $user; // in case you want the current authenticated user
protected $request_method;
protected $id;
public function __construct(Request $request)
{
// $request->user() returns an instance of the authenticated user
// $this->user = $request->user(); // in case you want the current authenticated user
// $request->method() returns method of the request (GET, POST, PUT, DELETE, ...)
$this->request_method = strtoupper($request->method());
// segments(): Returns an array containing all of the segments for the request path
// it is important to assign the returned "segments" array to a variable first before using it, otherwise an error will occur
$segments = $request->segments();
// note this way will be valid only if "id" of the element is the last segment
$this->id = end($segments);
}
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$rules = $this->rules;
if ($this->request_method == "POST") {
// do nothing..
} elseif (in_array($this->request_method, ["PUT", "PATCH"])) {
$article = Article::find($this->id);
if ($article) {
// forcing a unique rule to ignore a given id | https://laravel.com/docs/5.3/validation
$rules["slug"] = [
"required",
"alpha_dash",
Rule::unique("articles", "slug")->ignore($article->id, "id"),
];
// this is also can be used
// $rules['slug'] = "required|alpha_dash|unique:articles,slug,$article->id,id";
}
}
return $rules;
}
}
3- In your controller, you can use that ArticleFormRequest in store() and update() methods like this:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ArticleFormRequest;
class ArticlesController extends Controller
{
public function store(ArticleFormRequest $request)
{
// your code here..
}
public function update(ArticleFormRequest $request, $id)
{
// Your code here..
}
}
As already mentioned you can use the ignore feature in the validator functionality.
Just reference the id of the item you wish to ignore and make sure that when you update you use a patch request!
See more info here! http://laravel.com/docs/5.0/validation#rule-unique
protected $rules = [
'name' => 'required|min:3',
'slug' => 'required|alpha_dash|unique:questions'
];
public function rules()
{
$rules = $this->rules;
if ($this->isMethod('patch'))
{
$id = $this->articles;
$rules['slug'] = $rules['slug'].',slug,'.$id;
}
return $rules;
}