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