2016-11-10 1 views
0

c'est table posts:récupérer des données à partir de plusieurs tables à l'aide 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'); 
    } 
} 

c'est table commentaires:

<?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'); 
    } 
} 

ce modèle post

<?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'); 
    } 


} 

c'est modèle Commentaire

<?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'); 
    } 

} 

Dans la page de la lame que je peux easilly récupérer tous les commentaires d'un poste particulier, comme: suppose que je suis arrivé messages $ comme poste

@foreach ($post->comment as $comment) 
{{$comment->comment}} 

mais en ajax comment pourrais-je faites ceci supposons que je retourne la réponse() -> json ($ posts); toute suggestion? Il me aider beaucoup

+0

inclure votre code javascript – Beginner

Répondre

0

Vous ne devez pas écrire response()->json($posts), vous pouvez simplement return $posts et Laravel convertira réponse à JSON automatiquement.

À propos de votre problème exact: Lorsque vous interrogez $posts dans le contrôleur, ajouter with('comment') exécution, comme: $posts = Post::with('comment')->get() il sera alors revenir messages avec un commentaire préchargées. Il est chargement désireux de Laravel, vous pouvez en lire davantage ici: https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

+0

que je veux faire à l'aide ajax est pourquoi j'écrire la réponse() -> JSON (messages $) cette – leo