Reputation: 691
I'm using Laravel 5.2 and I renamed my Model from "Articles" to "Article". I did the same for the class as well as the controller, but I am having an error in "show" method in my controller.
ReflectionException in Container.php line 738:
Here is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\ArticleRequest;
use App\Http\Controllers\Controller;
use App\Article;
use App\User;
use App\Tag;
use Auth;
class ArticlesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$articles = article::latest('created_at')->get();
return view('articles.home', compact('articles'));
}
public function show(Article $article)
{
return view('articles.show', compact('article'));
}
public function create()
{
$tag = Tag::lists('name', 'id');
return view('articles.create', compact('tag'));
}
public function store(ArticleRequest $request)
{
$article = Auth::user()->article()->create($request->all());
$article->tags()->attach($request->input('tags'));
flash('Your Article Has Been Successfully Saved','Good Job');
return redirect('articles');
}
public function edit(Article $articles)
{
return view('articles.edit', compact('articles'));
}
public function update(Article $articles, ArticleRequest $request)
{
$articles->update($request->all());
return redirect('articles');
}
}
Here is my Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $table = "articles";
protected $fillable = [
'title',
'body',
'published_at',
'user_id'
];
public function user()
{
return $this->belongsTo('App\User');
}
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
}
Here is the route registered:
Route::resource('articles','ArticlesController');
Finally Here's my show.blade.php:
@extends('app')
@section('content')
<h2>{{ $article->title }}</h2>
<div class="body">
<p>{{ $article->body }}</p>
</div>
@stop
Upvotes: 4
Views: 2806
Reputation: 25221
If you're using explicit route model binding, did you update RouteServiceProvider
with the new class name?
If you aren't using explicit binding, your route is articles
and your model is Article
so Laravel is not able to implicitly bind it anymore. Change the route to Route::resource('article')
and it should work.
Upvotes: 6