這個職位表中檢索來自多個表中的數據用ajax
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('prf_id')->unsigned();
$table->foreign('prf_id')->references('id')->on('profiles')->onDelete('cascade');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->longText('comment');
$table->integer('like')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('comments');
}
}
這個崗位模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $table='posts';
protected $fillable = ['status'];
protected $hidden = [];
public function profile(){
return $this->belongsTo('App\Profile', 'prf_id');
}
public function user(){
return $this->belongsTo('App\User');
}
public function comment(){
return $this->hasMany('App\Comment');
}
}
這是評價模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $table='comments';
protected $fillable = ['comment','like'];
protected $hidden = [];
public function post(){
return $this->belongsTo('App\Post', 'post_id');
}
public function profile(){
return $this->belongsTo('App\Profile', 'prf_id');
}
public function user(){
return $this->belongsTo('App\User');
}
}
在刀片頁面,我可以easilly檢索特定職位像所有評論: 假設我有$崗位作爲後
@foreach ($post->comment as $comment)
{{$comment->comment}}
但在阿賈克斯我怎麼能做這個 假設我返回響應() - > json($ posts); 什麼建議嗎?它會幫助我很多
包括JavaScript代碼 – Beginner