How can i edit and delete a page in laravel 5.2? - laravel-5

When i try to edit and delete an employee,an error will shows.
For delete
Missing argument 1 for App\Http\Controllers\Admin\CreateEmployeeController::deleteemployee()
For edit
Missing argument 1 for App\Http\Controllers\Admin\CreateEmployeeController::editemployee()
Methods:
public function editemployee($id)
{
$employee = CreateEmployee::where('id',$id)->get();
return view('app.admin.employee.editemployee',compact('employee'));
}
public function updateemployee(Request $request)
{
CreateEmployee::where('id',$request->id)->update(array('username'=>$request->username,'area'=>$request->area_name));
Session::flash('flash_notification', array('level' => 'success', 'message' => 'channel details updated successfully'));
return Redirect::action('Admin\CreateEmployeeController#addemployee',array('id' => $request->id));
}
public function deleteemployee($id)
{
$employee = CreateEmployee::where('id',$id)->get();
return view('app.admin.employee.deleteemployee',compact('employee'));
}
public function deleteconfirms($id)
{
$employee = CreateEmployee::where('id',$id)->delete();
Session::flash('flash_notification', array('level' => 'success', 'message' => 'employee deleted successfully'));
return Redirect::action('Admin\CreateEmployeeController#addemployee');
}

As I can see that your methods deleteemployee , deleteconfirms and editemployee are expecting the id field.
While in your route you are not using any "Route parameters" (for details see Route Parameters ).
So, change your routes to include Route Parameters as follows. where id represents employee_id
Route::get('edit-employee/{id}','CreateEmployeeController#editemp‌​loyee');
Route::post('update-employee','CreateEmployeeController#upd‌​ateemployee');
Route::get('delete-employee/{id}','CreateEmployeeController#delet‌​eemployee');
Route::post('delete-confirms/{id}','CreateEmployeeController#dele‌​teconfirms');

Related

how to use reflash or more session to transport session

in my controllers at laravel 9 **postManageTwoFactor** method i use $request->session()->flash('phone2' , $data['phone']);
for transport session to method **getPhoneVerify** , this is ture!
then i use $request->session()->reflash(); when i want to "reflash session " to postPhoneVerify method for last time, this is null or false!
i need this session $request->session()->get('phone2') in postPhoneVerify method...\
thank for helping me
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ActiveCode;
class ProfileController extends Controller
{
public function index()
{
return view('profile.index');
}
public function manageTwoFactor()
{
return view('profile.two-factor-auth');
}
public function postManageTwoFactor(Request $request)
{
$data = $request->validate([
'type' => 'required|in:on,off',
'phone' => 'required_unless:type,off',
]);
if($data['type'] === 'on'){
if($request->user()->phone_number !== $data['phone']){
/*create the new code for users...*/
$code = ActiveCode::generateCode($request->user());
/*
* in sessions "flash" meaning:
* validate unitl one route and not any more....
* easy: $data['phone'] store in 'phone2'/
*/
$request->session()->flash('phone2' , $data['phone']);
/*-------- send the code number to phone-----------*/
//TODO send sms for 2auth...
return redirect(route('prof.two.phone'));
}else{
/*
* put that 'on' becuse the number that entered is the same
*/
$request->user()->update([
'two_factor' => 'on'
]);
}
}
if($data['type'] === 'off'){
$request->user()->update([
'two_factor' => 'off'
]);
}
return back();
}
/*
* when this method or route , should be ruunig where users Request
* the code and fill the phone number field...
*/
public function getPhoneVerify(Request $request)
{
if (! $request->session()->has('phone2')){
return redirect(route('profile.2fa'));
}
$request->session()->reflash();
return view('profile.phone-verify');
}
public function postPhoneVerify(Request $request)
{
$request->validate([
'token' => 'required'
]);
if (! $request->session()->has('phone2')){
return redirect(route('profile.2fa'));
}
$status = ActiveCode::verifyCode($request->token , $request->user());
if($status){
/*---- after verify Code we need delete old record for each row user in active_codes table in dbase ---*/
$request->user()->activeCode()->delete();
/*--after all, with under code we can UPDATE active_codes table in database...*/
$request->user()->update([
'phone_number' => $request->session()->get('phone2'),
'two_factor' => 'on'
]);
alert()->success('احراز هویت دو مرحله ای شما انجام شد' , 'عملیات موفق آمیز بود');
}else{
alert()->success('لطفا دوباره تلاش کنید','عملیات موفق آمیز نبود');
}
return redirect(route('profile.2fa'));
}
}

Call to a member function move() on null when update in laravel

I got this errror when trying to update data. If I update the image there's no error, but if I'm not it showing Call to a member function move() on null
This is my code:
public function update($id, Request $request)
{
$change = Profile::findorfail($id);
$before = $change->foto_profil;
$profile = [
'fullname' => $request['fullname'],
'phone' => $request['phone'],
'ttl' => $request['ttl'],
'foto_profil' => $before
];
$request->foto_profil->move(public_path().'/image', $before);
$change->update($profile);
return redirect('/profil');
}
You may determine if a file is present on the request using the hasFile() method :
if($request->hasFile('foto_profil')){
$request->foto_profil->move(public_path().'/image', $before);
}
See the documentation here
just add validation if photo exists in request
if($request->foto_profil) {
$request->foto_profil->move(public_path().'/image', $before);
}

Laravel model function best prickets

