this is my first post on stackoverflow. I've created my first project with Laravel a few months ago, it contains some models, controllers, observers etc, but I have stucked in the last one, which was created in the same way as previous ones (I guess...), I can't find what I've missed this time. Creating records work fine, but editing or showing unfortunately no: function edit(ExpenseCourse $expenseCourse) should show array with 1 record indexed in URL http://.......expensecourses/1/edit but it shows empty array. Table expense_courses is not empty, it contains one previously created record with id = 1 The same situation is with the show method.
Here is my ExpenseCourseController.php
<?php
namespace App\Http\Controllers;
use App\Models\ExpenseCourse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Auth;
class ExpenseCourseController extends Controller
{
public function edit(ExpenseCourse $expenseCourse)
{
dd($expenseCourse);
$depts = DB::table('depts')
->where('company_id','=',1)
->orderby('dept_shortname')
->get();
return view('expensecourses.edit',compact('expenseCourse'), [
'depts' => $depts,
]);
}
}
Here is my ExpenseCourse.php model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ExpenseCourse extends Model
{
use HasFactory;
protected $fillable = [
'dept_id', 'name', 'company_name', 'year', 'hours_count', 'estimated_participants_number', 'estimated_price', 'real_price', 'status_id', 'planned'
];
}
Here is my migration 2021_12_17_140931_create_expense_courses_table.php file.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExpenseCoursesTable extends Migration
{
public function up()
{
Schema::create('expense_courses', function (Blueprint $table) {
$table->id();
$table->integer('dept_id');
$table->string('name');
$table->string('company_name');
$table->year('year');
$table->integer('hours_count');
$table->integer('estimated_participants_number');
$table->integer('estimated_price');
$table->integer('real_price')->nullable();
$table->integer('status_id')->default(1);
$table->boolean('planned')->default(1);
$table->integer('created_by')->nullable();
$table->integer('updated_by')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('expense_courses');
}
}
Here is my route web.php file
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ParticipantController;
use App\Http\Controllers\CoursesTypesController;
use App\Http\Controllers\TrainerController;
use App\Http\Controllers\CourseController;
use App\Http\Controllers\ExpenseCourseController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\DeptController;
use App\Http\Controllers\PositionsTypesController;
use App\Http\Controllers\PrintTemplatesController;
use App\Http\Controllers\ReportsController;
Route::get('/', function () {
return view('welcome2');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::resource('participants', ParticipantController::class)->middleware('auth');
Route::resource('coursestypes', CoursesTypesController::class)->middleware('auth');
Route::resource('trainers', TrainerController::class)->middleware('auth');
Route::resource('courses', CourseController::class)->middleware('auth');
Route::resource('expensecourses', ExpenseCourseController::class)->middleware('auth');
Route::resource('companies', CompanyController::class)->middleware('auth');
Route::resource('depts', DeptController::class)->middleware('auth');
Route::resource('positionstypes', PositionsTypesController::class)->middleware('auth');
Route::get('participants/fill/{course_id}', [ParticipantController::class, 'fill'])->name('participants/fill')->middleware('auth');
Route::get('participants/create/{course_id}', [ParticipantController::class, 'create'])->middleware('auth');
Route::get('participants/edit/{participant_id}/{course_id}', [ParticipantController::class, 'edit'])->middleware('auth');
Route::get('search/results', [CourseController::class, 'results'])->middleware('auth');
Route::get('search/query', [CourseController::class, 'search'])->middleware('auth');
Route::get('companies/getdepts/{company_id}', [CompanyController::class, 'getdepts'])->name('companies/getdepts')->middleware('auth');
Route::get('prints/print/{participant_id}', [PrintTemplatesController::class, 'print'])->name('prints/print')->middleware('auth');
Route::get('reports/quantity', [ReportsController::class, 'quantity_report'])->middleware('auth'); // raport ilościowy
Route::get('reports/annual', [ReportsController::class, 'annual_report'])->middleware('auth'); // raport roczny
URL::forceScheme('https');
I would be gratefull if you could help me. Thanks.
CodePudding user response:
This is because variable and constants are case sensitive in php and in your case name of Route::resource
does not match parameter name in the method of the controller class. You can change either your route name or your parameters in edit
and show
method.
1. Change route name:
Use camelcase in route name, change expensecourses
to expenseCourses
Route::resource('expenseCourses', ExpenseCourseController::class)->middleware('auth');
OR
2. Change parameter name
public function edit(ExpenseCourse $expensecourse){}
public function show(ExpenseCourse $expensecourse){}
CodePudding user response:
Of course, you are absolutely right! My bad. I've created this controller on the base of previous one which had one-word name, so I've completely missed lettercase issue. Thanks a lot for your help!