Laravel - Undefined variable: articles - php

public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', compact('articles'));
}
I'm receiving an error Undefined variable: articles. Above is the code I'm using in my controller. What is the error that I'm making?
VIew
#if(!empty($articles))
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
#endif

Just check if there are any articles in your search, like:
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = [];
}
And use it in the view:
#forelse($articles as $article)
...
#empty
<p>There are no articles!</p>
#endforelse

You are declaring the variable inside the if, but if the condition is false, the variable is never set. Try to use an else condition:
public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = null;
}
return view('pages.press.index', compact('articles'));
}
You will also need to check if $articles is null in your view.

that's because the variable is never assigned a value and does not exist in the external context of the -if
try to use this
public function press (Request $request)
{
$articles = array();
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', [
'articles' => $articles
]);
}
View
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
I hope this help you

Related

How to show image from database with eager loading?

I want to display multiple product image from database with eager loading, and when i tried to displaying it, console said localhost:8000/storage/ 404 not found. Any ideas how to solve this? this is my first time using eager loading & relationship on Laravel. Thank you!
Here is my controller :
public function homeProduct(){
$products = Product::with('productCategory')->get();
$images = Product::with('productImage')->get();
return view('home.index', compact('products', 'images'));
}
Product Model :
public function productImage()
{
return $this->hasMany(ProductImage::class, 'product_id', 'id');
}
Product Image Model :
public function product()
{
return $this->belongsTo(Product::class, 'id');
}
Here are the index.blade.php View :
#foreach ($products as $p)
<div class="col-lg-6 col-12">
<div class="portfolio-thumb mb-5" data-aos="fade-up">
<div id="yoreBeans" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
#foreach ($images as $key => $image)
<div class="carousel-item {{ $key == 0 ? 'active' : '' }}">
<a href="{{ asset('storage/'.$image->path) }}" class="image-popup">
<img src="{{ asset('storage/'.$image->path) }}" class="img-fluid portfolio-image" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="portfolio-info">
<h3 class="text-black">{{ $p->name }}</h3>
<h4 class="text-danger">{{ $p->productCategory->name }}</h4>
</div>
</div>
</div>
#endforeach
Your method $images = Product::with('productImage')->get(); will also return Product objects with images.
What you exactly want is to eager load both realtions and then reference it at the same time:
public function homeProduct(){
$products = Product::with(['productCategory', 'productImage'])->get();
return view('home.index', compact('products'));
}
And your view like this:
#foreach ($products as $p)
<div class="col-lg-6 col-12">
<div class="portfolio-thumb mb-5" data-aos="fade-up">
<div id="yoreBeans" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
<!-- // this line changed --->
#foreach ($p->productImage as $key => $image)
<div class="carousel-item {{ $key == 0 ? 'active' : '' }}">
<a href="{{ asset('storage/'.$image->path) }}" class="image-popup">
<img src="{{ asset('storage/'.$image->path) }}" class="img-fluid portfolio-image" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="portfolio-info">
<h3 class="text-black">{{ $p->name }}</h3>
<h4 class="text-danger">{{ $p->productCategory->name }}</h4>
</div>
</div>
</div>
#endforeach

How to display all Jobs that belongs to Company in Laravel

I need to display only jobs that belogs to company in companies/show.blade.php
This is show method in CompanyController:
public function show(Company $company)
{
$jobs = Job::where('company_id', $company->id)
->latest()
->get();
return view('pages.companies.show', [
'jobs' => $jobs
]);
}
This is foreach in show.blade:
#foreach($jobs as $job)
<div class="col-12 col-md-5">
<h2>{{ $job->job_name }}</h2>
</div>
<div class="col-12 col-md-7">
<p>{{ $job->description }}</p>
</div>
#endforeach
But it doesnt display anything. Where is mistake?
first of all you have define relations as below:
Company Model
public function jobs(){
return $this->hasMany('App\Job');
}
Job Model
public function company(){
return $this->belongsTo('App\Company');
}
Controller
public function show(Company $company)
{
$jobs = $company->jobs;
return view('pages.companies.show', [
'jobs' => $jobs
]);
}
Blade View
#foreach($jobs as $job)
<div class="col-12 col-md-5">
<h2>{{ $job->job_name }}</h2>
</div>
<div class="col-12 col-md-7">
<p>{{ $job->description }}</p>
</div>
#endforeach

Display data in the blade from the database

