CRUD of users, add more edit and update routes

1

I currently have a CRUD for the users. I'm using the edit and update path to modify the name and email data, but I need another edit path and an update path to change the user's password. I want to work them separately.

The question is how to generate those new routes of edit2 and update2. Try adding them to the user's driver and I do not create them.

I put the code of the user's driver in which I added the function of edit2 to create the route but it does not work:

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\UserStoreRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Http\Controllers\Controller;
use App\User;

use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;



class UserController extends Controller
{

public function __construct()
{
    $this->middleware('auth');
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $users = User::orderBy('id', 'DESC')->paginate();

    return view('admin.users.index', compact('users'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
     return view('admin.users.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(UserStoreRequest $request)
{
    //validacion
    $user = User::create($request->all());

    return redirect()->route('users.edit', $user->id)
        ->with('info', 'Usuario creado con exito');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    $user = User::find($id);

    return view('admin.users.show', compact('user'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $user = User::find($id);

    return view('admin.users.edit', compact('user'));
}

public function edit2($id)
{
    $user = User::find($id);

    return view('admin.users.edit2', compact('user'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(UserUpdateRequest $request, $id)
{
    $user = User::find($id);

    $user->name     = $request->name;
    $user->email     = $request->email;

    $user->save();  

    return redirect()->route('users.index')
                     ->with('info','El usuario ha sido actualizado');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $user = User::find($id)->delete();

    return back()->with('info', 'Eliminado correctamente');
}
}

I also place the web route code

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
return view('inicio');
});

Route::get('instituto', function () {
return view('instituto');
})->name('instituto');



Route::redirect('news', 'blog');

Auth::routes();

//Web
Route::get('blog', 'Web\PageController@blog')->name('blog');
Route::get('entrada/{slug}',   'Web\PageController@post')->name('post');
Route::get('categoria/{slug}', 'Web\PageController@category')->name('category');
Route::get('etiqueta/{slug}',  'Web\PageController@tag')->name('tag');
//Admin
Route::resource('tags',       'Admin\TagController');
Route::resource('categories', 'Admin\CategoryController');
Route::resource('posts',      'Admin\PostController');
Route::resource('users',      'Admin\UserController');

EDIT1: Add the routes and they are recognized by the route: list

But I still get the message that it does not recognize the route, they were also added to the controller.

    
asked by Kinafune 13.04.2018 в 02:02
source

1 answer

1

From what I understand, you try to generate a new method for edit and another one for update , with their corresponding routes.

To begin with, we must define these new routes. As we already have the resource routes, we must define before the routes that we want to add, as follows:

Route::get('users/{user}/edit2', 'Admin\UserController@edit2');
Route::put('users/{user}/update2', 'Admin\UserController@update2');
Route::resource('users', 'Admin\UserController');

This way we have the resource routes ( Laravel documentation ), and two new routes that you can call as best suits you. Next we define the methods in the controller:

public function edit2($id)
{
    $user = User::find($id);

    return view('admin.users.edit2', compact('user'));
}

public function update2(UserUpdateRequest $request, $id)
{
    $user = User::find($id);

    // Actualizamos

    $user->save();  

    return redirect()->route('users.index')
                     ->with('info','El usuario ha sido actualizado');
}

If we already have the views and the links / forms call the routes correctly, everything should work correctly. I hope it helps you.

EDIT 1:

I just saw your edit, your problem may be that you're calling from the index.blade.php view to a path defined with the name users.edit2 . As you can see in your capture of the route:list all have a name except the two that we have just defined, to add a name you should leave them in the following way:

Route::get('users/{user}/edit2', 'Admin\UserController@edit2')->name('users.edit2');
Route::put('users/{user}/update2', 'Admin\UserController@update2')->name('users.update2');
Route::resource('users', 'Admin\UserController');

Tell me if you have any problems this way.

    
answered by 13.04.2018 / 10:19
source