How to make url with slug in Laravel 5.4? - php

Right now I am trying to do a Laravel url with slug. I have my main page where I show all categories:
<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>
And now I want to go to a certain category page. I created routes:
Route::get('/test', 'FuelAccountingController#index')->name('fuel_accounting');
Route::get('/test/{slug}', 'FuelAccountingController#element');
And functions in controller:
public function index()
{
$categories = DB::table('fuel_accounting_categories')
->select('id', 'name', 'slug')
->get();
return view('pages.fuel_accounting')->with('categories', $categories);
}
public function element($slug)
{
$category = DB::table('fuel_accounting_categories')
->select('slug')
->where('slug', '=', $slug)
->first();
return view('pages.fuel_accounting_element')->with('category', $category);
}
And when I am trying to reach a page (for example: laravel.dev/test/current_category) it does not work. Can someone explain me how to do that?
Error: Sorry, the page you are looking for could not be found. (1/1) NotFoundHttpException
FIXED

<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>

Related

Define article id

I have a page of a separate article to which we go by slug
php.blade
#extends('layouts.app')
#section('content')
<div class="article-wrapper">
<div class="container">
<h1>{{ $article->title }}</h1>
<h3>{{ $article->subtitle }}</h3>
<img src="{{ $article->main_image }}" alt="">
<h3>{{ date('d F Y', strtotime($article->published_at)) }}</h3>
<h3>{{ $article->views }} Views</h3>
#foreach($article_blocks as $article_block)
<div class="text-image">
<h2>{{ $article_block->title_1 }}</h2>
<h3>{{ $article_block->text_1 }}</h3>
<img src="{{ $article_block->main_image }}" alt="">
</div>
#endforeach
#endsection
controller
public function index(Request $request, $slug)
{
$article = Article::where('slug', $slug)->first();
$article_blocks = ArticleBlock::where('article_id', 1)->get();
return view('article', compact('article', 'article_blocks'));
}
Next, I have another model for blocks, each article has its own blocks, as you can see in my controller, I manually write the article ID to get the blocks specific for it.
How to make it itself determine which article we are on, and display blocks only for it?
Try :
public function index(Request $request, $slug)
{
$article = Article::firstWhere('slug', $slug);
$article_blocks = ArticleBlock::where('article_id', $article->id)->get();
return view('article', compact('article', 'article_blocks'));
}
If you have created relationships between your models, you can use it as follows:
public function index(Request $request, $slug)
{
$article = Article::with('articleBlock')->where('slug', $slug)->get();
return view('article', compact('article'));
}

Laravel display product by category

Web Route
Route::get('/categories/{id}', [ProductController::class, 'categories'])->name('categories');
ProductController
public function categories($id)
{
$categories = Category::with('product')->find($id);
return view('products.categories')->with('products', $categories);
}
When returning the $categories, it displays the category with selected product (Gaming Chair: Ranger Gaming Chair)
{"id":4,"category":"Gaming Chair","created_at":"2021-06-27T16:04:27.000000Z","updated_at":"2021-06-27T16:04:27.000000Z","product":[{"id":2,"category_id":4,"product_name":"Ranger Gaming Chair","description":"Lorem Ipsum","product_picture":"1624846316.png","price":4325.23,"created_at":"2021-06-28T02:11:56.000000Z","updated_at":"2021-06-28T02:11:56.000000Z"}]}
But every time I return to view('products.categories') and doing foreach loop
#foreach($categories as $item)
<h5>{{ $item->category }}</h5>
<h5>{{ $item->product_name }}</h5>
#endforeach
It displays the whole category, not the "Gaming Chair" :/
Please help me display it.
try this way
in your controller
public function categories($id)
{
//make this variable singular not plural..its easier to read/understand
$category = Category::with('product')->find($id);
return view('products.categories')->with('products', $categories);
}
and in your blade
#foreach($category->products as $item)
<h5>{{ $item->category_id }}</h5>
<h5>{{ $item->product_name }}</h5>
#endforeach

Laravel How to add orderBy on categorized posts

