#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
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?
By messy I think what you imply here is that the elements are out place.
Laravel 8 uses tailwind css.
If it's the problem and you have queried it correctly with paginate()
then what you can do is, use bootstrap.
Simply add the following code in AppServiceProvider file and it will use bootstrap for paginate styling.
class AppServiceProvider extends ServiceProvider {
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
Paginator::useBootstrap(); //this line will be in boot method*
}
}
Related
I am trying to print out the lists of payment methods using the foreach loop, however, i get an undefined variable.. error.
Here is my controller class
class HomeController extends Controller
{
//
public function index()
{
$payments = Payment::where('status', 1)->get();
return view('user.account.index', [
'payments' => $payments
]);
}
}
and here is my section in my view page(index) i want to loop through
#foreach ($payments as $payment)
<!-- {{ Cryptocap::getSingleAsset($payment->name) }} -->
<div class="card col-md-6 no-padding ">
<div class="card-body">
<div class="h1 text-muted text-right mb-4">
<i class="pe-7s-wallet"></i>
</div>
<div class="h4 mb-0">
<span class="">0.0012930403</span>
</div>
<small class="text-muted text-uppercase font-weight-bold">BTC</small>
<div class="progress progress-xs mt-3 mb-0 bg-flat-color-1" style="width: 40%; height: 5px;"></div>
</div>
</div>
#endforeach
The above approach works on other areas of my application, as this is the approach i have been using throughout the application. But it doesn't seem to work on this particular controller and view.
Can you give this a try
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
For more info visit this link
Passing data from controller to view in Laravel
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 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?
I want to show the results of a database query inside the navigation tab of the home page. How do I call the $results variable inside the posts tab of the home page (the last code block). Thanks
PostController.php
class NoteController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = DB::select(....);
return view('posts.index',['results' => $posts]);
}
The post index blade
#extends('layouts.app')
#section('content')
<div class="container">
<div class="col-md-8">
#foreach($results as $notes)
<div class="panel panel-default">
<div class="panel-body">
{{$notes->note}}
</div>
</div>
#endforeach
</div>
</div>
#endsection
The home index file
#extends('layouts.app')
#section('content')
<div class="container">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#posts">Posts</a></li>
<div class="tab-content">
<div id="posts" class="tab-pane fade in active">
</div>
</div>
</div>
#endsection
to use save function in multiple views you can use AppServiceProvider.php file in App->Providers. there you can make composer view to pass variables in all your views look at sample below:
View::composer('*', function ($view) {
$ads = Ad::all();
foreach($ads as $ad){
$customJs = $ad->js;
$customCss = $ad->css;
}
$view->with('customJs', $customJs);
$view->with('customCss', $customCss);
});
by this sample now I have ability of using $customJs and $customCss in any view i want.
Hope it helps.
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