2016-07-29 71 views
1

因此,首先我開始使用laravel和php的整體工作。現在我遇到了一個問題,我不知道如何從我的數據庫中顯示特定的視頻。我的用戶模型:Laravel如何通過它的ID顯示視頻

class User extends Model implements Authenticatable{ 
    use \Illuminate\Auth\Authenticatable; 
    public function videos() { 
     return $this->hasMany('App\Video'); 
    } 
} 

我的視頻模式:

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

一切順利的話,當我依次通過我的影片在儀表板顯示它們:

<div class="row" id="features"> 
     @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> 
        <iframe width="320" height="250" 
        src="https://www.youtube.com/embed/{{ $video->video_url }}" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen"> 
        </iframe> 
        <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 
    </div> 

但在這一點:

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

I ope N個新的路線是(http://localhost:8000/video/11/view),在這種情況下,我想其中ID 11等於我VIDEO_URL

視頻表的代碼顯示視頻:

public function up(){ 
     Schema::create('videos', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->timestamps(); 
      $table->text('video_name'); 
      $table->text('video_url'); 
      $table->text('description'); 
      $table->integer('user_id'); 
     }); 
} 

路線:

Route::get('/video/{video_id}/view', [ 
    'uses' => '[email protected]', 
    'as' => 'view.video']); 

回答

0

變化以下路線..

Route::get('/video/{video}/view', [ 
'uses' => '[email protected]', 
    'as' => 'view.video']); 

在控制器..

public function ViewVideo(Video $video){ 
    //any authorization logic... 
    return view('whatever',compact('video')); 
} 
+1

它的工作<3這樣一個簡單的解決方案,謝謝了很多! :) – Arthur

0

由於Laravel 5.2的有一種叫做Implicit Route Model Binding你可以閱讀一下here的文檔的事情。

所以在你的例子中。你可以改變你的路線是這樣的:

Route::get('/video/{video}/view', [ 
    'uses' => '[email protected]', 
    'as' => 'view.video' 
]); 

而在你Video控制器的視圖方法:

public functin view(App\Video $video) { 
    // Your logic 
} 
+0

謝謝,已經解決了我的問題:) – Arthur