php - YouTube Vid Attached - How do you remove an associated category_id from 'posts' table upon deletion of category in 'categories' table in Laravel? -
question semi-solved/narrowed down. read edit/update.
so, have blog app... in blog app have 2 routes... blog.index
, blog.single
. index lists blog posts , links blog.single
view , passes in slug identify specific post. i'm attaching youtube video of problem me narrating... i've never come across issue before.
basically if click on "read more" button on index page, posts displayed correctly on single page... posts giving me following error.
trying property of non-object (view: /home/vagrant/sites/blog/resources/views/blog/single.blade.php)
here code 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
i added new comments migration , controller, , have edited post model this:
namespace app; use illuminate\database\eloquent\model; class post extends model { // access category model public function category() { return $this->belongsto('app\category'); } // access tags model public function tags() { return $this->belongstomany('app\tag'); } // access comments model public function comments() { return $this->hasmany('app\comment'); } }
and here blog controller code here:
use illuminate\http\request; use app\post; class blogcontroller extends controller { // index of blog posts public function getindex() { // grab of posts db $posts = post::orderby('created_at', 'desc')->paginate(10); // return index view return view('blog.index')->withposts($posts); } // single post public function getsingle($slug) { // grab post via slug $post = post::where('slug', '=', $slug)->first(); // return via pass in post object return view('blog.single')->withpost($post); } }
in youtube video give brief overview of database, , code. 6 minutes long.
youtube video here: https://youtu.be/f78qzqq4rts
youtube tutorials i'm following here: https://www.youtube.com/watch?v=z-kydpg8j34&index=47&list=plwakr305cro-q90j---jxvzbod4cdrbvx
edit/update:
so i've narrowed down issue line of code... works , again doesn't...
<p>posted in: {{ $post->category->name }}</p>
so, thought myself, lets take @ post model see if category defined correctly can access it.
here post model category...
// access category model public function category() { return $this->belongsto('app\category'); }
everything looked good, , went directly database using sql pro see category on posts work, , categories on post dont work. defined 'category_id' column in 'posts' table.
i noticed posts worked used category id '2'. other posts didn't work, had category other '2'. went 'categories' table, , looked see if there category other 2. there wasn't.
somehow long way, when deleting category, wasn't removing association in posts table. i'm not sure how upon deletion of category categories table wipe clean references category id in posts table.
here migration function categories table creation
public function up() { schema::create('categories', function (blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); }
here function adding category_id column 'posts' table.
schema::table('posts', function (blueprint $table) { // add category id posts table $table->integer('category_id')->nullable()->after('slug')->unsigned(); });
and here destroy function category in category controller.
public function destroy($id) { $category = category::find($id); $category->delete(); session::flash('success', 'deleted category'); return redirect()->route('categories.index'); }
so, information should able help... how rid of association in posts table upon deletion of category in categories table?
here youtube video explaining narrowed down question:
$table->integer('category_id')->nullable()->after('slug')->unsigned();
this migration suggests post may or may not belong category since category_id nullable field.
so {{ $post->category->name }}
won't work because may posts don't belong category.
two ways solve this:
- remove nullable if want make sure every post belongs category or
- alter
{{ $post->category->name }}
{{ $post->category_id ? $post->category->name : "" }}
as how rid of association in posts table, there 2 options:
- if want delete posts related category when category deleted, use
$category->posts()->delete();
- if want dissociate posts, use
$category->posts()->update(['category_id' => null]);
Comments
Post a Comment