質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

2回答

4658閲覧

Laravelでのエラー、”Attribute [department] does not exist”の解決方法について。

pei123

総合スコア10

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2019/02/25 18:09

前提・実現したいこと

Laravel初心者です。現在Laravelを使ってコードを書いています。
web.phpをいじっていたらエラーが発生しました。
どのように解決すればいいのか御指南いただけないでしょうか?
宜しくお願い致します。

発生している問題・エラーメッセージ

InvalidArgumentException
Attribute [department] does not exist.

###該当のソースコード
web.php

<?php Route::patch('/departments/{department}', 'DepartmentsController@update'); Route::delete('/departments/{department}', 'DepartmentsController@destroy'); Route::department('/departments/{department}', 'CommentsController@store'); Route::delete('/departments/{department}/comments/{comment}', 'CommentsController@destroy'); Route::patch('/departments/{department}', 'DepartmentsController@update'); Route::department('/departments', 'DepartmentsController@store'); Route::get('/departments/{department}/edit', 'DepartmentsController@edit'); Route::get('/departments/create', 'DepartmentsController@create'); // }); Route::get('/', 'DepartmentsController@index'); // Route::get('/Departments/{id}', 'DepartmentsController@show'); Route::get('/departments/{department}', 'DepartmentsController@show')->where("department", "[0-9]+"); Route::get('/contacts', 'ContactsController@index'); // Route::get('/Contacts/{id}', 'ContactsController@show'); Route::get('/Contacts/{contact}', 'ContactsController@show'); DepartmentsController.php <?php namespace App\Http\Controllers; use Auth; use Illuminate\Http\Request; use App\Department; use App\Image; use App\Http\Requests\DepartmentRequest; use Illuminate\Http\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile; class DepartmentsController extends Controller { public function index() { // $departments = \App\Department::all(); // $departments = Department::all(); // $departments = Department::orderBy('created_at', 'desc')->get(); $departments = Department::latest()->get(); // $departments = []; // dd($departments->toArray()); // dump die // return view('departments.index', ['departments' => $departments]); return view('departments.index')->with('departments', $departments); } // public function show($id) { public function show(Department $department) { // $department = Department::find($id); // $department = Department::findOrFail($id); // dump($department); $department->load(['comments', 'mainImage']); // dd($department); return view('departments.show')->with('department', $department); } public function create() { return view('departments.create'); } public function store(DepartmentRequest $request) { $department = new Department(); $department->title = $request->title; $department->body = $request->body; $department->user_id = Auth::id(); $department->save(); $this->updateImage($request, $department); return redirect('/'); } public function edit(Department $department) { return view('departments.edit')->with('department', $department); } private function updateImage($request, $department) { if ($request->hasFile('main_image_filename')) { if (!empty($image)) { // imageの差し替え $image->delete(); } $image = new Image; $image->storeDepartment($request->main_image_filename); } if (!empty($image)) { $image->alt = $request->get('image_alt'); $image->department_id = $department->id; $image->save(); $department->main_image_id = $image->id; $department->save(); } } public function update(DepartmentRequest $request, Department $department) { $department->title = $request->title; $department->body = $request->body; $department->user_id = Auth::id(); $department->save(); $this->updateImage($request, $department); return redirect('/'); } public function destroy(Department $department) { $department->delete(); return redirect('/'); } } ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Route::department('/departments/{department}', 'CommentsController@store');

これが原因です。
ルートのメソッドにRoute::departmentなんてものはなく、使用できるのは次の6つです。

Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback);

https://laravel.com/docs/5.7/routing#basic-routing

解決方法としては、
Controllerのメソッド名がstoreなのでRoute::postだとは思いますが、そこは要件に合わせてRoute::departmentを上記のどれかに変更してください。

投稿2019/02/25 22:58

編集2019/02/25 23:10
xenbeat

総合スコア4258

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

PHP

1Route::get('/departments/{department}', 'DepartmentsController@show')->where("department", "[0-9]+");

でshow()が受け取るのは整数であるはずです。

一方で、関数の定義では

PHP

1public function show(Department $department) {

とモデル(ですよね?)を受け取るようになっている、という齟齬があります。

ここは整数を受け取るようにし、そのIDでモデルを検索する、とすべきかと思います。

PHP

1public function show($department) { 2 $dep = Department::find($department); 3

つまり現在コメントアウトされている方が正しいと思われます。

投稿2019/02/25 23:06

kazto

総合スコア7196

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kazto

2019/02/25 23:08

む、Route::department 見つけられなかった… xenbeatさんのおっしゃる通りかと思います。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問