i want to create some CMS project with the laravel, when the user trying to click the categories, the app will show the post that only shows the category that user clicked. When the user click the categories, the app says error Bad Method Call. What should i do? Any help will be appreciated.
Here's the code from web.php
<?php
use App\Http\Controllers\Blog\PostsController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'WelcomeController#index')->name('welcome');
Route::get('blog/posts/{post}', [PostsController::class, 'show'])->name('blog.show');
Route::get('blog/categories/{category}', [PostsController::class, 'category'])->name('blog.category');
Route::get('blog/tags/{tag}', [PostsController::class, 'tag'])->name('blog.tag');
Auth::routes();
Route::middleware(['auth'])->group(function () {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('categories', 'CategoriesController');
Route::resource('posts','PostsController');
Route::resource('tags','TagsController');
Route::get('trashed-posts','PostsController#trashed')->name('trashed-posts.index');
Route::put('restore-post/{post}', 'PostsController#restore')->name('restore-posts');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('users/profile', 'UserController#edit')->name('users.edit-profile');
Route::put('users/profile', 'UserController#update')->name('users.update-profile');
Route::get('users','UserController#index')->name('users.index');
Route::post('users/{user}/make-admin', 'UserController#makeAdmin')->name('users.make-admin');
});
Here's the code from PostsController.php
<?php
namespace App\Http\Controllers\Blog;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
use App\Tag;
use App\Category;
class PostsController extends Controller
{
public function show(Post $post) {
return view('blog.show')->with('post', $post);
}
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->posts()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
public function tag(Tag $tag) {
return view('blog.tag')
->with('tag', $tag)
->with('posts', $tag->posts()->simplePaginate(3));
}
}
Here's the code from App\Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function post() {
return $this->hasMany(Post::class);
}
}
Here's the code from category.blade.php
#extends('layouts.blog')
#section('title')
Category {{ $category->name }}
#endsection
#section('header')
<header class="header text-center text-white" style="background-image: linear-gradient(-225deg, #5D9FFF 0%, #B8DCFF 48%, #6BBBFF 100%);">
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h1>{{ $category->name }}</h1>
<p class="lead-2 opacity-90 mt-6">Read and get updated on how we progress</p>
</div>
</div>
</div>
</header><!-- /.header -->
#endsection
#section('content')
<main class="main-content">
<div class="section bg-gray">
<div class="container">
<div class="row">
<div class="col-md-8 col-xl-9">
<div class="row gap-y">
#forelse($posts as $post)
<div class="col-md-6">
<div class="card border hover-shadow-6 mb-6 d-block">
<img class="card-img-top" src="{{ asset($post->image) }}" alt="Card image cap">
<div class="p-6 text-center">
<p>
<a class="small-5 text-lighter text-uppercase ls-2 fw-400" href="#"></a>
{{ $post->category->name }}
</p>
<h5 class="mb-0">
<a class="text-dark" href="{{ route('blog.show',$post->id) }}">
{{ $post->title }}
</a>
</h5>
</div>
</div>
</div>
#empty
<p class="text-center">
No Results found for query <strong>{{ request()->query('search') }} </strong>
</p>
#endforelse
</div>
{{-- <nav class="flexbox mt-30">
<a class="btn btn-white disabled"><i class="ti-arrow-left fs-9 mr-4"></i> Newer</a>
<a class="btn btn-white" href="#">Older <i class="ti-arrow-right fs-9 ml-4"></i></a>
</nav> --}}
{{ $posts->appends(['search' => request()->query('search') ])->links() }}
</div>
#include('partials.sidebar')
</div>
</div>
</div>
</main>
#endsection
If you guys didn't see which file that i don't show up, just ask,.
If you guys see any problems that confusing, i can use teamviewer.
Any Help will be appreciated.
Thank you.
The error told that you don't have posts. Indeed it is. What you have is post() method on Category class, as evidently seen in this line:
public function post() {
You should change this ->with('posts', $category->posts()->simplePaginate()) to be:
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->post()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
In your model, you defined a post relationship whereas you use a posts relationship then, they're not the same one. You should replace post by posts because it's a to-many relationship.
Your relationship is post and you have multiple ->with('posts', $category->posts()->simplePaginate()). Can you change those to ->with('post') and try again?
Related
I had a problem while trying out the recently released laravel 8, I'm trying to find out what the changes are and how it works. When I did that I had a problem with the paginate laravel 8 UI getting messy and somehow it happened. Is there anyone who can help me? or have experienced the same thing?
Like this the view I got in laravel 8
Laravel 8 paginate UI
and this is the code I use "Index.blade.php"
#extends('layouts.app')
#section('title', 'Post')
#section('contents')
<div class="container">
<div class="row">
#foreach ($posts as $post)
<div class="col-md-4 mb-4">
<div class="row">
<div class="card mb-4">
<div class="card-header">
{{ $post->title }}
</div>
<div class="card-body">
{{ $post->body }}
</div>
<div class="card-footer">
{{ $post->created_at->diffForHumans() }}
</div>
</div>
</div>
</div>
#endforeach
</div>
<div class="d-felx justify-content-center">
{{ $posts->links() }}
</div>
</div>
#endsection
the code I use for PostController
<?php
namespace App\Http\Controllers;
use App\Models\Posts;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
$posts = Posts::latest()->paginate(6);
// dd($post);
return view('post.index', compact('posts'));
}
}
just make sure you have this in your AppServiceProvider.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
and you're good to go.
I tried doing adding the Paginator::useBootstrap(); in the AppServiceProvider.php but it didn't work.
But this worked:
// Directly in your blade file
$posts->links('pagination::bootstrap-4')
First go to the file app\Providers\AppServiceProvider.php and add:
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
Use this after #endforeach in your post.blade.php:
{{ $blogs->links() }}
Don't use this way:
$posts->links('pagination::bootstrap-4')in AppServiceProvider.php
Right way :
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
Then in any blade, you want to use pagination like this:
{{ $posts->links() }}
I want to pass the username to the url in the browser as in "/profile/{username that's logged in here}". But to test things out i tried to pass id first.
What's wrong with this is not when i press the actual link. The problem occurs when i tried to access just the welcome page.
I've called the route in the view and passed the correct param 'user' to it but it's still giving me an undefined variable error.
here's the route
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/profile/{user}', 'ProfilesController#index')->name('profile.show');
The ProfilesController:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function index($user)
{
$user = User::find($user);
return view('home', [
'user' => $user,
]);
}
}
The welcome blade:
<div class="flex-center position-ref full-height">
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
<div class="content">
<div class="title m-b-md">
gramClone
</div>
<p class="content">
where IG pics get to be shittier.
</p>
</div>
</div>
the home blade it calls after pressing home link:
#section('content')
<div class="container">
<div class="row">
<div class="col-4 p-5">
<img src="/png/logo.png" alt="logo" style="height: 6rem; width: 6rem;" class="float-right rounded-circle">
</div>
<div class="col-8 pt-5">
<div><h1>{{$user->username}}</h1></div>
<div class="d-flex">
<div class="pr-5"><strong>number here</strong> posts</div>
<div class="pr-5"><strong>number here</strong> followers</div>
<div class="pr-5"><strong>number here</strong> following</div>
</div>
<div class="pt-4 font-weight-bold">Bio Summary Here</div>
<div>Bio Here</div>
<div>Link Here</div>
</div>
</div>
<div class="row pt-5">
<div class="col-4"><img src="https://images.unsplash.com/photo-1566624790190-511a09f6ddbd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
<div class="col-4"><img src="https://images.unsplash.com/photo-1544127715-bafd09df7c52?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
<div class="col-4"><img src="https://images.unsplash.com/photo-1566592952746-15ea1f1a4133?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=653&q=80" alt="" class="w-100"></div>
</div>
</div>
#endsection
Some of my friends said it's cause i have to put the param at get'/' as well but when i tried it, it gave me the previous error of unpassed param again.
Please help me with this, i've been on overflow for 3 hours already and still got no actual answer to this.
Found out the problem after commenting out my route calling in the welcome blade, apparently it's the #auth making things freaky. After some quick googling i found out that apparently you can't just say route('profile.show', $user) if you have an #auth there.
You need to call it like this: auth()->user().
Ok, so I saw similar questions asked but none of the answers helped, so forgive me if it seems like this is a duplicate.
I am following a course on Udemy(It's been over a week, the instructor hasn't answered my question, although they are active on the site, hence why I'm here), the codes I'm displaying below is how the instructor has taught us to write it.
THE ISSUE:
We are creating a blog and that blog has many categories and many posts but each post belongs to only ONE category which can hold multiple posts.
Here's a look at the model for them.
public function user(){
return $this->belongsTo('App\User');
}
public function posts(){
return $this->hasMany('App\Post');
}
POST MODEL
public function user(){
return $this->belongsTo('App\User');
}
public function category(){
return $this->belongsTo('App\Category');
}
They each have their own controller also, we also have a frontend controller that displays all the categories and when you click read more it takes you to a single page that displays the most recent post within that category.
This single page also has a pagination that supposed to display the previous post that belongs to that category.
INSTEAD: it takes the user to the most recent post regardless of what category it belongs to and display all the previous post for all category.
Below is the frontend controller
public function index()
{
return view('categories')
->with('categories',Category:orderBy('created_at', 'desc')->take(24)->get())
->with('tag', Tag::all())
->with('posttitle', Post::orderBy('created_at', 'desc')->first());
}
public function tag($id)
{
$tag = Tag::find($id);
return view('tag')->with('tag', $tag)
->with('categories', $tag->tag)
->with('categories', Category::all());
}
public function singlePage($slug)
{
$post = Post::where('slug', $slug)->first();
$next_id = Page::where('id', '>', $page->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('single')->with('post', $post)
->with('categories', Category::all())
->with('posttitle', Post::take(1)->get())
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id));
}
Here's a look at the view for the index function
<div class="container">
<div class="container-fluid">
<div class="row medium-padding120 bg-border-color">
<div class="container">
<div class="col-lg-12">
<div class="offers">
<div class="row">
<div class="col-lg-8 col-md-12 col-sm-12 col-xs-12">
<div class="heading">
<h4 class="h2 heading-title">Discover New Articles</h4>
<div class="heading-line">
<span class="short-line"></span>
<span class="long-line"></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="case-item-wrap">
#foreach($categories as $category)
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="case-item">
<div class="case-item__thumb">
<img src="{{ $category->featured_img }}" alt="{{ $category->title }}"style="width:300px; height:280px;">
</div>
<h6 class="case-item__title text-center">{!! $category->title !!}</h6>
<!--<i class="seoicon-clock"></i>
<time class="published" datetime="2016-04-17 12:00:00">
{{ $category->created_at->diffForHumans() }}
</time>-->
<h6 style="width:300px; height:85px;">{!! str_limit($category->summary, 85) !!}
<small> Read More</small>
</h6>
</div>
</div>
#endforeach
</div>
</div>
<div class="padded-50"></div>
</div>
</div>
</div>
</div>
and this is the single page view below
<div class="content-wrapper"> <div class="container"> <div>
<main class="main">
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-1 col-md-7 col-md-offset-1 col-lg-6 col-lg-offset-3 panel panel-reading">
<article class="hentry post post-standard-details">
<h2>{!! $post->posttitle !!}</h2>
<div class="post__content">
<div class="post-additional-info">
<div class="post__author author vcard">
By:
<div class="post__author-name fn">
{{ $post->user->username}}
</div>
</div>
{{--Post date aka page date --}}
<span class="post__date">
<time class="published" datetime="2016-03-20 12:00:00">
<i class="seoicon-clock"></i> {{ $post->created_at->diffForHumans() }}
</time>
</span>
{{--End Post date aka page date--}}
</div>
{{--Post content aka page content--}}
<div class="post__content-info">
<p> {!! $post->content !!} </p>
</div>
{{--End post content aka page content--}}
</div>
<div class="pagination-arrow">
{{--Left arrow--}}
#if($prev)
<a href="{{ route('post.single', ['slug' => $prev->slug] )}}" class="btn-prev-wrap">
<svg class="btn-prev">
<use xlink:href="#arrow-left"></use>
</svg>
<div class="btn-content">
<div class="btn-content-title">Previous Page</div>
<p class="btn-content-subtitle">{{ $prev->posttitle}}</p>
</div>
</a>
#endif
{{--Right arrow--}}
#if($next)
<a href="{{ route('post.single', ['slug'=>$next->slug]) }}" class="btn-next-wrap">
<div class="btn-content">
<div class="btn-content-title">Next Page</div>
<p class="btn-content-subtitle">{{ $next->posttitle }}</p>
</div>
<svg class="btn-next">
<use xlink:href="#arrow-right"></use>
</svg>
</a>
#endif
</div>
</article>
</div>
</main>
HERE'S the post database
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('posttitle');
$table->string('slug');
$table->text('content');
$table->softDeletes();
$table->timestamps();
});
}
IN CONCLUSION:
Using this how would you filter it so that only the post that has the same category_id is displayed when the user clicks on that category?
Thank you
As a bonus I did a short screen recording of the problem https://www.screencast.com/t/vVzRMSunH
Sorry, I cant repeat all your codes but if I well understand you, you want to be able to show on a single page, all posts belonging to a specifique category. If it is, the controller who generate view should be like this. Question of organisation, I prefer to have a specific Controller for all action about category model.
/**
* Show all posts of category $id
*
* #param int $id : Id of category mapped via route
*/
public function show($id)
{
$category = Category::findOrFail($id);
$posts = $category->posts()->paginate();
return view('category.show', compact('posts'));
}
And your resources/views/category/show.blade.php can be like this
#extends('layouts.app')
#section('content')
#foreach($posts as $post)
<h2>{{ $post->title }}</h2>
{{-- and you can show more informations about post here --}}
#endforeach
{{-- pagination links --}}
{!! $posts->links() !!}
#stop
I'm laravel begginer and I wan't to make simple CMS. Ok, so i have simple question:
I want when click on avatar (profile image) - redirect to user profile (http://example.com/profiles/nickname/id)
In DB it saves that:
as you see author_id I have, now I need to get author name from users table:
And then generate url: http://example.com/profiles/Evaldas/2 (Evaldas, because author_id is 2 in topics table)
My routes file:
Route::get('topic/{tname}/{tid}', 'viewTopic#showTopic');
My viewTopic.php Controller:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewTopic extends Controller
{
public function showTopic($tname, $tid)
{
return View::make('posts', [
'topics' => DB::table('topics')
->where('id', $tid)
->where('seo_title', $tname)
->first(),
'posts' => DB::table('posts')
->where('topic_id', $tid)
->select()
->get()
]);
}
}
And layout:
#extends('layouts.main')
#section('content')
<div class="media">
<div class="media-left">
<a href="HERE MUST BE HREF TO PROFILE">
<img class="media-object" src="http://localhost/uploads/avatars/2.jpg" style="width: 64px">
</a>
</div>
<div class="media-body" rel="#author{{ $topics->author_id }}">
<h4 class="media-heading">{{ $topics->title }}</h4>
#if(!empty($topics->text))
{{ $topics->text }}
#else
Message empty :(
#endif
</div>
#foreach($posts as $post)
<div class="media">
<div class="media-left">
<a href="HERE MUST BE HREF TO PROFILE">
<img class="media-object" src="http://localhost/uploads/avatars/1.png" style="width: 64px">
</a>
</div>
<div class="media-body" rel="#post{{ $post->pid }}">
{{ $post->text }}
</div>
</div>
#endforeach
</div>
#stop
Thanks in advance ;)
You could use the User object to look it up ... but a better option would be to join the 'posts' table to the 'user' table, and select it from the results - have a look at http://laravel.com/docs/5.1/queries#joins
This is the code from CategoriesController:
public function index()
{
$categories = Category::all();
return view('/home', compact('categories'));
}
And this is the code that I'm writing to the home.blade.php
#foreach($categories as $cat)
#endforeach
And then in home.blade.php
I'm getting this error: Undefined variable: categories
Why this happening ? Everything works fine with Articles but not categories and the code is the same.
Home.blade.php
#extends('app')
#section('content')
<div class="col-md-4 pull-right">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Categories</h3>
</div>
<div class="panel-body">
#foreach($categories as $cat)
<div class="col-lg-6">
<ul class="list-unstyled">
<li>News
</li>
</ul>
</div>
#endforeach
</div>
</div>
</div>
#if(count($articles))
#foreach($articles as $article)
<div class="panel panel-default col-md-8">
<div class="panel-heading">
<h3>{{ $article->title }}</h3>
<small>posted by {{ $article->author->name }} {{ $article->created_at }}</small>
</div>
<div class="panel-body">
{!! str_limit($article->body, 1000) !!}
<hr>
Read More
</div>
</div>
#endforeach
#else
<h1>No articles at the moment.</h1>
#endif
<br>
{!! $articles->render() !!}
#stop
You have two routes
Route::get('/home', 'ArticlesController#index');
Route::get('/home', 'CategoriesController#index');
Which means when you visit /home, only ArticlesController#index is fired, not both.
This means that the $categories variable used in the view is not populated because your index() method in CategoriesController is intended to do this but is never called.
Therefore you need to do something like this
Route::get('/home', 'HomeController#index');
public function index()
{
$categories = Category::all();
$articles = Article::all();
return view('/home', compact('articles', 'categories'));
}
try to use
return View::make('/home')->with(compact('categories'));
instead of
return view('/home', compact('categories'));
I insert this in the top of my #foreach
<?php
use App\models\PersonalContact;
$data = PersonalContact::all();
?>
or this before #foreach add #if statement like this
#if (count($data) > 0)
#foreach ($data as $bloginfo)
<tr role="row" class="odd">
<td class="sorting_1">{{$bloginfo->name}}</td>
<td>{{$bloginfo->description}}</td>
<td>{{$bloginfo->email}}</td>
<td>{{$bloginfo->phone}}</td>
<td><img src="{{url('images/'.$bloginfo->image)}}" alt="" width="100px" height="100px"></td>
<td><i class="fas fa-edit"></i>Add | <i class="fas fa-edit"></i>Edit | <a class="btn btn-app"><i class="fas fa-edit"></i>Delete</a></td>
</tr>
#endforeach
#endif
return view('/home', compact('categories'));
The problem was $categories on compact method, you must use like a string