I have a laravel-application where I have a Blog section. The posts are split up into categories so that the user can switch between each categories. So far so good but since I added the categorization and I click the "All"-tab, the orderBy()-method does not work anymore. Instead, the posts appear under their respective category, ignoring the orderBy-method.
Here is my controller:
$blogs = QueryBuilder::for(Blog::class)
->allowedFilters([
AllowedFilter::exact('category', 'blog_category_id'),
])
->orderBy('publishes_on', 'desc')
->where("publishes_on", "<=", Carbon::now())
->where('blog_status_id', '=', '2')
->with('blog_category', 'employee')
->get();
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blogs', 'blog_categories'));
then in my view:
// the tab menu - click events handled with Javascript
<ul>
<li data-tab-id="all" class="tab all active">All</li>
#foreach ($blog_categories as $category)
<li data-tab-id="{{ strtolower($category->name) }}" class="tab {{ strtolower($category->name) }}">{{ $category->name }}</li>
#endforeach
</ul>
// the posts loop
#foreach($blog_categories as $category)
#foreach($category->blog_post as $blog)
<div class="col-md-4 post {{ strtolower($category->name) }}">
<p>{!! $blog->text !!}</p>
</div>
#endforeach
#endforeach
so, how can I achieve that the orderBy works again as intented?
thanks in advance!

Display posts belonging to category in laravel

I am trying to get the posts that belong to each category, i had this working before but i can't seem to find what i have done wrong here.
I have a Post Table and a Categories Table
ROUTE
Route::get('articles/category/{id}', ['as' => 'post.category', 'uses' => 'ArticlesController#getPostCategory']);
CONTROLLER
public function getPostCategory($id)
{
$postCategories = PostCategory::with('posts')
->where('post_category_id', '=', $id)
->first();
$categories = PostCategory::all();
// return view
return view('categories.categoriesposts')->with('postCategories', $postCategories)->with('categories', $categories);
}
VIEW
#foreach($postCategories->posts as $post)
<div class="well">
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placekitten.com/150/150">
</a>
<div class="media-body">
<h4 class="media-heading">{{ substr($post->title, 0, 50) }}</h4>
<p class="text-right">By Francisco</p>
<p>{{ substr($post->body, 0, 90) }}</p>
<ul class="list-inline list-unstyled">
</ul>
</div>
</div>
</div>
#endforeach
POST MODAL
public function postCategory()
{
return $this->belongsTo('App\PostCategory');
}
POSTCATEGORY MODAL
class PostCategory extends Model
{
// connect Categories to Posts tables
protected $table = 'post_categories';
// Category belongs to more than 1 post
public function posts()
{
return $this->hasMany('App\Post', 'post_category_id');
}
}
I can't see what i am doing wrong every time I go to a category it shows
Trying to get property of non-object
Any help will be much appreciated thanks
replace your lines:
$postCategories = PostCategory::with('posts')
->where('post_category_id', '=', $id)
->first();
with:
$postCategories = PostCategory::find($id)->posts;
Change your line in your PostCategoryModel to:
return $this->hasMany('App\Post', 'post_category_id','post_category_id');

Laravel search for topic by title and generate link

I'm laravel newbie. I have something like this:
Now I wan't to make when click first topic redirect to that topic url. For that I need to use DB:: function. I wan't search by given title, and then get id, etc.. For example i have posts:
table name same as topic title in image above, so I need use DB::select or ::table and search by that name, then print results. How to?
Layout:
#extends('layouts.main')
#section('content')
<div class="panel panel-default">
<div class="panel-heading">Forum</div>
<div class="panel-body">
#forelse($forums as $forum)
#forelse($topics as $topic)
{{ $topic->title }}<br>
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau šis forumas yra tuščias!</div>
#endforelse
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau forumas nerastas</div>
#endforelse
</div>
</div>
{!! $topics->render() !!}
#stop
Thanks in advance ;(
UPDATED
viewForum Controller:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewForum extends Controller
{
public function showForum($fname, $fid)
{
$forums = DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->get();
$topics = DB::table('topics')
->where('forum_id', $fid)
->select()
->paginate(1);
return View::make('forum', compact('forums', 'topics'));
}
}
In your topic href do this:
{{ $topic->title }}<br>
In your routes.php add this
Route::get('topic/{fname}/{fid}', 'viewForum#showForum');

Categories