2
我有创建一个用户和评论之间一对多的关系模型:如何使用laravel控制器中的关系来检索数据?
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class comment extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
我如何使用DB来获得使用的模型功能(关系)使用控制器内的模式?
除了使用路线:
Route::get('/test', function(){
$Comments = App\Comment::all();
return view('Commentss', ['Comments' => $Comments]);
});
我想使用的控制器,但不起作用?
$Comments = DB::table('comments')->get(); (is there a way to use DB with model)
Or
use App\Comment;
$Comments = App\Comment::all(); (this says Class 'App\Http\Controllers\App\Comment' not found)
因此,在年底,我可以考虑通过检索数据:
<p>{{$Comment->user->whatever}}</p>
Bea T我对它XD – g3mini
它作为一种魅力,谢谢,:) – DomainFlag
很高兴它帮助。 ) –