I have route like this
Route::apiResource('article.comment', ArticleCommentController::class)->except('destroy', 'update');
Route::apiResource('article.comment', ArticleCommentController::class)->only('update')->withTrashed();
But, when I am check the routing using php artisan r:l
, show error like this
BadMethodCallException
Method Illuminate\Routing\PendingResourceRegistration::withTrashed does not exist.
at D:\dev\api_app\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:113
109▕ */
110▕ public function __call($method, $parameters)
111▕ {
112▕ if (! static::hasMacro($method)) {
➜ 113▕ throw new BadMethodCallException(sprintf(
114▕ 'Method %s::%s does not exist.', static::class, $method
115▕ ));
116▕ }
117▕
• Bad Method Call: Did you mean Illuminate\Routing\PendingResourceRegistration::withoutMiddleware() ?
1 D:\dev\api_app\routes\api.php:88
Illuminate\Routing\PendingResourceRegistration::__call("withTrashed", [])
2 D:\dev\api_app\vendor\laravel\framework\src\Illuminate\Routing\Router.php:423
Illuminate\Routing\RouteFileRegistrar::{closure}(Object(Illuminate\Routing\Router))
why, method ->withTrashed()
can't add to route resource?
CodePudding user response:
Your Eloquent model should use the Illuminate\Database\Eloquent\SoftDeletes
trait to make use of the withTrashed()
method.
ArticleComment::class
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class ArticleComment extends Model
{
use SoftDeletes;
}
Updated :
Why do you use withTrashed()
method in route?
you should use withTrashed()
in Eloquent Queries ( in your controller)
Change your routes to :
Route::apiResource('article.comment', ArticleCommentController::class)->only('update');
And in your controller get items with withTrashed()
method.
For example ( in your Controller ) :
$articleComment = ArticleComment::withTrashed()->find($your_update_id);
// Then do what you want ...