2016-07-31 161 views
1

現在,我在我的頁面上顯示視頻,點擊它後,我將其重定向到/ video {video}/view(其中{video}是視頻ID)。Laravel在不同的頁面上顯示不同的帖子

@foreach($videos as $video) 
      <div class="col-sm-4 feature"> 
       <div class="panel"> 
        <div class="panel-heading"> 
         <h3 class="panel-title video_name">{{ $video->video_name }}</h3> 
        </div> 
        <div class="embed-responsive embed-responsive-16by9"> 
        <iframe width="320" height="250" class="embed-responsive-item" 
        src="https://www.youtube.com/embed/{{ $video->video_url }}" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen"> 
        </iframe> 
        </div> 
        <div class="info"> 
        <p>Posted by {{ $video->user->first_name }} on {{ $video->created_at }}</p> 
         <hr class="postInfo"> 
        </div> 

        <p>{{ $video->description }} </p> 
        <a href="{{ route('view.video', [$video->id]) }}" class="btn btn-danger btn-block">Continue to video</a> 
       </div> 
      </div> 
     @endforeach 

我已經在我的數據庫

Schema::create('posts', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->timestamps(); 
     $table->text('body'); 
     $table->integer('user_id'); 
     $table->integer('video_id'); 
    }); 

創造職位,但問題是我不知道我怎麼能顯示不同的影片不同的崗位,我越來越user_ID的,但我不能獲取video_id。

帖子創建功能:

public function postCreatePost(Request $request) { 
    $post = new Post(); 
    $post->body = $request['body']; 
    $request->user()->posts()->save($post); 
    return redirect()->route('dashboard'); 
} 

回答

0

我不得不給我的路線,我張貼,然後$ post-> video() - > associate($ video_id);與我的視頻ID。

0

在文檔看看https://laravel.com/docs/5.2/eloquent-relationships

事情你要創建被稱爲關係,我假定這將是一對一的視頻然後張貼,然後在用戶和視頻和帖子上一對多。

我不確定你是怎麼得到用戶的。但是,首先要做的是定義外鍵。

Schema::create('posts', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->timestamps(); 
    $table->text('body'); 
    $table->integer('user_id'); 
    $table->integer('video_id'); 

    $table->foreign('user_id') 
      ->references('id')->on('users'); 
    $table->foreign('video_id') 
      ->references('id')->on('videos'); 
}); 

您將在其他表上應用外鍵。無論如何,我假設這兩個相關的表格是usersvideos。接下來,應用這些關係。

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class User extends Model 
{ 
    /** 
    * Get the post associated by the user. 
    */ 
    public function posts() 
    { 
     return $this->hasMany('App\Post'); 
    } 
} 

再次,我假設你在App命名空間下。然後爲這個職位。

class Post extends Model 
{ 
    public function user() 
    { 
     return $this->belongsTo('App\User'); 
    } 

    public function video() 
    { 
     return $this->hasOne('App\Video'); 
    } 
} 

最後,視頻。

class Video extends Model 
{ 
    public function post() 
    { 
     return $this->belongsTo('App\Post'); 
    } 
} 

我重複我的第一個假設。 您可能會在郵件和視頻中進行一對一的對話,然後在用戶和帖子上進行一對一的對話。這意味着,用戶可以有很多帖子,這些帖子將會有一個視頻。

控制器,如果我的假設在這一點上是正確的,它可能看起來像這樣。

//You might want to use route model binding instead 
//@see https://laravel.com/docs/5.2/routing#route-model-binding 

public function showPost(Request $request, $post) { 
    $post = \App\Post::where('id', $post)->with('video')->get(); 

    return view('template.post', ['post' => $post]); 
} 

public function showVideo(Request $request, $video) { 
    $video = \App\Video::where('id', $video)->with('post')->get(); 

    return view('template.video', ['video' => $video]); 
} 

public function postCreatePost(Request $request) 
{ 
    $post = new Post(); 
    $post->body = $request['body']; 
    $request->user()->posts()->save($post); 

    $video = new Video(); 
    $video->id = $request['video']; 
    $post->video()->save($video); 

    return redirect()->route('dashboard'); 
} 
相關問題