I'm trying to display the latest post, posted on my website.
I have a vague idea of how it needs to be done but I'm not 100% sure.
I think I need to do something like this in my PostController.php
public function index()
{
$laatstepost = Post::orderBy('created_at', 'desc')->take(1)->get();
return View('showposts')->with('laatsteposts', $laatstepost);
}
And then something like this in my view.
<div class="panel-body">
<h3>{{ $laatsteposts->title }}</h3>
<p>{{ $laatsteposts->text}}</p>
Lees meer...
</div>
I don't know if I'm heading the right way or that I'm completely off track. If u guys can help me out that would be great! Thanks
Here is a screenshot of where it needs to be displayed: http://imgur.com/a/c1feW
If I miss code that is needed for this, Tell me
Use the first() method to get an object instead of collection:
public function index()
{
$laatstepost = Post::latest()->first();
return view('showposts')->with('laatsteposts', $laatstepost);
}
And in the view:
<div class="panel-body">
<h3>{{ $laatsteposts->title }}</h3>
<p>{{ $laatsteposts->text }}</p>
Lees meer...
</div>
Alternatively, you can use your code to get a collection with just one element and display data in the view using first() collection method:
<div class="panel-body">
<h3>{{ $laatsteposts->first()->title }}</h3>
<p>{{ $laatsteposts->first()->text }}</p>
Lees meer...
</div>
Related
I am getting above error while requesting to next page but I want posts title is as page title as well
#extends('layouts.app')
#section('pageTitle', $post->title)
#section('content')
Go Back
<br> <br>
<h1>{{ $post->title }}</h1>
<div class="">
<p>{!! $post->body !!}</p>
</div>
<hr>
<small>Written on: {{ $post->created_at }}</small>
<hr>
Edit
#endsection
public function show($id)
{
$post = Post::find($id);
return view('posts.show')->with('post', $post);
}
Post::find(...) can return null. You will have to make sure that you are actually receiving a model instance, the record exists, before trying to use it. You can use something like Post::findOrFail(...) to allow this to fail when it doesn't find a record. This would allow you to continue in the code knowing $post will be a valid instance.
Because the return type should be an array,
you can use this
public function show($id)
{
$post[] = Post::find($id);
return view('posts.show')->with('post', $post);
}
I have a strange problem, I am working on a personal project in Laravel, a "Q&A" type site, so far everything is fine. When I want to post a question from the database, it will be displayed twice on the display page, even if I only put the question with a certain id.
the error that appears
Route:
Route::get('/viewUserQuestion/{post}', 'PostsController#viewUserQuestion')->name('viewQuestion');
Controllerr:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::latest()->get();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
Blade:
<div class="card-body p-0">
#foreach($posts as $post)
<div class="mailbox-read-info">
<h5 align="center"> {{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div><div class="mailbox-read-message">
<p>{{ $post->content }}</p>
Route to the display page:
<td>{{ $post->id }}</td>
What do you think would be the exact problem? I would be grateful if you could help me, does it run twice or does it?
Basically I want to display the data from each id, but in different pages without overlapping. As it is here, you click on a question and display it not twice.
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 can`t access the values from this variable $post from controller:
in blade view:
#foreach($post as $p)
<div class="col-md-3">
<div class="boxpromo">
<h4>{{ $p->title }}</h4>
<p>{{ $p->post_description }}</p>
<div class="price">
<div class="data">{{ $p->out_date }}</div>
<div class="cost">{{ $p->price }}</div>
</div>
</div>
</div>
#endforeach
In controller i have:
public function postSearchTransport(Request $request, Post $post){
...
// create a new query builder
$post = $post->newQuery();
// add some filters from the user in the search query
if($request->has('searchWord')){
$post->where('title', 'LIKE', '%'.$request->input('searchWord').'%');
}
...//more filters
$post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $post);
}
What i did wrong? I should be able to access in the view all the values from the query result, but i can`t this way.
Should i convert that $post std object from controller into an array then return it to the view???
You must pass to the view the returned value of paginate method like this :
$posts = $post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $posts);
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');