How could I display data from the database in certain tables in my view? Without foreach, because it would overlap me. I want to take an id and that will be displayed and if I click on another id, it will display what I want from the other id. Here's how I did it.
Controller:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::all();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
Blade:
<div class="card-body p-0">
<div class="mailbox-read-info">
<h5 align="center">{{ $posts->title }}</h5>
<h6> From userID: {{ $posts->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $posts->created_at }}</span></h6>
</div>
<div class="mailbox-read-message">
<p>{{ $posts->content }}</p>
</div>
I must say that I have looked at other issues like this, but I can't get a tip?
#foreach($posts as $post)
<div class="card-body p-0">
<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>
</div>
</div>
#endforeach
EDIT: If you want to return only 1 post, use this in your controller:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::where('id', 1)->first();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
$posts is a collection so you have to iterate over it - example:
<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>
</div>
#endforeach
</div>

Laravel 5 FatalErrorException in a056f8dd3913f4dd0869e1f55450ecd4 line 6: syntax error, unexpected ':', expecting '('

Im working on a user search by following a tutorial. I can get the script to show the raw data but when I use:
return view('search.results')->with('users', $users);
I get the FatalError Exception
Heres my search controller:
namespace Aries\Http\Controllers;
use DB;
use Aries\Models\User;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function getResults(Request $request)
{
$query = $request->input('query');
if(!$query) {
return redirect()->route('home');
}
$users = User::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%{$query}%")->orWhere('username', 'LIKE', "%{$query}%")->get();
return view('search.results')->with('users', $users);
}
}
search/results.blade.php
#extends('templates.default')
#section('content')
<h3>Your search for "{{ Request::input('query') }}"</h3>
#if (!$users->count())
<p>No results found, sorry.</p>
#elseif
<div class="row">
<div class="col-lg-12">
#foreach ($users as $user)
#include('user/partials/userblock')
#endforeach
</div>
</div>
#stop
user/userblock.blade.php
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" alt="" src="">
</a>
<div class="media-body">
<h4 class="media-heading">{{ $user->getNameOrUsername() }}</h4>
#if ($user->location)
<p>{{ $user->location }}</p>
#endif
</div>
</div>
I have looked over everything and I cant find the problem, Works fine when I comment out that return view();
#if (!$users->count())
<p>No results found, sorry.</p>
#elseif
elseif is expecting a second conditional statement there. Either use #else, or add a condition.

Undefined variable: $ Laravel 5

This is the code from CategoriesController:
public function index()
{
$categories = Category::all();
return view('/home', compact('categories'));
}
And this is the code that I'm writing to the home.blade.php
#foreach($categories as $cat)
#endforeach
And then in home.blade.php
I'm getting this error: Undefined variable: categories
Why this happening ? Everything works fine with Articles but not categories and the code is the same.
Home.blade.php
#extends('app')
#section('content')
<div class="col-md-4 pull-right">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Categories</h3>
</div>
<div class="panel-body">
#foreach($categories as $cat)
<div class="col-lg-6">
<ul class="list-unstyled">
<li>News
</li>
</ul>
</div>
#endforeach
</div>
</div>
</div>
#if(count($articles))
#foreach($articles as $article)
<div class="panel panel-default col-md-8">
<div class="panel-heading">
<h3>{{ $article->title }}</h3>
<small>posted by {{ $article->author->name }} {{ $article->created_at }}</small>
</div>
<div class="panel-body">
{!! str_limit($article->body, 1000) !!}
<hr>
Read More
</div>
</div>
#endforeach
#else
<h1>No articles at the moment.</h1>
#endif
<br>
{!! $articles->render() !!}
#stop
You have two routes
Route::get('/home', 'ArticlesController#index');
Route::get('/home', 'CategoriesController#index');
Which means when you visit /home, only ArticlesController#index is fired, not both.
This means that the $categories variable used in the view is not populated because your index() method in CategoriesController is intended to do this but is never called.
Therefore you need to do something like this
Route::get('/home', 'HomeController#index');
public function index()
{
$categories = Category::all();
$articles = Article::all();
return view('/home', compact('articles', 'categories'));
}
try to use
return View::make('/home')->with(compact('categories'));
instead of
return view('/home', compact('categories'));
I insert this in the top of my #foreach
<?php
use App\models\PersonalContact;
$data = PersonalContact::all();
?>
or this before #foreach add #if statement like this
#if (count($data) > 0)
#foreach ($data as $bloginfo)
<tr role="row" class="odd">
<td class="sorting_1">{{$bloginfo->name}}</td>
<td>{{$bloginfo->description}}</td>
<td>{{$bloginfo->email}}</td>
<td>{{$bloginfo->phone}}</td>
<td><img src="{{url('images/'.$bloginfo->image)}}" alt="" width="100px" height="100px"></td>
<td><i class="fas fa-edit"></i>Add | <i class="fas fa-edit"></i>Edit | <a class="btn btn-app"><i class="fas fa-edit"></i>Delete</a></td>
</tr>
#endforeach
#endif
return view('/home', compact('categories'));
The problem was $categories on compact method, you must use like a string

Categories