samedi 31 octobre 2015

Adding an update method laravel

I have a register function done as shown below and was wondering how can I add an update method. Do I just add another route for my update and a controller function for it? Am I on the right track? Is the below correct? When run, it tells me fatal exception, unexpected return ..

route.php

Route::post('manage_accounts', 'ManageAccountsController@register');
Route::post('manage_accounts', 'ManageAccountsController@update');

controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\StoreNewUserRequest;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Hash;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Role;

class ManageAccountsController extends Controller
{
    public $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function index() 
    {
        $users = User::orderBy('name')->get();
        $roles = Role::all();

        return view('manage_accounts', compact('users', 'roles'));
    }

    public function register(StoreNewUserRequest $request)
    {
        // process the form here
        $this->userRepository->upsert($request);
        Session::flash('flash_message', 'User successfully added!');

        return redirect()->back();
    }

    public function update(UpdateUserRequest $request)
    {
        $this->userRepository->upsert($request)
        //Session::flash('flash_message', 'User successfully updated!');

        return redirect()->back();
    }
}

class UserRepository {

    public function upsert($data)
    {

            // Now we can separate this upsert function here
        $user = new User;
        $user->name     = $data['name'];
        $user->email    = $data['email'];
        $user->password = Hash::make($data['password']);
        $user->mobile   = $data['mobile'];
        $user->role_id  = $data['role_id'];

            // save our user
        $user->save();

        return $user;
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire