php - YouTube Vid Attached - 在 Laravel 中删除 'posts' 表中的类别后,如何从 'categories' 表中删除关联的 Category_id?

标签 php html mysql laravel laravel-blade

问题半解决/缩小范围。阅读编辑/更新。

所以,我有一个博客应用程序...在该博客应用程序中我有两条路线... 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

我刚刚添加了一个新的评论迁移和 Controller ,并且我已将 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');
    }

}

这是我的博客 Controller 代码:

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

编辑/更新:

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

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

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

这是我的类别帖子模型...

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

一切看起来都不错,所以我直接使用 SQL Pro 进入数据库,查看帖子中哪些类别有效,以及帖子中哪些类别无效。这是由“posts”表中的“category_id”列定义的。

我注意到有效的帖子使用了类别 ID“2”。其他不起作用的帖子有一个“其他”“2”类别。所以我转到我的“类别”表,查看是否还有除 2 之外的类别。没有。

不知何故,在删除类别时,我并没有删除帖子表中的关联。我不知道如何在从类别表中删除类别后同时清除帖子表中对类别 ID 的所有引用。

这是我用于创建类别表的迁移函数

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

这是用于将category_id列添加到“posts”表的up函数。

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

这是类别 Controller 中类别的销毁函数。

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

最佳答案

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

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

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

解决这个问题的两种方法:

  1. 如果您想确保每个帖子都属于一个类别,请删除可为空的 OR
  2. {{ $post->category->name }} 更改为 {{ $post->category_id ? $post->类别->名称:""}}

至于如何去掉posts表中的关联,有2种选择:

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

关于php - YouTube Vid Attached - 在 Laravel 中删除 'posts' 表中的类别后,如何从 'categories' 表中删除关联的 Category_id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45785853/

相关文章:

PHP & mysql 显示来自多个表的数据

javascript - 将工厂与 ng 模块绑定(bind)是一种好习惯吗?

MySQL:整列的 MAX() *帮助*

php - 将空间数据插入MySQL数据库

PHP mysqli_connect : authentication method unknown to the client [caching_sha2_password]

php - 我想选择所有最小可用数量为零的产品名称

html - 适用于桌面而非移动设备的 Heroku App 的媒体查询(尽管尝试了多种解决方案)

java - Hibernate 在读取之前永久清除所有现有数据

php - 在 PHP 中创建 DAO 的正确方法

html - 如何在页面中间对齐列表(ul 和 Li)