2016-11-10 7 views
0

это сообщения таблица:извлекать данные из нескольких таблицы с помощью Ajax

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreatePostsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('posts', 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->longText('status'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('posts'); 
    } 
} 

это комментарии таблица:

<?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}} 

но в AJAX, как мог я сделайте это предположим i return response() -> json ($ posts); любое предложение? Это поможет мне много

+0

включить свой код яваскрипта – Beginner

ответ

0

Вам не нужно писать response()->json($posts), Вы можете просто return $posts и Laravel преобразует ответ в формате JSON автоматически.

О вашей конкретной проблеме: при запросе $posts в контроллере, добавьте with('comment') исполнение, например: $posts = Post::with('comment')->get(), после чего будут возвращены сообщения с предварительно запрограммированным комментарием. Это жадная загрузка Laravel в, Вы можете прочитать об этом здесь: https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

+0

я хочу сделать это с помощью AJAX Вот почему я пишу ответ() -> JSON ($ сообщений) это – leo