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'));
}
Related
It works really good, but when I've created new page something goes wrong and now my blog view doesn't show data, but blogs view still correctly show data . I am trying to show detailed data of each blog when user click on button "Details"
MainController:
public function blog(Blogs $blog)
{
return view('blog', compact('blog'));
}
public function blogs()
{
return view('blogs',['blogs' => Blogs::all(),]);
}
blogs.blade.php:
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<div class="row">
#foreach($blogs as $blog)
#include('layouts.cardBlog', compact('blog'))
#endforeach
</div>
#endsection
and cardBlog.blade.php:
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{($blog->image) }}">
<div class="caption">
<h3>{{ $blog->title }}</h3>
<p>{{ $blog->body }}</p>
<p>
<a href="{{route('blog', $blog->id) }}"
class="btn btn-default"
role="button">#lang('main.more')</a>
#csrf
</p>
</div>
</div>
</div>
blog.blade.php
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<h1>{{ $blog->title}}</h1>
<img src="{{$blog->image }}">
<p>{{ $blog->body }}</p>
#endsection
web.php:
Route::get('/', 'MainController#index')->name('index');
Route::get('/categories', 'MainController#categories')->name('categories');
Route::get('/about', 'MainController#aboutus')->name('about');
Route::get('/contact-us', 'ContactUSController#contactUS')->name('contact-us');
Route::post('contactus', ['as'=>'contactus.store','uses'=>'ContactUSController#contactSaveData']);
Route::get('/contacts', 'MainController#contacts')->name('contacts');
Route::get('/blogs', 'MainController#blogs')->name('blogs');
Route::get('/blog/{id}', 'MainController#blog')->name('blog');
Route::get('/intership', 'MainController#intership')->name('intership');
Route::get('/{category}', 'MainController#category')->name('category');
Route::get('/{category}/{product}/{skus}', 'MainController#sku')->name('sku');
Route::post('subscription/{skus}', 'MainController#subscribe')->name('subscription');
What's is the error that it shown?
I suggest use "compact" in this line code
return view('blogs',['blogs' => Blogs::all(),]);
Something like this
public function blogs()
{
$blogs = Blogs::all();
return view('blogs',compact('blogs'));
}
Try this
public function blog($id)
{
$blog = Blogs::findOrFail($id)
return view('blog', compact('blog'));
}
views/show.blade.php
#extends('layouts.app')
#section('content')
<h5>Showing Task {{ $task->title }}</h5>
<div class="jumbotron text-center">
<p>
<strong>Task Title:</strong> {{ $task->title }}<br>
<strong>Description:</strong> {{ $task->description }}
</p>
</div>
#endsection
Controllers/HomeController.php
public function show(Task $task)
{
return view('show', compact('task', $task));
}
routes/web.php
Route::get('show/{id}', 'HomeController#show')->name('show');
views/viewalltask.blade.php
<td>{{$data->title}}</td>
No error / No Record / instead of particulr record display blank page
You need to change the route configuration to:
Route::get('show/{task}', 'HomeController#show')->name('show');
This way Laravel's IOC container knows how to resolve/bind it.
Must match the variable name used in the method definition:
public function show(Task $task)
I'm new to Laravel and I'm trying to get one of the values in one collection by one ID retrieved from another collection.
My function returns 2 Collections:
public function index()
{
$category = BlogCategory::all(['id', 'name']);
$post = BlogPost::orderBy('id', 'desc')->take(14)->get();
return view('blog.index', ['posts' => $post], ['categories' => $category]);
}
In a foreach loop I'm getting values from collections:
#foreach($posts as $post)
#if($loop->iteration > 2)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $categories->get($post->category_id) }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endif
#endforeach
I'm partially getting the result as you can see in the image below, but I want to get only the name.
Here is the code that I'm trying to figure out
{{ $categories->get($post->category_id) }}
If there is a better or correct way to do it, let me know.
Blog Posts migration:
public function up()
{
Schema::create('blog_posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->longText('content');
$table->mediumText('slug');
$table->bigInteger('author_id')->unsigned();
$table->bigInteger('category_id')->unsigned();
$table->timestamps();
$table->foreign('author_id')->references('id')->on('blog_authors');
$table->foreign('category_id')->references('id')->on('blog_categories');
});
}
You should put in place the relationships between BlogPost and BlogCategory models, seen that you already have a category_id field in BlogPost model, i.e.:
in BlogPost Model:
public function category(){
return $this->belongsTo(\App\BlogCategory::class);
}
in BlogCategory Model:
public function posts(){
return $this->hasMany(\App\BlogPost::class);
}
Next you can eager load categories with the $posts in your controller with only two queries:
public function index()
{
$posts = BlogPost::with('category')->orderBy('id', 'desc')->take(14)->get();
return view('blog.index', compact('posts'));
}
Then in your view you can access each $post->category object directly because eager loaded in the controller:
#foreach($posts as $post)
#if($loop->iteration > 2)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $post->category->name }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endif
#endforeach
It could be optimized, first, you need to make relation one to many from categories to posts
first: make sure that you have in posts migrations category_id column
Second: Open Category Model and write this method this will allow you to fetch posts that belong to the category
public function posts(){
return $this->hasMany(\App\Post::class);
}
Third: open shop model and write this method this will allow you to fetch category that belongs to the post
public function catgeory(){
return $this->belongsTo(\App\Category::class);
}
Finally: you will edit your view like this one
#foreach($posts as $post)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $post->category->title }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endforeach
and of course, you wouldn't call categories in your controller anymore
public function index()
{
$post = BlogPost::orderBy('id', 'desc')->take(14)
->with('category')->get();
return view('blog.index', ['posts' => $post]);
}
Maybe it better use a relation to drive this situation. You can load in the controller the data in this way.
In the Model Post:
function categories(){
return $this->belongsToMany('App\BlogCategory')->select(array('id', 'name');
}
Maybe is hasOne -> the relationship if you don't have a table pivot...
In the Controller:
public function index()
{
$data['posts'] = BlogPost::orderBy('id', 'desc')->take(14)->with('categories')->get();
return view('blog.index', $data);
}
In the View:
#foreach($posts as $post)
#if($loop->iteration > 2)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $post->categories->name }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endif
#endforeach
this line $categories->get($post->category_id) return for you an array of category, so the solution for you here is just like bellow:
{{ $categories->get($post->category_id)['name'] }}
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>
I'm trying to list articles from database then show details of the article when the user clicks on it.
How can I pass the article id or name to the route/controller to display the details? I tried butting the id in the href but I don't know how to handle it from routes!
this is the articles list:
#foreach ($articles as $article)
<div class="panel-body">
{{$article->name}}
</div>
#endforeach
where do I go from here?
Your View file for listing all the articles:
#foreach ($articles as $article)
<div class="panel-body">
{{$article->name}}
</div>
#endforeach
routes.php
Route::get('/article/{id}', 'ArticlesController#show');
ArticlesController.php
/**
* Show the article of the given id.
*
* #param int $id The id of the article
* #return \Illuminate\View\View
*/
public function show($id)
{
$article = Article::findOrFail($id);
return view('articles.show', compact('article'));
}
show.blade.php
<h3>{{ $article->name }}</h3>
<p>{{ $article->body }}</p>
If you want to use route() you can pass parameters in second argument
#foreach ($articles as $article)
<div class="panel-body">
{{ $article->name }}
^^^ or whatever is the name of your route
</div>
#endforeach
Naturally this assumes you have your route defined in app\Http\routes.php. For example
Route::get('/articles/{id}', ['as' => 'article.show', 'uses' => 'ArticleController#show']);
Alternatively you can use url()
#foreach ($articles as $article)
<div class="panel-body">
{{ $article->name }}
</div>
#endforeach