How to use Blade inside ng-repeat? - php

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'));
}

Related

how to get an item in an iteration using foreach?

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>

laravel show method doesn't have the style

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">

Laravel 5.2 Undefined variable theme

I'm making a forum and the if the user clicks on a theme, he gets redirected to the page where all the topics are that belong to that theme. What I'm trying to do, is that the user is able to click on the topic and gets redirected to it.
The problem is, is that I get an error saying Undefined variable: theme
As you can see in the code below, I have a foreach loop in my theme.blade.php which loops all the topics that belong to the theme he/she clicked on. In the beginning of the loop there is a <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1 which is supposed to bring the user to the topic that he/she clicked on. The URL that is supposed to show is forum.dev/theme/{theme_id}/topics/{topic_id} But that doesn't work.
And it gives me that error code. All the code is below, If i miss some code, Please inform me about it and i'll update the question. Thanks in advance
theme.blade.php
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 125, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Posted by: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Last reply</h6>
<p class="center-align"></p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController#show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController#topic')->name('showtopic');
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {
//THEMES
Route::get('/theme/{theme_id}/edit', 'ThemesController#edit')->name('edittheme');
Route::patch('/theme/{theme_id}/edit', 'ThemesController#update')->name('updatetheme');
Route::get('/theme/create', 'ThemesController#create')->name('createtheme');
Route::post('/theme/create', 'ThemesController#save')->name('savetheme');
Route::delete('/theme/{theme_id}/delete', 'ThemesController#destroy')->name('deletetheme');
//TOPICS
Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#update')->name('updatetopic');
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#save')->name('savetopic');
Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController#destroy')->name('deletetopic');
});
Route::get('user/profile', 'UserController#profile')->name('showprofile');
Route::post('user/profile', 'UserController#update_avatar');
TopicsController.php (Show method)
public function show($id)
{
$theme = Theme::find($id);
$topics = Topic::find($id);
return view('topics/topic')->with('topics', $topics)->with('theme', $theme);
}
I don't know if you can use the double '->with' option.
What will work is the following:
return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]);
You pass an array as the second argument of the view. You can add as many keys and variables as you like.

Trying to get property of non-object in aside

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>

Laravel and nested foreach of database query error

I am building a menu on laravel.
In blade layout i have:
#foreach ($menu1 as $menu1)
{{$menu1->nome}}
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
<span class="glyphicon glyphicon-chevron-down"></span>
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<ul class="list-unstyled">
#foreach ($menu2 as $menu2)
<li> {{$menu2->nome}} </li>
#endforeach
</ul>
</div>
</div>
</div>
#endforeach
in my controller i pass 2 query.
public function menu($lingua)
{
return View::make('index', ['menu1'=>DB::table('cat_nome')->join('lingua', 'cat_nome.id_lingua', '=', 'lingua.id') ->where('lingua.lingua','=',$lingua)->get(array('cat_nome.nome')),
'menu2' => DB::table('campo_nome')->join('lingua', 'campo_nome.id_lingua', '=', 'lingua.id') ->where('lingua.lingua','=',$lingua)->get(array('campo_nome.nome'))]
);
}
I tried to UNnest the foreach and everything works fine. When i try to nest the foreach i get
Trying to get property of non-object
#foreach ($menu2 as $menu2)
That line is most likely the culprit. You cannot use the same variable name twice in a foreach loop like that and expect good results. The second one is supposed to be what each entry in $menu2 will be scoped to during each iteration of the loop.
See the foreach documentation
Try changing the loop to look more like this:
#foreach ($menu2 as $innerMenu)
<li> {{$innerMenu->nome}} </li>
#endforeach
I would suggest doing the same thing for your outer menu as well.

Categories