Home > Enterprise >  How can i fix this error? Class "App\Department" not found (View: E:\proiecte\HRmanagem
How can i fix this error? Class "App\Department" not found (View: E:\proiecte\HRmanagem

Time:10-06

Can you please help me with this error Class "APP\Department" not found (View: E:\proiecte\HRmanagement-app\resources\views\admin\user\create.blade.php). I'm trying to do a HR management App. And in my form I want to bring sa departments and rols.

Here is my UserControle

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Department;
use App\Role;


class UserController extends Controller
{
    //am creat functia pentru a afisa departamentele
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'firstname'=>'required',
            'lastname'=>'required',
            'email'=> 'required | string | email | max:255 | unique:users',
            'password'=>'required|string',
            'department_id'=>'required',
            'role_id'=>'required',
            'image'=>'required|mimes:jpeg,jpg,png',
            'start_from'=>'required',
            'designation'=>'required',
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

my create.blade.php

    use App\Department;
@extends('admin.layouts.master')

@section('content')
<div class="container mt-5">
    <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
                <li class="breadcrumb-item active" aria-current="page">Register employee
                    
                </li>
              </ol>
            </nav>
        <form action="{{route('users.store')}}" method="post" enctype="multipart/form-data">@csrf

    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">General Information</div>
                <div class="card-body">
                    <div class="form-group">
                        <label>First name</label>
                        <input type="text" name="firstname" class="form-control" required="">
                    </div>
                    <div class="form-group">
                        <label>Last name</label>
                        <input type="text" name="lastname" class="form-control" required="">
                    </div>
                    <div class="form-group">
                        <label>Address</label>
                        <input type="text" name="address" class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label>Mobile number </label>
                        <input type="number" name="mobile_number" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Department</label>
                         <select class="form-control" name="department_id"  required="">
                             
                           @foreach(APP\Department::all() as $department)
                            <option value="{{$department->id}}">{{$department->name}}</option>
                           @endforeach
                            
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Designation</label>
                        <input type="text" name="designation" class="form-control" required="">
                    </div>
                    <div class="form-group">
                        <label>Start date</label>
                        <input type="date" name="start_from" class="form-control" placeholder="dd-mm-yyyy" required="">
                    </div>
                    <div class="form-group">
                        <label>Image</label>
                        <input type="file" name="image" class="form-control" accept="image/*" required="">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">Login Information</div>
                <div class="card-body">
                    <div class="form-group">
                        <label>Email </label>
                        <input type="email" name="email" class="form-control" required="">
                    </div>
                    <div class="form-group">
                        <label>Password</label>
                        <input type="password" name="password" class="form-control" required="">
                    </div>
                    <div class="form-group">
                        <label>Role</label>
                        <select class="form-control" name="role_id" required="">
                        @foreach(App\Role::all() as $role)
                            <option value="{{$role->id}}">{{$role->name}}</option>
                        @endforeach 
                            
                        </select>
                    </div>
                    
                </div>

            </div>
            <br>
            <div class="form-group">
                <button class="btn btn-primary " type="submit">Submit</button>
            </div>
        </div>
      
    </div>
</form>
</div>
    
@endsection

and web.php

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::post('departments','Departmentcontroller@store');
Route::resource('departments', 'Departmentcontroller');
Route::view('hrmanagement-app','admin.create');
Route::post('roles','RoleController@store');
Route::resource('roles', 'RoleController');
Route::post('users','UserController@store');
Route::resource('users', 'UserController');

CodePudding user response:

You have a typo in your create.blade.php blade:

@foreach(APP\Department::all() as $department)
    <option value="{{$department->id}}">{{$department->name}}</option>
@endforeach

You are using APP\Department::all(), with all caps. Instead, use this:

@foreach(App\Department::all() as $department)
    <option value="{{$department->id}}">{{$department->name}}</option>
@endforeach

CodePudding user response:

You have APP\Department instead of App\Department in create.blade.php

CodePudding user response:

The typology must be respected as the others have specified, APP\Department is different from App\Department

But Good practices is to create required variables in controller, and just send them to the view, example is:

    public function create()
    {
        $departments = App\Department::all();   
        return view('admin.user.create', [
            'departments' => $departments
        ]);
        //return view('admin.user.create', compact('departments'));
    }

This type of organization allow you to manipulate data that will be send in your view, you can manage it with more operation on data that will be send. something that can be confused if it is found in the view.

CodePudding user response:

Why you want to use model in view. This is not a standard way to implement models in view and not secure.

Remove use App\Department; from create.blade.php

You already mentioned your model in controller so it will works fine.

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Department; 
use App\Role;

Do like this in your controller change create method

 public function create()
    {
        $departments = Department::get()->all();
        
        return view('admin.user.create', compact('departments'));
    }

Changes these lines from create.blade.php

@foreach($departments as $department)
     <option value="{{$department->id}}">{{$department->name}}</option>
@endforeach

Remove this lines from your web.php

Route::post('departments','Departmentcontroller@store');

this line will do everything like loading index, create or update page, even they store or update and delete as well

  • Related