Im having a hard time extracting an item in an array, for example trying to retrieve a post in posts.
this is the error
keep in mind everything works, i can extract posts but im trying to successfully show the delete button to users.
im following laravel polices, to show a delete button for the user who made the post
laravel.com/docs/5.5/authorization#via-blade-templates
Undefined variable: post (View:
/Applications/MAMP/htdocs/eli42/resources/views/home.blade.php)
this is what i have so far
public function index()
{
$posts = Post::with('user')->get();
return view('home', compact('posts'));
}
home.blade.php
<div id="mypost" class="col-md-8 panel-default" ng-repeat="post in myposts ">
<div id="eli-style-heading" class="panel-heading"><% post.user.name %></div>
<div class="panel-body panel">
<figure>
<p> <% post.body %></p>
<p> <% post.created_at %></p>
</figure>
<span>
#foreach($posts as $post)
#if(Auth::user()->can('delete',$post) )
<i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)">
</i>
#endif
#endforeach
<span>Edit</span>
</span>
</div>
</div>
I'm not sure what im doing wrong
it needs to show one close button not 3
return returns program control to the calling module. Execution
resumes at the expression following the called module's invocation.
You are sending request on method getPosts for collecting posts. So you don't need to return posts variable by index method. Then you need it in getPosts.
You are using angular and in this case you need to have variable about delete availability but this variable you need in javascript. Just add it in controller and send to angular, then use ng-if attribute in angular for show/hide delete button.
In this case you need to change getPosts method as it is in answer what will add deletable variable in every post then you can use post.deletable in angular. What you need to end this task is that add ng-if="post.deletable" to <i> tag what means if delete post is available show this tag(button).
Controller
public function getPosts()
{
return response()->json(Post::with('user')->get()->map(function(Post $post){
return collect($post->toArray())->put('deletable', auth()->user()->can('delete', $post));
}));
}
public function index()
{
return view('home');
}
It looks like you are using Angular. For this case your blade seems good.
In this case you don't need $posts variable in view and you don't need #foreach too in blade.
<div id="mypost" class="col-md-8 panel-default" ng-repeat="post in myposts ">
<div id="eli-style-heading" class="panel-heading"><% post.user.name %></div>
<div class="panel-body panel">
<figure>
<p> <% post.body %></p>
<p> <% post.created_at %></p>
</figure>
<span>
<i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)" ng-if="post.deletable"></i>
<span>Edit</span>
</span>
</div>
</div>
You should try this:
Function
public function index() {
$posts = Post::with('user')->get();
return view('home', compact('posts'));
}
home.blade.php
<div id="mypost" class="col-md-8 panel-default" ng-repeat="post in myposts ">
<div id="eli-style-heading" class="panel-heading"><% post.user.name %></div>
<div class="panel-body panel">
<figure>
<p> <% post.body %></p>
<p> <% post.created_at %></p>
</figure>
<span>
#foreach(posts as $post)
#if(Auth::user()->can('delete',$post->id) )
<i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)">
</i>
#endif
#endforeach
<span>Edit</span>
</span>
</div>
</div>
Related
//here is my controller `
public function index($id){
$users=User::where('id',$id)->get();
return view('user',compact('users'));
}
}`
//here is my User model
public function getPosts()
{
return $this->hasMany('App\Post','user_id','id');
}
//here is my user blade
#foreach($user->getPosts as $post)
<div class="profile-content">
<section class="post-heading">
<div class="rows">
<div class="col-md-11">
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object photo-profile" src="https://scontent.fgyd4-2.fna.fbcdn.net/v/t1.0-9/p960x960/73248174_2597397730318615_6397819353256951808_o.jpg?_nc_cat=109&_nc_sid=7aed08&_nc_ohc=VxH_cAqJ778AX_7B97W&_nc_ht=scontent.fgyd4-2.fna&_nc_tp=6&oh=e5b02c7f91818664cf253e71f2ddf106&oe=5F5F4E74" width="40" height="40" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">{{$user->name}} </h4>
{{$user->$post['created_at']}}
</div>
</div>
</div>
<div class="col-md-1">
<i class="glyphicon glyphicon-chevron-down"></i>
</div>
</div>
</section>
<section class="post-body">
<p>{{ $user->getPost->post['post_description'] }}</p> <!-- i stick around here -->
</section>
//it give this error to me
Property [getPosts] does not exist on this collection instance. (View: C:\xampp\htdocs\site\resources\views\user.blade.php)
In controller get() will return collection,you want one user, so use find() or first(), it will return one user object
public function index($id){
$user=User::find($id); //or $user=User::where('id',$id)->first();
return view('user',compact('user'));
}
Relation method
public function posts()
{
return $this->hasMany('App\Post','user_id','id');
}
Since their is one to many relation between user and posts,you have to use loop to show posts for a user
foreach($user->posts as $post)
<div class="profile-content">
<section class="post-heading">
<div class="rows">
<div class="col-md-11">
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object photo-profile" src="https://scontent.fgyd4-2.fna.fbcdn.net/v/t1.0-9/p960x960/73248174_2597397730318615_6397819353256951808_o.jpg?_nc_cat=109&_nc_sid=7aed08&_nc_ohc=VxH_cAqJ778AX_7B97W&_nc_ht=scontent.fgyd4-2.fna&_nc_tp=6&oh=e5b02c7f91818664cf253e71f2ddf106&oe=5F5F4E74" width="40" height="40" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">{{$user->name}} </h4>
{{$post->created_at}}
</div>
</div>
</div>
<div class="col-md-1">
<i class="glyphicon glyphicon-chevron-down"></i>
</div>
</div>
</section>
<section class="post-body">
<p>{{ $post->post_description }}</p> <!-- i stick around here -->
</section>
#endforeach
$users=User::where('id',$id)->get(); returns a collection of users
So when you do this:
#foreach($user->getPosts as $post)
$user is a collection of users. That collection doesn't have a getPosts() method.
you are returning list of users, so you don't have one user and you can't have the posts of all certain user
below code can fix the issue:
User::where('id', $id)->first();
First of all you should update your controller like this
User::find($id);
You should update your relation like this
public function posts()
{
return $this->hasMany('App\Post','user_id','id');
}
After that you should update your blade like this
foreach ($user->posts as $post)
You can't access posts with ->getPosts. If you want, you should use like this ->getPosts()->get()
I'm having an issue displaying a button to a post if a user made it, it renders out multiple buttons when i only need one
Note:im using laravel, and im utilizing blade templating for policies, and its the following line of code
#if(Auth::user()->can('delete',$post))
i need the post variable, in order for it to work. however if i don't do a for each, the post would be undefined, i need a way for the if statement to know its one of the post with out looping through posts this will render multiple buttons.
I get this:
HTML:
<div id="mypost" class="col-md-8 panel-default" ng-repeat="post in myposts">
<div id="eli-style-heading" class="panel-heading"><% post.title %></div>
<div class="panel-body panel">
<figure>
<p> <% post.body %></p>
<p> by: <% post.user.name %></p>
<p> <% post.created_at %></p>
</figure>
<span>
#foreach($posts as $post)
#if(Auth::user()->can('delete',$post))
<i style="color:red;" class="glyphicon glyphicon-remove" ng-click="deletePost(post)"></i>
#endif
#endforeach
</span>
</div>
</div>
PostController.php:
public function index()
{
$posts = Post::with('user')->get();
return view('home', compact('posts'));
}
I'm new on laravel and i'm trying to make on-many relationship between category table and devices table on database
in Category view i have all the category and i want when i clik to one category to go to another view and show all the devices of this category
It work's with my using rsource controller for the category and in the show method : CatgoryController.php
public function show($id)
{
$categories = Category::find($id);
$all_devices = $categories->devices;
return view('devices',compact('categories',$categories,'all_devices',$all_devices));
}
and in the devices view i do:
devices.blade.php
#extends('master')
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-12 text-center animateup">
<div class="inline-icons-text section-heading">
<div class="inline-icon">
<hr/><hr/><hr/><hr/><hr/>
</div>
<span class="inline-icon heading-width">
<span class="heading-text"><h1> {{$categories->category_name}} </h1> </span>
</span>
<div class="inline-icon">
<hr/><hr/><hr/><hr/><hr/>
</div>
</div>
</div>
</div>
<br/>
<div class="row text-center">
#foreach ($all_devices as $device)
<div class="col-md-3 col-sm-6 animateleft">
<h5>
{{$device->device_name}}
<span class="glyphicon glyphicon-chevron-right"></span>
</h5>
</div>
#endforeach
</div>
</div>
#stop
this is how i show all the category and when click on one it goes to anthor view to show the devics of this category
categories.blade.php
<div class="row text-center">
#foreach ($categories as $categories)
<div class="col-md-3 col-sm-6 animateleft">
<h5>
{{$categories->category_name}}
<span class="glyphicon glyphicon-chevron-right"></span>
</h5>
</div>
#endforeach
</div>
and finally this my rout Rout.php
Route::resource('categories','CategoryController');
Route::resource('/devices','DevicesController');
My Problem is that evry thing work but the page that have the devices of one category dosen't have the style of the site
move ur stylesto public folder into any subfolder..here i'm using devices as sub-folder and devices.css is my file..
place link like this in ur master blade.with ur folder and file names
<link rel="stylesheet" href="{{URL::to('/')}}/devices/devices.css">
I´m trying to do a simple blog with Laravel 5.3. Index page include aside with categories and tags, and we can filter different post with categories or tags names.
In routes I have this.
Route::get('categories/{name}',[
'as'=>'front.search.category',
'uses'=>'FrontController#searchCategory'
]);
Route::get('tags/{name}',[
'as'=>'front.search.tag',
'uses'=>'FrontController#searchTag'
]);
In Article
public function scopeSearchCategory($query,$name){
$query->where('name','=',$name);
}
In Tag
public function scopeSearchTag($query,$name){
$query->where('name','=',$name);
}
In FrontController.
public function searchCategory($name){
$tags=Tag::all();
$sliders=Slider::all();
$categories = Category::SearchCategory($name)->first();
$articles = $categories->articles()->paginate(5);
$articles->each(function($articles){
$articles->category;
$articles->images;
});
return view('front.index')->with('articles', $articles)->with('sliders', $sliders)->with('categories', $categories)->with('tags',$tags);
}
public function searchTag($name){
$categories=Category::all();
$sliders=Slider::all();
$tags = Tag::SearchTag($name)->first();
$articles = $tags->articles()->paginate(5);
$articles->each(function($articles){
$articles->category;
$articles->images;
});
return view('front.index')->with('articles', $articles)->with('sliders', $sliders)->with('categories', $categories)->with('tags',$tags);
}
In aside
<aside class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title">{{trans('app.title_categories')}}</h2>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($categories as $category)
<a href="{{route('front.search.category',$category->name)}}">
<li class="list-group-item">
<span class="badge">{{$category->articles->count()}}</span>
{{$category->name}}
</li>
</a>
#endforeach
</ul>
</div>
</aside>
<aside class="panel panel-success">
<div class="panel-heading">
<h2 class="panel-title">Tags</h2>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($tags as $tag)
<a href="{{route('front.search.tag',$tag->name)}}">
<li class="list-group-item">
<span class="badge">{{$tag->articles->count()}}</span>
{{$tag->name}}
</li>
</a>
#endforeach
</ul>
</div>
</aside>
When a click on aside href to filter tags or categories return 'Trying to get property of non-object'. If I remove scopeSearchs and put in FrontController functions $Tag:all() or Category:all() href works, but filter fail, return all post. ¿Any idea why return this error? ¿Can´t do foreach if return one object?
You should use
$tags = Tag::searchTag($name)->first();
When you are using scope function it should always be camelCase
so searchTag() should be start with small s not capital s
You should remove foreach
use this
<a href="{{route('front.search.tag',$tags->name)}}">
<li class="list-group-item">
<span class="badge">{{$tags->articles->count()}}</span>
{{$tags->name}}
</li>
</a>
I have looked at a similar question but unfortunately the answer does not apply to me.
I know that my object does contain my data but the inside of the #foreach is never called in my view.
Here is my controller:
$newest_article = $this->article->orderBy('created_at', 'DESC')->first();
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
return View::make('site/news/index', compact('newest_article', 'articles'));
My model's scopeAllButFirst function:
public function scopeAllButFirst($query, $id){
return $query->where('id', '<>', $id);
}
And lastly my view:
#extends('site.layouts.default')
#section('content')
<section id="news-main">
<div class="container">
<!-- all posts wrapper -->
<div class="news-box-wrap js-packery clearfix" data-packery-options='{ "itemSelector": ".news-item"}'>
#foreach($articles as $article)
<div class="col-sm-4 news-item">
<a href="{{{ $article->url() }}}" class="news-box-inner">
<div class="news-box-image-wrap">
#if($article->image->url('tile_landscape'))
<img src="{{{ $article->image->url('tile_landscape') }}}" class="news-box-image">
#endif
<span class="news-box-icon">
<i class="fa fa-align-left"></i>
</span>
</div>
<div class="news-box-bottom">
<div class="news-box-data">
<span class="news-box-cat">News</span>
•
<span class="news-box-time">{{{ $article->date() }}}</span>
</div>
<h3>{{{ $article->title }}}</h3>
<div class="news-box-det">
<span class="news-box-author">{{{ $article->author->username }}}</span>
<span class="news-box-like"><i class="fa fa-smile-o fa-lg"></i> 225</span>
<span class="news-box-comment"><i class="fa fa-comment-o fa-lg"></i> 16</span>
</div>
</div>
</a>
</div>
#endforeach
</div>
#stop
I need to loop through the Eloquent object because I have to access some of the methods in my model class.
just as a reference, I am following andrewelkins/Laravel-4-Bootstrap-Starter-Site
UPDATE
In my debugging I noticed if I replace
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
with
$articles = Article::all();
I don't have this problem.
For those facing the problem, the answer as usual is in the API. Turns out you have to call the get() method in order to receive a Collection. And that is what all() returns. So simply changing
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC');
to
$articles = Article::allButFirst($newest_article->id)->orderBy('created_at', 'DESC')->get();
solves the problem.
Hope it helps