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);
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'm trying to pass a variable from a controller route to a view so i can display data relating to new assoicate model. It's working for other contrllers, but i can't figure out why it's not here.
Controller
#Index - Not passing down
public function index(){
$Advert = new PropertyAdvert();
return view('pages/Advert/index', compact('Advert'));
}
#Show - Is working, this is just an example.
public function show($id){
$user = Auth::user();
$Advert = PropertyAdvert::where('id', $id)->first();
return view('pages/Advert/show', compact('Advert', 'user'));
}
This is the route
Route::get('/property', 'AdvertisementController#index');
Route::get('/advertise', 'AdvertisementController#create')->middleware('auth');
Route::post('/createadvert', 'AdvertisementController#store');
Route::get('/property/{id}', 'AdvertisementController#show');
This is the template i'm trying to pass down to show.blade.php
<div class="row text-center" style="display:flex; flex-wrap:wrap">
<div class="col-lg-12">
<h3>Our most recent properties</h3>
#foreach ($Advert as $property)
<div class="col-md-3 col-sm-6">
<div class="thumbnail">
<img src="data:image/gif;base64,{{ $property->photo }}">
<div class="caption">
<h4>{{$property->address .', '. $property->town .', '. $property->county}}</h4>
</div>
<p>
More Info!
</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
Try this:
public function index(){
$Advert = PropertyAdvert::get();
return view('pages/Advert/index', compact('Advert'));
}
You need to actually access the data for that model.
I'm trying to make a posting system, with a resource routing combination on the posts. When I try to run the app to view the posts, it returns an error stating that the posts could not be found within the view. I have the controller code for the index and the show functions:
public function index()
{
$posts = Post::latest()->get();
return view('view', compact('posts'));
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
The view that I have for the app uses the post variable to display the posts:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-2">
<div class="panel panel-default">
<!-- Posts will be displayed on the same panel -->
<div class="panel-body" id="view">
#foreach($posts as $post)
<article id="post">
<a href="/view/posts{{ $post->id }}">
{{ $post->title }}
</a>
<div class="body">
{{ $post->body }}
</div>
<!-- Footer for posts will include interaction features -->
</article>
#endforeach
</div>
</div>
</div>
</div>
</div>
Is there something that the laravel installation isn't doing correctly? Is the compact function set up correctly?
Your controller index function should be something like this:
public function index()
{
$posts = Post::get();
return view('posts.index', compact('posts'));
}
The return of the index action is wrong
return view('view', compact('posts'));
Change 'view' with 'posts'
Use compact('posts')
If you're a beginner checkout the laracasts video series to get a good understanding of the Laravel framework.
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>
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