im new in Laravel , I have an issue as below
I make in category model query to check is category is exist or not
as below
public function scopeIsExist($query ,$id)
{
return $query->where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC')->first();
}
and my controller is
public function edit($id)
{
$dataView['category'] = Category::IsExist($id);
if(!$dataView['category'])
{
return view('layouts.error');
}else{
$dataView['title'] = 'name';
$dataView['allCategories'] = Category::Allcategories()->get();
return view('dashboard.category.edit')->with($dataView);
}
}
my problem is when I use method isEXIST if id not found it not redirect to error page but ween i remove ISEXIST AND replace it as below
$dataView['category'] = Category::where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC')->first();
it work well .
can any one help me
That's because local scope should return an instance of \Illuminate\Database\Eloquent\Builder. You should remove the first() in the scope and put it in the controller.
Redefine your scope like so:
public function scopeIsExist($query ,$id)
{
return $query->where(['deleted' => 1, 'id' => $id])->orderBy('id', 'DESC');
}
In your controller edit method:
$dataView['category'] = Category::IsExist($id)->first();
You can have a look to the doc for local scopes https://laravel.com/docs/8.x/eloquent#local-scopes

Laravel - redirect from controller method not working

I have this method in my DocumentsController and am trying to implement some simple permissions system, in that a user that is not an admin must have been assigned a branch and a department before adding, editing or deleting a Document.
Here is the code for the method
/**
* Check the credentials of the user that is not an admin
* to add, modify a document
*/
private function checkCredentials() {
$user = auth()->user();
// dd((!is_admin() && !$user->branch_id && !$user->department_id));
if (!is_admin() && !$user->branch_id && !$user->department_id) {
// dd(redirect()->route('documents'));
return redirect()->route('documents')->with([
'class' => 'alert-danger',
'message' => 'Ask your administrator to assign you a branch and department first',
]);
}
}
And here is how am calling it in one of my controller methods that mapped to the route Route::get('/documents/add', ['as' => 'documents.add', 'uses' => 'DocumentsController#create',]);
public function create()
{
$this->checkCredentials();
...
return view('main/documents/create', $data);
}
Problem is the redirection is not working as it continues to display the form, even when the user has not yet been assigned a branch or department, that is when both the branch_id and department_id are both equal to null.
What could be the reason for this? Thank you.
You are not returning the redirect from the controller, try this:
/**
* Check the credentials of the user that is not an admin
* to add, modify a document
*/
private function checkCredentials() {
$user = auth()->user();
// dd((!is_admin() && !$user->branch_id && !$user->department_id));
if (!is_admin() && !$user->branch_id && !$user->department_id) {
// dd(redirect()->route('documents'));
return false;
}
}
public function create()
{
if(!$this->checkCredentials()) {
return redirect()->route('documents')->with([
'class' => 'alert-danger',
'message' => 'Ask your administrator to assign you a branch and department first',
]);
}
...
return view('main/documents/create', $data);
}
I think you use authorization (gate/policy). https://laravel.com/docs/5.8/authorization
Your code need to be
<?php
private function checkCredentials() {
$user = auth()->user();
if (!is_admin() && !$user->branch_id && !$user->department_id) {
return redirect()->route('documents.add')->with([
'class' => 'alert-danger',
'message' => 'Ask your administrator to assign you a branch and department first',
]);
}
}
public function create()
{
$this->checkCredentials();
//...
return view('main/documents/create', compact('data'));
}

How To Create Conditional for Unique Validation (UPDATE/PATCH) on Form Request

I'm trying to get my controller cleaner by moving 'validation request' into a form request called 'BookRequest'.
The problem is on the update process, I try to create a condition to check, if it PATCH or POST with the following codes
MyRequest.php
public function rules()
{
// Check Create or Update
if ($this->method() == 'PATCH')
{
$a_rules = 'required|string|size:6|unique:books,column2,' .$this->get('id');
$b_rules = 'sometimes|string|size:10|unique:books,column3,' .$this->get('id');
}
else
{
$a_rules = 'required|string|size:6|unique:books,column2';
$b_rules = 'sometimes|string|size:10|unique:books,column3';
}
return [
'column1' => 'required|string|max:100',
'column2' => $a_rules,
'column3' => $b_rules,
'column4' => 'required|date',
'column5' => 'required|in:foo,bar',
'column6' => 'required',
'column7' => 'required',
'column8' => 'required',
];
}
.$this->get('id') it failed, the form still treat the unique on the update.
Controller#update
public function update($id, BookRequest $request)
{
$book = Book::findOrFail($id);
$input = $request->all();
$book->update($request->all());
return view('dashboards.book');
}
Controller#edit
public function edit($id)
{
$book = Book::findOrFail($id);
return view('dashboards.edit', compact('book'));
}
Controller#create
public function create()
{
return view('dashboards.create');
}
Controller#store
public function store(BookRequest $request)
{
$input = $request->all();
$book = Book::create($input);
return redirect('dashboards/book/index');
}
I try the alternative .$book->id, and it throw me an ErrorException Undefined variable: book
Any suggestion? I'm using Laravel 5.2 by the way
You are using book as your route parameter but trying to get with id. try this-
if ($this->method() == 'PATCH')
{
$a_rules = 'required|string|size:6|unique:books,column2,' .$this->route()->parameter('book');
$b_rules = 'sometimes|string|size:10|unique:books,column3,' .$this->route()->parameter('book');
}
Hope it helps :)

Resources