0

I'm trying to publish a post with register user in my laravel project But this Error happened I don't Know why please help me.

enter image description here and my PostController is:

 public function __construct()
  {
    $this->middleware('auth')->except(['index','show']);
  }

public function index()
{
    $posts=Post::latest()->get();

    return view('posts.index',compact('posts'));
}

public function show(Post $post)
{

    return view('posts.show',compact('post'));
}

public function create()
{
    return view('posts.create');
}

public function store()
{

   $this->validate(request(),[
       'title'=>'required',
       'body' => 'required|min:15'
   ]);
   Post::create(request([
        'title' => request('title'),
        'body' => request('body'),
        'user_id' =>auth()->id()
        //auth()->user()->id
    ]));

    return redirect('/');
 }

How Can I fix it?

9
  • Please show full error screenshot, which would make some sense. Commented Apr 18, 2017 at 16:29
  • What function have an error? Commented Apr 18, 2017 at 16:34
  • @PandhiBhaumik Ok! I edite my question Commented Apr 18, 2017 at 16:35
  • @GeninaAnneGabuten I shared full screenshot of error please check it. Commented Apr 18, 2017 at 16:39
  • in store method i think.. you should have public function store(Request $request) Commented Apr 18, 2017 at 16:42

2 Answers 2

1

Try something like this.

public function store(Request $request)
{
    $model= new Model;
    $this->validate($request,[
        'title'=>'required',
        'body' => 'required|min:15'
    ]);

    $model->title= $request->title;
    $model->body= $request->body;
    $model->user_id= auth()->id();
    $model->save();  
    return redirect('/');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code

public function store(Request $request)
{

   $this->validate(request(),[
       'title'=>'required',
       'body' => 'required|min:15'
   ]);
   Post::create([
        'title' => $request->title,
        'body' => $request->body,
        'user_id' =>auth()->id()
        //auth()->user()->id
    ]);
return redirect('/');
}

of course my code might not work properly but if you could provide me with create method details in post model i can help better

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.