我想要实现的是@foreach发布所有新闻,@ foreach在另一个@foreach之间发布所有评论,并发布新闻帖中的ID。
我不确定如何将此ID传递给getNewsComments函数。
我的控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\News;
use App\newsComments;
class newsController extends Controller
{
public function getAllNews(){
$results = News::all();
return view('index')->with('news', $results);
}
}
路线:
Route::get('/', 'newsController@getAllNews');
新闻模式:
class News extends Model
{
// set table
protected $table = 'lg_news';
public function newsComments(){
return $this->hasMany('App\newsCommments');
}
}
评论模型:
class newsComments extends Model
{
// set table name
protected $table = 'lg_newscomments';
}
视图
@foreach ($news as $article)
@foreach($news->$newsComments as $comment)
@endforeach
@endforeach
错误:
未定义的变量:newsComments(查看: C:\ XAMPP \ htdocs中\资源\视图\ index.blade.php)
答案 0 :(得分:1)
您不需要多个路线,只需要两个相互关联的表格,其中1:N关系
第(1)条 - >评论(N)
然后,您将为每个表创建一个模型,并创建如Laravel文档中所述的关系
One to Many relationship in Laravel
然后您将获取所有帖子并将其传递给视图:
public function getAllArticles()
{
$posts = Post::all();
return view('view-name', $posts);
}
最后,创建一个视图并显示帖子和评论:
@foreach($posts as $post)
{{ $post->title }}
{{ $post->body }}
@foreach($post->comments as $comment)
{{ $comment->title }}
{{ $comment->body }}
@endforeach
@endforeach
提醒:$post->comments
,评论是在您创建关系的模型中定义的方法名称
在web.php上定义路线:
Route::get('/', 'ControllerName@getAllArticles');
转到localhost:8000/
(如果托管了网站,则转到您的网站域)以查看结果
答案 1 :(得分:1)
更改此行:
set theSubject to "Voicemail"
set theContent to read "/private/tmp/voice.tgCrnv/BODY.txt"
set theAddress to "me@example1.com"
set theSender to "me@example2.com"
set theAttachmentFile to "/private/tmp/voice.tgCrnv/msg_1bb3b4f2-c6b1-4012-89c0-a19177cc6ca2.wav" as POSIX file as alias
tell application "Mail"
set msg to make new outgoing message with properties ¬
{subject:theSubject, content:theContent, visible:false, sender:theSender}
tell msg
make new to recipient at end of every to recipient with properties {address:theAddress}
make new attachment at end of last character of content with properties ¬
{file name:theAttachmentFile}
end tell
delay 1
send msg
end tell
到
return view('index')->with('news', $results);
它可能会起作用。
PS:return view('index', ['news' => $results]);
功能会设置一个会话!它不会传递变量来查看。