Could someone let me know why I get this error:
"Undefined variable: listings (View: \app\views\pages\listings.blade.php)"
Controller
class ListingsController extends BaseController {
public function showListings() {
$listings = Listing::paginate(10);
return View::make('pages.listings', $listings);
}
}
View
<div class="container">
<div id="main">
<div class="row">
<div class="span9">
<h1 class="page-header">Listings</h1>
#foreach($listings as $listing)
{{ $listing->address }}
#endforeach
</div>
</div>
</div>
</div>
#include('includes.footer')
Try doing like this:
return View::make('pages.listings')->with('listings', $listings);
or
return View::make('pages.listings', array('listings' => $listings));
or even
return View::make('pages.listings', compact('listings'));
Related
I got an error says undefined variable data referring to my foreach in my blade
so my errors is :
Undefined variable: data (View: C:\Users\adila\Desktop\onlineshop\onlineshop\resources\views\user\product.blade.php)
so this is my blade file :
<div class="latest-products">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section-heading">
<h2>Latest Products</h2>
view all products <i class="fa fa-angle-right"></i>
</div>
</div>`enter code here`
#foreach($data as $product)
<div class="col-md-4">
<div class="product-item">
<img src="assets/images/product_06.jpg" alt="">
<div class="down-content">
<h4>{{$product->title}}</h4>
<h6>$ {{$product->harga}}</h6>
<p>{{$product->deskripsi}}</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
and this is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Product;
class HomeController extends Controller
{
public function redirect(){
$usertype = Auth::user()->usertype;
if($usertype=='1'){
return view('admin.home');
}
else{
return view('user.home');
}
}
public function index(){
if(Auth::id()){
return redirect('redirect');
}
else{
$data = Product::all();
return view('user.home',compact('data', $data));
}
}
}
and my Route :
Route::get('/', [HomeController::class, 'index']);
Please help me
Please remove $data from compact() function
return view('user.home',compact('data'));
You passed the variable $data to the view('user.home', compact('data')) but you have an error in the product.blade I think the problem is on the product. blade file. check that file
You error is in the product.blade.php view and in your controller you're returning the home.blade.php view.
You can avoid the Undefined variable error in your product.blade.php adding an isset in your view like this :
#if(isset($data))
#foreach($data as $product)
(...)
#endforeach
#endif
I have a custom authentication guard Applicant and already have created ApplicantsProfile model.
Problem 1 :
so far i am stuck with the show method, it is working but whatever ID i passed to the route it renders the view .
Problem 2 :
I want to access each applicant profile by it's ID
Problem 3 :
How do edit and update the authenticated user profile?
Profile Model
class ApplicantsProfile extends Model
{
protected $fillable = [
'job_title', 'applicant_id',
];
public function applicant()
{
return $this->belongsTo(Applicant::class);
}
}
Profile Controller
class ApplicantsProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth:applicant');
}
public function show($id)
{
$profile = ApplicantsProfile::find($id);
return view('applicant.profile', compact('profile'));
}
}
Profile View
#extends('layouts.auth')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card">
<div class="card-header">{{Auth::guard('applicant')->user()->name}}</div>
<div class="card-body">
{{Auth::guard('applicant')->user()->profile->job_title}}
</div>
</div>
</div>
</div>
</div>
#endsection
Route
Route::get('/account/{profile}', 'ApplicantsProfileController#show');
If I go to route: account/1 or account/2 it renders the view of the authenticated user.
in your profile view, you use Auth so it's normal to show these results.
you have to modify your view blade to something like :
#extends('layouts.layout')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card">
<div class="card-header">
{{ $applicantsProfile['name'] }}
</div>
<!-- complete your code like so -->
in your controller you have to do something like:
public function show(ApplicantsProfile $applicantsProfile)
{
return view('applicant.profile', compact('applicantsProfile'));
}
and in your web.php route:
Route::get('/account/{applicantsProfile}', 'ApplicantsProfileController#show');
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 have the following action in my controller:
public function viewPost($id)
{
$current_post = forum_post::with('replies')->where('id', $id)->get();
return view('forumViewPost',['currentPost', $current_post]);
}
Then I have the following view:
#extends('layout.app')
#section('content')
<div class="container">
<div class="nk-gap-2"></div>
<ul class="nk-forum nk-forum-topic">
<li>
<div class="nk-forum-topic-author">
<img src="assets/images/avatar-2-sm.jpg" alt="Kurt Tucker">
<div class="nk-forum-topic-author-name" title="Kurt Tucker">
{{$currentPost->nickname}}
</div>
<div class="nk-forum-topic-author-role">
Member
</div>
</div>
<div class="nk-forum-topic-content">
{{$currentPost->content}}
</div>
<div class="nk-forum-topic-footer">
<span class="nk-forum-topic-date">June 19,
2017</span> <span class="nk-forum-action-btn"> Reply</span>
<span class="nk-forum-action-btn"> Spam</span>
<span class="nk-forum-action-btn"><span class="nk-action-heart liked"><span class="num">1</span>
Like</span></span>
</div>
</li>
#foreach($currentPost->replies as $replies)
#endforeach
</ul>
</div>
#endsection
now when I run this. I get the following error:
Undefined variable: currentPost
Can anyone tell me what I've done wrong?
If you use [] you may write this ['currentPost' => $current_post]
All
public function viewPost($id)
{
$current_post = forum_post::with('replies')->where('id', $id)->get();
return view('forumViewPost',['currentPost' => $current_post]);
}
You can also you compact(''); and as Wreigh mentioned change $current_post to $currectPost
public function viewPost($id)
{
$currentPost = forum_post::with('replies')->where('id', $id)->get();
return view('forumViewPost', compact('currentPost'));
}
Here i'm trying to to loop through all my users with their related assignments. I have made the User->Assignment Relationship in the model. When I dd($students) I receive the array of students with their related assignments, however when I view this on the page I do not receive the loop of assignments. What could I have missed in the code? Im sure it's somewhere in the html I messed up.
User Model:
public function assignments()
{
return $this->hasMany('App\Assignment');
}
Assignment Model:
public function userAssignment()
{
return $this->belongsTo('App\User');
}
Controller:
$students = User::where('position','student')->with('assignments')->get();
Html:
#foreach($students as $student)
<div class="item{{{ $isFirst ? ' active' : '' }}}">
<div class = "deck">
<div class = "row marg">
<div class="col-md-8">
<div class = "stdnt">
<h1>Student Info</h1>
<h1>{{$student->name}}</h1>
<p>Program: {{$student->pack}}</p>
<p>Level: {{$student->level}}</p>
<p>Status: {{$student->status}}</p>
<p>Lesson: {{$student->lesson}}</p>
</div>
</div>
<div class = "col-md-4">
<div class = "teac">
<h1>Teacher Info</h1>
<h1>{{$student->teacher}}</h1>
<p>Picture</p>
<p>assign form</p>
<p>assign button</p>
</div>
</div>
</div>
</div>
<div class = "mask flow">
<div class = "row-fluid marg">
#foreach($student->assignments() as $assignment)
<div class = "ablock">
<h1>{{$assignment->title}}</h1>
<p>{{$assignment->created_at}}</p>
<p>{{$assignment->type}}</p>
<p>{{$assignment->body}}</p>
</div>
#endforeach
</div>
</div>
{{--*/ $isFirst = false; /*--}}
</div>
#endforeach
Don't call the assignments method. Simply access it as a property:
#foreach($student->assignments as $assignment)
// ^^
#endforeach