Really struggling here, trying to display the posts on my page, bare in mind these posts are displaying on other pages. However i cannot display them on my index.php page.
welcome.blade.php
#if(count($posts) > 1)
#foreach($posts as $post)
<h2>{{$post->title}}</h2>
#endforeach
#else
</p>no posts found</p>
#endif
WelcomeController.php
public function index()
{
$posts = Post::all();
return view('Pages.welcome')->with('posts', $posts);
}
PostsController.php
public function index()
{
$posts = Post::all();
return view('posts.index')->with('posts', $posts);
}
Web.php (routes)
Route::get('/', 'PageController#index');
Route::get('/welcome','PageController#Welcome');
Route::get('/services', 'PageController#services');
Route::get('/register', 'PageController#register');
Route::get('/Create', 'PageController#Create');
Route::get('/search', 'PageController#search');
Route::get('/payment', 'PageController#Payment');
Route::resource('posts', 'PostsController');
Route::resource('search', 'SearchController');
Route::resource('reviews', 'ReviewsController');
HomeController.php
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
I have put postscontroller because this controller works and displays data to other pages (posts.index), however when i try to display data on Pages.index i am unable to do so ?
Make sure you are accessing the right path on your URL. In this case you should access localhost:8000/welcome .
In your route
Route::get('/index','PageController#HomePage');
Shoud be
Route::get('/welcome','WelcomeController#index');
because your controller file name is WelcomeController.php and you need to redirect to your index function in that controller.
Make sure you are redirecting to the right view in your controller
public function index()
{
$posts = Post::all();
return view('Pages.welcome', compact('posts'));
}
Here you are seeking the welcome.blade.php file in your /Pages directory
Your controllers and routes are fine but you miss-spell foreach
#foreac h($posts as $post)
Replace with :
#foreach($posts as $post)
now this should work
Related
when I enter the detail page in Laravel, it gives an error. I don't understand where the error is coming from. My codes are as follows.
where is the problem? actually it says it's empty, but it needs to pull data from $blog on controller side.
Controller:
public function show($id)
{
$blog = Blog::where('id',$id)->first();
return view('front.detail',compact('blog'));
}
routes/web.php:
Route::prefix('{lang?}')->middleware('locale')->group(function() {
Route::get('/', [MainController::class, 'index'])->name('home');
Route::get('/about', [MainController::class, 'about'])->name('about');
Route::resource('/blogs', MainController::class)->only([ 'show']);
});
detail.blade.php:
<li>
<h2>{{$blog->title}}</h2>
<p>{!! $blog->text !!}</p>
</li>
If you want to get a model by the main column you use find not where and the column:
public function show($id)
{
$blog = Blog::find($id);
return view('front.detail',compact('blog'));
}
Then, find can return the Model (hence an object) or null, so in your case it is not finding the model by ID.
What you should use is findOrFail so it throws an exception if it did not find the model:
public function show($id)
{
$blog = Blog::findOrFail($id);
return view('front.detail',compact('blog'));
}
Then you can continue debugging why it is not found
Doesn't appear you are passing the blog ID in with your URL:
Route::resource('/blogs', MainController::class)->only([ 'show']);
Try changing your route to this:
Route::resource('/blogs/{id}', MainController::class)->only([ 'show']);
Your URL should be something like (where 5 is the blog ID):
http://yourdomain.com/blogs/5
You could also use binding:
// Route
Route::get('/blogs/{blog}', [MainController::class, 'show']);
public function show(Blog $blog)
{
return view('front.detail', ['blog' => $blog]);
}
More info can be found here:
Route model binding
I have a page of posts. I added an edit icon so that when I click on it, it should show me the edit page. However, when I click, it displays an error.
Blade/Form
<form method="POST" action="{{ route('post.update',
['id'=>$post->id]) }}" enctype="multipart/form-data">
#csrf
Route
Route::get('post/edit/{id}', 'PostController#edit')
->name('post.edit');
The following is the edit() method inside the PostController.
public function edit($id)
{
$post = Post::find($id);
return view('posts.edit')->with('post', $post);
}
Try using findOrFail to throw a 404 if you pass an id that doesn't exist.
public function edit($id)
{
$post = Post::findOrFail($id);
return view('posts.edit')->with('post', $post);
}
or bind the model in the route
Route::get('post/edit/{post}', 'PostController#edit')->name('post.edit');
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
In my Laravel project with localization I made middleware, route group and all parameters, language switch work correct but when I click to send id by
I get the error:
Missing required parameters for [Route: products] [URI:
{lang}/products/{id}]
My Routes:
Route::group(['prefix' => '{lang}'], function () {
Route::get('/', 'AppController#index')->name('home');
Route::get('/categories', 'AppController#categories')->name('categories');
Route::get('products/{id}', 'AppController#products')->name('products');
Auth::routes();
});
My Middleware:
public function handle($request, Closure $next)
{
\App::setLocale($request->lang);
return $next($request);
}
My AppController:
public function products($id)
{
$products = Category::with('products')->where('id', $id)->get();
return view('products', compact('products'));
}
this is the URL:
http://127.0.0.1:8000/fa/products/1
if I change the above URL manually it works and shows the page:
http://127.0.0.1:8000/1/products/1
But if I click on:
I receive the error.
Since you added a route prefix the first parameter of the products method in your controller will be lang and the second one id.
This should fix the controller:
public function products($lang, $id)
{
$products = Category::with('products')->where('id', $id)->get();
return view('products', compact('products', 'lang'));
}
You need to use a key-value array in route('products', ['lang'=>app()->getLocale(), 'id'=>$category->id]) or whatever your route parameters are named in the original route.
Ref. Laravel Named Routes
PS. as Remul notes, since you have a lang param (as route prefix) the first param in your controller will be $lang then $id
public function products($lang, $id)
{
$products = Category::with('products')->where('id', $id)->get();
return view('products', compact('products'));
}
I created a customerController with Laravel resource. Now I want to show the list of the customers from the database using the index() method and it doesn't work when I visit the customer/index route. However if use the show() method instead and visit the customer/show route, it works perfectly.
Why does this behave this way and how do I get the index() method to do this?
class CustomerController extends Controller
{
public function index()
{
$customers = Customer::all();
return view('customer')->with('customers' , $customers);
}
public function show($id)
{
// adding the code in the index() method here makes the code run
// as expected
}
}
customer.blade.php
<ul>
#foreach($customers as $customer)
<li>{{$customer->name}}</li>
#endforeach
</ul>
routes/web.php
Route::resource('customer' , 'CustomerController');
I expect the output to be:
.sucre
.hameed
.micheal
I just learned how to use resource on Laravel 8.
Which version are you using?
Run php artisan route:list on your console to see every route available and their names.
To see all customers through your index function do the following:
public function index()
{
$customers = Customer::all();
return view('customer', [
'customers' => $customers
]);
}
Now just access your customer.blade.php file on browser.
PS: Don't forget of #foreach
#foreach ($customers as $customer)
{{ $customer->attribute }}
#endforeach
Hope this could be useful.
Every post in the list has Edit link
Edit
routes
Route::get('dashboard/posts/{id}/edit', 'PostsController#edit');
Route::put('dashboard/posts/{id}', 'PostsController#update');
methods in PostsController
public function edit($id)
{
$post = Post::findOrFail($id);
return view('dashboard.edit', compact('post'));
}
public function update($id, PostRequest $request)
{
$post = Post::findOrFail($id);
$post->update($request->all());
return redirect ('dashboard');
}
but on clink on the Edit button I get an error
NotFoundHttpException in RouteCollection.php line 161:
What's wrong? How to fix it?
In the route file you have written posts and in the href post
{{ URL::to('dashboard/post/' . $post->id . '/edit') }}
Route::get('dashboard/posts/{id}/edit', 'PostsController#edit');