You say "Try the second step, passing to the view as I gave it to you" do you mean "{!! Form::select('category_id', [ 1 => 'Testing' ], null, ['class' => 'form-control']) !!}"?
If so I'm currently doing and Here's what I have... https://i.stack.imgur.com/L41l1.jpg
I'm very new at Laravel, so I'm not sure how to "debug the array that you get from the categories"?
right now this is what I have in PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Post;
use App\Category;
use Session;
class PostsController extends Controller
{
public function __construct(){
$this->middleware('auth')->except(['category', 'show']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
protected $limit = 3;
public function index()
{
$posts = Post::with('author')
->latestFirst()
->published()
->paginate($this->limit);
return view('posts.index', compact('posts'));
}
public function category(Category $category)
{
$categoryName = $category->title;
$posts = $category->posts()
->with('author')
->latestFirst()
->published()
->paginate($this->limit);
return view('posts.index', compact('posts', 'categoryName'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories = Category::all();
return view('posts.create')->withCategories($categories);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'category_id' => 'required|integer',
'body' => 'required'
));
//create Post
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->category_id = $request->category_id;
$post->body = $request->body;
$post->author_id = auth()->user()->id;
$post->save();
return redirect('/posts')->with('success', 'Your post created created successfully');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
//$post = Post::find($id);
// Check for correct user
/*if(auth()->user()->id !==$post->author_id){
return redirect('/posts')->with('error', 'Unauthorized Page');
}
$post = Post::where('id', $post)
->orWhere('slug', $post)
->firstOrFail();*/
$categories = Category::all()->pluck('title', 'id');
return view('posts.edit', compact('post', 'categories'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
/*$updated = Category::findorFail($id);
$categories = $request->all();
$category_id = $request->get('category_id');
$updated->fill($categories)->save();
return redirect('/dashboard')->with('success', 'Your post created updated successfully');*/
$this->validate($request, [
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255',
'body' => 'required'
]);
//create Post
$post = Post::find($id);
$post->title = $request->input('title');
$post->slug = $request->input('slug');
$post->category_id = $request->input('category_id');
$post->body = $request->input('body');
$post->author_id = auth()->user()->id;
$post->save();
return redirect('/dashboard')->with('success', 'Your post created updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$posts = Post::with('author');
// Check for correct user
if(auth()->user()->id !==$post->author_id){
return redirect('/posts')->with('error', 'Unauthorized Page');
}
$post->delete();
return redirect('/dashboard')->with('success', 'Your post has been "Delete" successfully');
}
}
Were do I use the "dd($categories);" that you give me?