附加的 YouTube 视频 - 如何在删除 'categories' table 中的 Laravel 中的类别后从 'posts' table 中删除关联的 category_id?

YouTube Vid Attached - How do you remove an associated category_id from 'posts' table upon deletion of category in 'categories' table in Laravel?

问题semi-solved/narrowed下来。阅读 EDIT/UPDATE.

所以,我有一个博客应用程序...在那个博客应用程序中我有两条路线...blog.indexblog.single。索引列出了博客 post 和指向 blog.single 视图的链接,并传入 slug 以识别特定的 post。我附上了关于问题的 YouTube 视频,我在叙述...我以前从未遇到过这个问题。

基本上,如果我单击索引页上的 "read more" 按钮,只有一些 post 会在单个页面上正确显示...但是有些 post 会给出我出现以下错误。

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

我刚刚添加了一个新的评论迁移和控制器,我已经将 Post 模型编辑为:

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

EDIT/UPDATE:

所以我将问题缩小到这行代码...有时有效,有时又无效...

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

所以,我心想,让我们看一下 Post 模型,看看是否正确定义了类别,以便我们可以访问它。

这是我的 Post 类别模型...

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

一切看起来都很好,所以我使用 SQL Pro 直接进入数据库,查看 post 上的哪些类别有效,以及 [=89= 上的哪些类别] 那行不通。这是由 'posts' table 中的 'category_id' 列定义的。

我注意到有效的 posts 使用了类别 ID“2”。其他 post 不起作用,类别为 OTHER THEN '2'。所以我去了我的 'categories' table,看看是否还有 2 以外的类别。没有。

不知何故,在删除类别时,我并没有删除 post 中的关联 table。我不确定如何在从类别 table 中删除类别时也清除 posts table.

中对类别 ID 的所有引用

这是我为类别 table 创建的向上迁移函数

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

这是将 category_id 列添加到 'posts' table 的 up 函数。

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

因此,有了这些信息,您应该能够帮助...在删除类别 table?

这是一个解释缩小问题的 YouTube 视频:

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

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

此迁移表明您的 post 可能属于也可能不属于类别,因为 category_id 是一个可为空的字段。

所以 {{ $post->category->name }} 并不总是有效,因为它们可能是某些 post 不属于任何类别的。

两种解决方法:

  1. 如果要确保每个 post 都属于某个类别,请删除 nullable 或
  2. {{ $post->category->name }}更改为{{ $post->category_id ? $post->category->name : "" }}

如何去掉post中的关联table,有2种选择:

  1. 如果要在删除类别时删除与该类别相关的所有post,请使用$category->posts()->delete();
  2. 如果你只想分离 posts,只需使用 $category->posts()->update(['category_id' => null]);

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

运行 成为我代码中的一个问题,因为我没有使用三元运算符,但我修复了它!欣赏视频和解决方案!

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