ErrorException Undefined variable: post - php

ok so I wanted to fetch a post from my database with my controller and then show it in my view in welcome.blade.php but the message keep showing ErrorException:
Undefined variable: post
I guess the problem is in how I write my code to get the getIndex function in my routes, so I've been trying to change that but I keep getting the same result.
so this is the line in welcome.blade.php:
<!-- Blog item -->
<div class="blog-item">
<div class="blog-thumb">
<img src="asset/img/blog/1.jpg" alt="">
</div>
<div class="blog-text text-box text-white">
<div class="top-meta"><small><i>{{ Carbon\Carbon::parse($post->created_at)->format('d-m-Y') }} by {{ $post->name }}</i></small>/ di Rakitan</div>
#foreach($posts as $post)
<h3 class="blog-post-title">{{ $post->title }}</h3>
<p>{!! \Illuminate\Support\Str::words($post->description, 30, '...') !!}</p>
<blockquote>
<p>
Learn more </p>
</blockquote>
</div><!-- /.blog-post -->
#endforeach
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
<!-- Blog item -->
this is the controller PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
public function getIndex() {
$posts = DB::table('users')->leftjoin('posts', 'users.id', '=', 'posts.author')->paginate(2);
return view('welcome', ['posts' => $posts]);
}
}
and this is the routes file web.php
<?php
/*
|--------------------------------------------------------------------------
| 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('/', 'PostController#getIndex',function () {
return view('welcome')->name('index');
});
Route::get('/', 'PostController#getIndex')->name('index');
Route::get('/2019', 'BlogController#blog');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/author/post','HomeController#getPostForm')->name('post.form');
Route::post('/author/post', 'HomeController#createPost')->name('post.form');
Route::get('/author/post/detail/{id}', 'HomeController#getPost')->name('post.detail');
Route::get('/author/post/edit/{id}', 'HomeController#editPost')->name('post.edit');
Route::post('/author/post/edit/{id}', 'HomeController#updatePost')->name('post.update');
Route::get('/author/post/delete/{id}', 'HomeController#deletePost')->name('post.delete');

You are setting the meta in the HTML before you are defining post in your for loop.
#foreach($posts as $post)
<!--moved meta in here-->
<div class="top-meta"><small><i>{{ Carbon\Carbon::parse($post->created_at)->format('d-m-Y') }} by {{ $post->name }}</i></small>/ di Rakitan</div>
...
#endforeach

Related

Class 'App\Http\Controllers\Controller' not found in Laravel 8

Controller
namespace App\Http\Controllers;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function show($id)
{
$article = Article::find($id);
return view('articles.show', ['article' => $article]);
}
}
Routes
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('about', [
'articles' => App\Models\Article::take(3)->latest()->get()
]);
});
Route::get('/articles/{article}',
'App\Http\Controllers\ArticlesController#show');
show.blade.php
#extends ('layout')
#section ('content')
<div id="wrapper">
<div id="page" class="container">
<div id="content">
<div class="title">
<h3>{{ $article->title }}</h3>
<p>
<img src="/images/banner.jpg" alt=""
class="image image-full"/>
</p>
<p>{{ $article->body }}</p>
</div>
</div>
#endsection
Error
Class 'App\Http\Controllers\Controller' not found
http://localhost:8000/articles/2
I am not sure where I am going wrong. I have looked at the documentation for routes/controllers in Laravel 8, and it looks like what I have should be working.
It seems there is something wrong with your extended controller.
please check Controller.php existed in App\Http\Controllers or not.
All controller same as yours (ArticlesController) extended from Controller

How to view two tables data on the same blade template (view)?

I have a DB with multiple tables. I want to display the DB data on my laravel application. I want to display two tables data on the same page. I have created the model, view, and controller for the app but I am being able to display only one table. I cannot show the other table.
I think I need to define a model with multiple relationships which I am not getting how to do.
My tables are called posts and videos I have nothing on my model. the controller and the view is given below.
Controller
namespace App\Http\Controllers;
use App\Post;
use App\Video;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
$posts = Post::all();
return view('landing')->with('posts', $posts);
}
}
View
#extends('layouts.app')
#extends('layouts.navbar')
#section('title')
Landing Page
#endsection
#section('content')
<main class="py-4">
<div class="container">
<div class="row">
<div class="col-md-8">
<h3>Section 1</h3>
#foreach ($posts as $post)
<ul class="list-group">
<li class="list-group-item">
<a href="/posts/{{ $post->id }}">
{{ $post->title }}
{{ $post->brief }}
{{ $post->body }}
{{ $post->cover_image }}
</a>
</li>
</ul>
#endforeach
</div>
<div class="col-md-4">
<h3>Section 2</h3>
#foreach ($videos as $video)
<ul class="list-group">
<li class="list-group-item">
<a href="/videos/{{ $video->id }}">
{{ $video->title }}
</a>
</li>
</ul>
#endforeach
</div>
</div>
</div>
</main>
#endsection
Route on the web.php
<?php
use App\Http\Controllers\PagesController;
use Illuminate\Support\Facades\Route;
Route::get('/', 'PostsController#index');
Route::get('/', 'VideosController#index');
Route::get('/posts/{post}', 'PostsController#show');
Route::resource('posts', 'PostsController');
Route::resource('videos', 'VideosController');
Auth::routes();
Controller for videos data table
<?php
namespace App\Http\Controllers;
use App\Video;
use Illuminate\Http\Request;
class VideosController extends Controller
{
public function index()
{
$videos = Video::all();
return view('landing')->with('videos', $videos);
}
}
On this code I see this problem
If I remove the route for videos on my web.php I could see posts data.
So how could I display both the posts and the videos data at the same time?
Pass both Model together :
<?php
namespace App\Http\Controllers;
use App\Video;
use App\Post;
use Illuminate\Http\Request;
class VideosController extends Controller
{
public function index()
{
$videos = Video::all();
$posts = Post::all();
return view('landing', compact('videos','posts'));
}
}
public function profile($id)
{
$id = Crypt::decrypt($id);
$measurements = DB::select( DB::raw("SELECT * FROM measurements WHERE custom_id = '$id'") );
$customers = DB::table('customers')->where('customer_id', '=', $id)->get();
return view('measurements.profile', compact('measurements','customers'));
}

LARAVEL 7 - How to pass variables to views

I have two tables, Companies and Projects. A company hasMany projects and a project belongsTo a company.
Company.php model
protected $fillable = [
'id', 'name', 'description'
];
public function projects()
{
return $this->hasMany('App/Project');
}
Project.php model
protected $fillable = [
'name', 'description', 'company_id', 'days'
];
public function company()
{
return $this->belongsTo('App/Company');
}
From my index.blade.php, I list the companies only and I have made them clickable so that when a user clicks on a company listed, they are taken to show.blade.php where the name of the company and the projects that belong to that company are displayed like so.
<div class="jumbotron">
<h1>{{ $company->name }}</h1>
<p class="lead">{{ $company->description }}</p>
</div>
<div class="row">
#foreach($company->projects as $project)
<div class="col-lg-4">
<h2>{{ $project->name }}</h2>
<p class="text-danger">{{ $project->description }}</p>
<p><a class="btn btn-primary" href="/projects/{{ $project->id }}" role="button">View Projects »</a></p>
</div>
#endforeach
</div>
Now am getting an undefined variable $project error. So I decided to declare variable in my show() function of the CompaniesController.php like so
public function show(Company $company)
{
$company = Company::find($company->id);
$projects = Company::find(1)->projects;
return view('companies.show', ['company' => $company, 'projects' => $projects]);
}
And access variable in show.blade.php like so
<div class="jumbotron">
<h1>{{ $company->name }}</h1>
<p class="lead">{{ $company->description }}</p>
</div>
<div class="row">
#foreach($projects as $project)
<div class="col-lg-4">
<h2>{{ $project->name }}</h2>
<p class="text-danger">{{ $project->description }}</p>
<p><a class="btn btn-primary" href="/projects/{{ $project->id }}" role="button">View Projects »</a></p>
</div>
#endforeach
</div>
Now am getting a Class 'App/Project' not found error when I access show.blade.php. I am having a challenge passing company projects to the view. Any help will be appreciated. Here are my routes;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('companies', 'CompaniesController');
Route::resource('projects', 'ProjectsController');
I would be hilarious if I am right....
In your models where defining relations replace App/Project with App\Project. Do the same for Company.... Replace "/" with "\".
You have to namespace Project class properly
Make sure file name is Project.php
Make sure inside Project.php namespace declaration is correct: namespace App;
Make sure class name inside Project.php is 'Project' : class Project extends Model { ...
Make sure you have imported it in controller. use App\Project
After all that done you will not get error:
Class 'App/Project' not found
You have correctly done passing variable in view but have a look here for another examples and methods passing about it:
https://laravel.com/docs/7.x/views
Hope this helps you
You're already using model binding. In your show method, you do not need to find. just return what you need
public function show(Company $company)
{
return view('companies.show', ['company' => $company];
}
In your view, you can then do:
#foreach($company->projects as $project)
...
#endforeach

Laravel Trying to route the same way as Reddit does with their subreddits

Hello fellow Stackoverflow users,
I am currently working on a hobby-project (a Reddit clone) using Laravel.
What I am trying to achieve is to use a similar routing to posts as Reddit does.
In my case I want to navigate to a page using the current subreddit-name and ID of the post (b/subreddit-name/postId) to determine where the website has to route to. I made it work to request the ID of the post, but I couldn't manage to get it done with the current subreddit-name the user clicks on (Subbie).
By the way: Subbie's the name which I am using instead of the term "subreddit", each post has a column stored in the database with the name of the subbie, as well as the ID of the post, so it should be all retrievable.
How can I make it possible so Laravel recognizes the "{subbie}" variable?
(Maybe I am using the wrong words to describe my problem, but I hope it all makes sense.)
Here is the code that I am using:
Router: web.php:
<?php
/*
|--------------------------------------------------------------------------
| 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('/', 'IndexController#index');
Route::get('/b/{subbie}/{id}', 'IndexController#post');
Controller: IndexController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Index;
class IndexController extends Controller
{
public function index() {
$posts = Index::all();
return view('index', ['posts' => $posts]);
}
public function post($id) {
$posts = Index::findOrFail($id);
return view('post', ['posts' => $posts]);
}
}
Model: Index.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Index extends Model
{
protected $table = 'posts';
protected $primaryKey = 'id';
}
View: index.blade.php:
#extends('layouts.app')
#section('content')
<div class="container mt-5">
<div class="col-12">
#if(count($posts) > 0)
<ul class="list-group">
#foreach ($posts as $post)
<a href="{!! url()->current() !!}/b/{{ $post->subbie }}/{{ $post->id }}">
<li class="list-group-item" style="text-decoration: none;">b/{{ $post->subbie }} · Posted by u/Test</li>
<li class="list-group-item" style="text-decoration: none;"><h5 class="h5">Tekst: {{ $post->body }}</h5></li>
</a>
#endforeach
</ul>
#else
<h1>Helaas zijn er nog geen artikels beschikbaar</h1>
#endif
</div>
</div>
#endsection
View 2: post.blade.php:
#extends('layouts.app')
#section('content')
<ul class="list-group">
<li class="list-group-item">ID: {{ $posts->id}}</li>
</ul>
#endsection

Laracasts tutorial: Is method 'Latest()' removed?

Is the method "latest" used in Controllers removed in newest version of Laravel?
In PHP Storm I get follow error: Method latest() not found in App/Thread.
public function index()
{
//
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
}
I'm following a LaraCasts tutorial, and browsing to said page gives me following error. -> forum.test/threads.
ErrorException (E_ERROR)
Method Illuminate\Database\Query\Builder::path does not exist. (View: D:\xampp\htdocs\forum\resources\views\threads\index.blade.php)
As per requested, my view: it is in resources/views/threads/index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Forum Threads</div>
<div class="panel-body">
#foreach ($threads as $thread)
<article>
<h4>
<a href="{{ $thread->path() }}">
{{ $thread->title }}
</a>
</h4>
<div class="body">{{ $thread->body }}</div>
</article>
<hr/>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Also, my routes.
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('threads', 'ThreadController');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
The error is not related to the code you posted. Method Illuminate\Database\Query\Builder::path does not exist.. You are calling somewhere path method which does not exist.
To answer your question, method latest() is still present in the (currently) newest version of Laravel 5.6:
https://laravel.com/api/5.6/Illuminate/Database/Query/Builder.html#method_latest
My guess would be you have an incorrect config of the Thread model relationships. Most probably you did not define path() relationship.
See this answer to similar question: https://stackoverflow.com/a/37934093/1885946

Categories