2017-08-20 16 views
0

问题半解决/缩小。阅读编辑/更新。YouTube Vid附加 - 在删除Laravel的“类别”表中的类别后,如何从“posts”表中删除关联的category_id?

所以,我有一个博客应用程序...在该博客应用程序中,我有两条路线...... blog.indexblog.single。该索引列出博客文章和blog.single视图的链接,并通过slug来标识特定帖子。我附上了一个关于这个问题的YouTube视频,正在叙述......我以前从来没有遇到过这个问题。

基本上,如果我点击索引页面上的“阅读更多”按钮,只有一些帖子在单个页面上正确显示......但是有些帖子给了我以下错误。

Trying to get property of non-object (View: /home/vagrant/sites/blog/resources/views/blog/single.blade.php)

这里是我的single.blade.php代码:

@extends('main') 

@section('title', $post->title) 

@section('content') 

    <div class="row"> 
     <div class="col-md-8 col-md-offset-2"> 
      <h1>{{ $post->title }}</h1> 
      <h5>Published: {{ date('M j, Y', strtotime($post->created_at)) }}</h5> 
      <p>{{ $post->body }}</p> 
      <hr> 
      <p>Posted In: {{ $post->category->name }}</p> 
     </div> 
    </div> 

@endsection 

我只是增加了一个新的评论迁移和控制器,并且我已经编辑了帖子模型这一点:

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Post extends Model 
{ 
    // Access to category model 
    public function category() { 
     return $this->belongsTo('App\Category'); 
    } 

    // Access to Tags Model 
    public function tags() { 
     return $this->belongsToMany('App\Tag'); 
    } 

    // Access to Comments Model 
    public function comments() { 
     return $this->hasMany('App\Comment'); 
    } 

} 

这里是我的博客控制器代码在这里:

use Illuminate\Http\Request; 

use App\Post; 

class BlogController extends Controller 
{ 

    // Get the Index of Blog Posts 
    public function getIndex() { 
     // Grab all of the posts from the DB 
     $posts = Post::orderBy('created_at', 'desc')->paginate(10); 

     // Return the Index View 
     return view('blog.index')->withPosts($posts); 
    } 


    // Get Single Post 
    public function getSingle($slug) { 
     // Grab the post via slug 
     $post = Post::where('slug', '=', $slug)->first(); 

     // Return the Via Pass in Post Object 
     return view('blog.single')->withPost($post); 
    } 

} 

在我的YouTube视频中,我给你简要介绍了数据库和代码。它大约6分钟长。

的Youtube视频在这里:https://youtu.be/F78QzqQ4rts

的Youtube教程我在这里如下:https://www.youtube.com/watch?v=z-KyDpG8j34&index=47&list=PLwAKR305CRO-Q90J---jXVzbOd4CDRbVx

编辑/ UPDATE:

所以,我的问题缩小到这行代码...有时工作,然后再有时不...

<p>Posted In: {{ $post->category->name }}</p>

所以,我想我自己,让我们看看邮政模型,看看类别是否正确定义,以便我们可以访问它。

这里是我的岗位模型类别...

// Access to category model 
public function category() { 
    return $this->belongsTo('App\Category'); 
} 

一切都看起来很不错,所以我使用SQL临看到的是对工作的职位是什么类别就直接到数据库,什么类别在不工作的帖子上。这是由'posts'表中的'category_id'列定义的。

我注意到工作的帖子使用了类别ID'2'。其他职位没有工作,有一个类别其他'2'。所以我去了我的'类别'表,看看是否有一个类别,然后是2.没有。

不知何故,在删除类别时,我没有删除posts表中的关联。我不知道如何删除类别表中的类别,也可以清除所有对帖子表中类别标识的引用。

这里是该类别表创建

public function up() 
{ 
    Schema::create('categories', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->timestamps(); 
    }); 
} 

这里我移民了功能是用于将CATEGORY_ID列到“信息”表中的最高性能。

Schema::table('posts', function (Blueprint $table) { 
     // Add category id to posts table 
     $table->integer('category_id')->nullable()->after('slug')->unsigned(); 
    }); 

这里是我的类别控制器中的类别的销毁功能。

public function destroy($id) 
{ 
    $category = Category::find($id); 

    $category->delete(); 

    Session::flash('success', 'Deleted the Category'); 

    return redirect()->route('categories.index'); 
} 

所以,有了这些信息,你应该能够帮助...我如何在帖子表格后在类别表中的类别删除掉的关联?

这里是一个YouTube视频解释收窄的问题:

https://www.youtube.com/watch?v=LGP14VH6miY

回答

1
$table->integer('category_id')->nullable()->after('slug')->unsigned(); 

这种迁移意味着你的职位可能会或可能不会因为CATEGORY_ID是一个可空场属于类别。

因此{{ $post->category->name }}不会总是工作,因为他们可能是某些不属于任何类别的帖子。

两种方式来解决这个问题:

  1. 如果你想确保每一个岗位属于一个类别或
  2. 阿尔特{{ $post->category->name }}{{ $post->category_id ? $post->category->name : "" }}

至于如何得到删除可空摆脱在帖子表中的关联,有2个选项:

  1. 如果你想删除有关时,该类别中删除一个类别的所有讯息,使用$category->posts()->delete();
  2. 如果你只想分离的职位,只需使用$category->posts()->update(['category_id' => null]);
+0

是不是有只是一个简单的功能有点像分离()在职位表中分离所述类别时,我可以使用,而不是更新的?这样我就不必将它传递给一个数组,它只知道哪个是null? detach()仅适用于manyToMany关系吗? –

+0

如果您想观看,则可以更好地解释我的问题,并增加了YouTube视频。 –

+0

是啊分离不工作在一对多的关系 – Paras

0

哇!所以经过一个小时的调试,我解决了这个问题。请参考这个youtube视频解决方案。

在我的代码中出现问题,因为我没有使用三元运算符,但我修复了它!享受视频和解决方案!

https://www.youtube.com/watch?v=YMEl5wkdQqw