Alex1
Alex1

Reputation: 27

ErrorException in Laravel

I created a Laravel project and in this project, I created a controller called PhotoController.php. In this file, the code is this:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class PhotoController extends Controller {
  //Display a listing of the resource.
  //@return \Illuminate\Http\Response
  public function index(){
    return 'index';
  }

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

  //Store a newly created resource in storage.
  //@param \Illuminate\Http\Request $request
  //@return \Illuminate\Http\Response
  public function store(Request $request){
    return 'store, uri: '. $request->path();
  }

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

  //Show the form for editing the specified resource.
  //@param int $id
  //@return \Illuminate\Http\Response
  public function edit($id){
    return 'edit, id: '. $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){
    return 'update, uri: '. $request->path() .' - id: '. $id;
  }

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

For this controller I created this route in web.php:

Route::resource('photo',[PhotoController::class]);

When I start server with command: php artisan serve, an error occurs:

ErrorException Array to String conversion

Under this things, it send me in vendor director in ResourceRegistrar.php. Here it show me line 410 that is:

$action = ['as' => $name, 'uses'=> $controller.'@'.$method].

How can I resolve this error? Thank you

Upvotes: 2

Views: 457

Answers (1)

Mouad ZIANI
Mouad ZIANI

Reputation: 138

For the resource route you don't need to wrap the Controller with an array, you have just to specify your controller namespace as 2nd argument on the resource method

In your case you can do :

Route::resource('photo', PhotoController::class);

Upvotes: 2

Related Questions