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.
Related
Okay i'm trying get "likes" and "users" in Posts by relationship hasOne.
here is my Post.php Model
class Posts extends Model
{
protected $table = 'posts';
public function User()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function Like()
{
return $this->hasOne(Like::class, 'post_id', 'id');
}}
My Blade template
#foreach ($showdeals as $deal)
<div class="tab-pane active" id="home" role="tabpanel">
<div class="card-body">
<div class="profiletimeline">
{{$deal->like->status}}
<br>
{{$deal->user->email}}
<div class="sl-item">
<div class="sl-left"> <img src=" {{asset( '/assets/images/users/2.jpg')}}" alt="user" class="img-circle"> </div>
<div class="sl-right">
<div> {{$deal->user->username}} || {{$deal->subject}} <Br> <span class="sl-date">{{$deal->created_at}}</span>
<div class="m-t-20 row">
<div class="col-md-3 col-xs-12"><img src="{{$deal->image}}" alt="user" class="img-responsive radius"></div>
<div class="col-md-9 col-xs-12">
<p> {{$deal->body}} </p> עבור למוצר </div>
</div>
<div class="like-comm m-t-20"> 2 תגובות <i class="fa fa-heart text-danger"></i> 5 לייקים </div>
</div>
</div>
</div>
</div>
<hr></div>
</div>
#endforeach
And there is my Controller
class PostsController extends Controller
{
public function showdeals()
{
$showdeals = Posts::with( 'User', 'Like')->get();
return view('posts.show', compact('showdeals'));
}
public function helpnewview(){
return view('posts.anew');
}
public function helpnew(Request $request){
//User pick link
$userlink = $request['userlink'];
return \Redirect::route('newdeal', compact('userlink'));
}
public function new(Request $request)
{
//Emdeb user link
$link = Embed::create($request['userlink']);
$linke = $request['userlink'];
return view('posts.new', compact('link', 'userlink', 'linke'));
}
public function create(Request $request)
{
$posts = New Posts;
$posts->user_id = Auth::User()->id;
$posts->subject = $request['subject'];
$posts->body = $request['body'];
$posts->link = $request['link'];
$posts->price = $request['price'];
$posts->image = $request['image'];
$posts->tag = $request['tag'];
$posts->save();
return back();
}
}
Now if I do something like {{$deal->user->email}} its will work,
if I go to something like this {{$deal->like->status}} its does not work,
am I missing something ?
If you want multiple relationships to be eagerly loaded you need to use an array of relationships: Model::with(['rl1', 'rl2'])->get();
public function showdeals()
{
...
$showdeals = Posts::with(['User', 'Like'])->get();
...
}
EDIT:
From that json in the comments that I see, there is no attribute named status in your Like model so thats probably the root of the problem
Controller edit this code
public function showdeals()
{
$showdeals = Posts::all();
return view('posts.show', compact('showdeals'));
}
And blade file code
#foreach ($showdeals as $deal)
<div class="tab-pane active" id="home" role="tabpanel">
<div class="card-body">
<div class="profiletimeline">
{{ $deal->Like->status }}
<br>
{{ $deal->User->email }}
#endforeach
I think everything is good except
{{$deal->like->status}} {{$deal->user->email}}
Please try as
{{$deal->Like()->status}}
<br>
{{$deal->User()->email}}
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'));
}
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.
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
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'));