2016-07-31 216 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'); 
} 
相关问题