Having Laravel 8 routing and controller issue
I'm bit new to laravel8 and got hang of it and I'm building a news portal as a project and learn at same time.
On the main index page I like to display some data such posts and categories and so on, where the data comes different tables in the db and from same controllers but different methods to the same route.
So I’m bit stuck here, the problem I’m having is that it’s not working
This is my code
Here are the routs
// main Index Page
Route::get('/','App\Http\Controllers\Home_pageController#categories);
// Index Latest Posts
Route::get('/','App\Http\Controllers\Home_pageController#homePageLatestPosts');
Methods in the Controllers
This is the method for displaying the latest posts in a sidebar
// Display latest limit by 10 the posts on home page
public function homePageLatestPosts(){
// $all_posts = Blogs::all();
$latest_posts = DB::table('blogs')->join('users', 'users.id', '=', 'blogs.added_by')->select('users.*','blogs.*')->orderBy('blogs.created_at', 'desc')->paginate(5);
// dd($latest_posts);
return view('welcome' , ['latest_posts'=>$latest_posts]);
}
// Show Categories
public function categories(){
$categories = DB::table('categories')->orderBy('category_name', 'desc')->get();
// dd($categories);
return view('welcome',['cats'=>$categories]);
}
I would like to know what the problem is and if there is a solution and if its the right approach that I', taking
I have googled around for a solution but not being able to solve it
thanks in advance
thanks for the responses.
i got a way around by fetching the data from DB in one method and passed them to the view for display.
use App\Models\Blog;
use App\Models\User;
use App\Models\Categories;
public function welcome(){
// Latest Posts
$latest_posts = DB::table('blogs')->join('users', 'users.id', '=', 'blogs.added_by')->select('users.*','blogs.*')->orderBy('blogs.created_at', 'desc')->paginate(10);
// Home Page Categories
$categories = DB::table('categories')->orderBy('category_name', 'desc')->get();
// Return
return view('welcome' , [ 'categories'=>$categories,'latest_posts'=>$latest_posts]);
}
Related
I'm new to laravel and I'm still trying to learn.
This my code in my controller:
public function index(User $user){
$posts = $user->posts()->with(['user', 'likes']);
return view('users.posts.index', [
'user' => $user,
'posts' => $posts,
]);
}
And this is the code on blade.php
{{$user->name}}
I've also tried checking if the code:
dd($posts = $user->posts()->with(['user', 'likes']));
and it seems fine since it's returning data from the database. With these codes it was supposed to show the User's Post on a different page when you click on the User's name. The problem is that it only shows the user's name but not the user's post. I'm only following a tutorial but the result was different with the tutorial and the one that I'm doing. Can someone please help?
I suppose you are trying to lazy eager load the posts relationship on this $user and load the user and likes relationship for those posts. You can try to use load to load these relationships on the Collection:
$posts = $user->posts->load('user', 'likes');
Or load on the User instance:
$user->load('posts.user', 'posts.likes');
$posts = $user->posts;
If you are not trying to have these relationships loaded on the $user but you just want the posts with their relationships you can eager load them off of the relationship itself:
$posts = $user->posts()->with('user', 'likes')->get();
I'm trying to display posts from the database, but i want to have the latest on top. This means I have to
do this inside of my HomeController.php:
$posts = Post::all()->sortByDesc('id');
return view('home', ['posts' => $posts]);
But when the site grows up, it might be complicated to find the particular post, so I decided to implement pagination. Unfortunately, pagination only works when I use this statement:
$posts = Post::paginate(10);
return view('home', ['posts' => $posts]);
When I'm trying to do things like that:
$posts = Post::all()->sortByDesc('id')->paginate(10);
My site throws an error, no matter what statement I use to display reversed posts and paginate them. Please help me and thank you guys for your every response.
You can use orderBy and pass 'DESC' to order the result in descending order.
$posts = Post::orderBy('id', 'DESC')->paginate(10);
Eloquent has a "latest" method that should help.
https://laravel.com/docs/7.x/queries#ordering-grouping-limit-and-offset
I intend to paginate a laravel eloquent model using a where clause. I expect the page links to show and be clickable leading to the clicked page. This is the default controller in which I paginate without a where clause.
public function index(Request $request){
$users = User::paginate(12)->onEachSide(1);
return view('/**',compact('users'));
}
I am able to paginate through till the last page. This is the route (I have placed the name is asterisks for confidentiality.:
Route::get('/**', 'SearchController#index');
This functions better, however I try to limit the rows with a where clause:
public function search(Request $request){
$location = $request->location;
$talent = $request ->talent;
$users = User::where('talent',$talent)->where('location',$location)->paginate(12);
return view('/searchResult',compact('users');
}
This is the route:
Route::get('/**', 'SearchController#search');
However, in the second case, the pagination result returns blank when I try to navigate. I am not sure why this is happening.
On the front view, I have:
{{$users->links()}}
This code works and I am now able to paginate effectively. Answer by lufc in the comment though. However, I am curious about the mechanism.
{{ $users->appends(Request::except('page'))->links() }}
I have a simple relationship Post x Files in my app (one post has many files and one file is associated to only one post). To make things more agile, I store the posts on the cache whenever it is not changed so I don't need to query the database again. The problem that I noticed is that only the Posts are being stored in the cache and not the files (and I suppose the problem is because the way I query them). My code is:
class Post extends Model
{
public function files(){
return $this->hasMany('Ibbr\File');
}
}
The function for obtaining the posts:
public static function pegaPostsBoard($nomeBoard)
{
$chave = 'posts_board';
if(Cache::has($chave))
return Cache::get($chave);
$posts = Post::orderBy('updated_at', 'desc')->where('board', $nomeBoard)->where('lead_id', null)->paginate(10);
Cache::forever($chave, $posts); //I "forget" the cache whenever the post is changed
return $posts;
}
I also tried adding ->join('files', 'posts.id', '=', 'files.post_id') before adding it to the cache but it didn't work. How did I noticed that the files are not being cached? Well, I reset the database so it cleans all the rows and I noticed that If I F5 the page the posts are still there (because they are cached) but not their files. So my question is how do I query it in such a way that the files are also stored?
Use with to append relationship to query results
$posts = Post::with('files')
->orderBy('updated_at', 'desc')
->where('board', $nomeBoard)
->where('lead_id', null)
->paginate(10);
I'm trying to show the last reply that was left on a post. But only the name and date of it. I tried the following things
What I think this one should do is look at the replies to the post. Sort them by ID and then display the first one's username. But it doesn't work.
$topic->replies->sortBydesc('id')->first()->user->username }}
I also tried this one where I requested all the replies in the entire forum and displayed the last one. It worked but I wasn't the one related to the post.
{{ $allposts->sortBydesc('id')->first()->user->username }}
Does anyone know how I can make this work?
Currently, i'm passing these two into the view (There is nothing wrong with these, they work but I think there should be something added)
public function show($id)
{
$topics = Topic::with('theme')->find($id);
$theme = Theme::with('topics')->find($id);
return view('themes.theme')->with('topics', $topics)->with('theme', $theme);
}
Thanks in advance
If you are trying to get topic's replies you need to eager load. Also if you have relation with user who replied eager load it as well.
Use latest() method for getting latest replies.
For only name and date we have select() .
public function show($id)
{
$topics = $topic->with('replies' => function($query) {
$query->with('user')->select('name', 'created_at')->latest();
});
$theme = Theme::with('topics')->find($id);
return view('themes.theme', compact('topics', 'theme'));
}