如何使用每个方法将结果放入视图中

时间:2018-04-06 22:16:51

标签: php laravel-5 collections

我想迭代结果而不是创建foreach循环 我使用each()方法:

$collection = Comment::all();

$comment =  $collection->each(function ($comment){
    dd($comment->comment);
});

当我dd()时,我得到了:

  

"帽子; “所以我应该这么想。"

但是当我转到视图时:

 return view('welcome')->with('comment',$comment);

我得到了

[{"id":1,"post_id":5,"comment":"Hatter; 'so I should think.","created_at":"2018-04-05 15:23:20","updated_at":"2018-04-05 15:23:20"},

{"id":2,"post_id":5,"comment":"Alice gently remarked;.","created_at":"2018-04-05 15:23:20","updated_at":"2018-04-05 15:23:20"},

依旧......

这是观点:

{{$comment}}

我想迭代整个集合并将数据放入$comment,然后在视图中显示它。

1 个答案:

答案 0 :(得分:0)

这很简单。 然后在属性名称

之后迭代import { Component, OnInit } from '@angular/core'; import {HttpClient} from '@angular/common/http'; import {Router} from '@angular/router'; import swal from 'sweetalert2'; import {UserService} from '../user.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit { datos: any; matricula: any; contrasena: any; constructor(private http: HttpClient, private router: Router, private user: UserService) { } ngOnInit() { if (this.user.getUserLoggedIn() === true) { this.router.navigate(['/productos']); } else { } } getCredentials() { this.http.get('http://localhost/cafeteria/login/validar.php').subscribe((data: any) => { this.datos = data; this.validar(); }); } validar() { if (this.datos[0].matricula === this.matricula) { if (this.datos[0].contrasena === this.contrasena) { this.user.setUserLoggedIn(); this.router.navigate(['/productos']); switch (this.datos[0].idrol) { case '1': this.router.navigate(['/productos']); break; case '2': this.router.navigate(['/cPanel']); break; case '3': this.router.navigate(['/cPanel']); break; } } } else { swal('Datos Incorrectos'); } } }
->