Paolo Privitera
Paolo Privitera

Reputation: 91

Call to a member function store() on null (PostsController)

I'm trying to save in database some elements and a photo with a form, I've added the "enctype="multipart/form-data" on the form and I've executed the command "php artisan storage:link", but when I click on Upload button, Laravel returns me this error: "Call to a member function store() on null" on file PostsController

Here is my file PostsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Post;

class PostsController extends Controller
{
public function submitpost(Request $req)
{ 
    $posttitle = $req->input('posttitle');
    $postauthor = $req->input('postauthor');
    $postcontent = $req->input('postcontent');
    $img = $req->input('img')->store('public/img');

    $dati = compact('posttitle', 'postauthor', 'postcontent', 'img');

    $b = new Post();
    $b->posttitle = $posttitle;
    $b->postauthor = $postauthor;
    $b->postcontent = $postcontent;
    $b->img = $img;

    $b->save();

    $posts = Post::all();
    return view('blog', compact('posts'));
}
}

This is my view file addpost.blade.php

<main class="main-content">
<div class="container-fluid photos">
  <div class="row justify-content-center">

    <div class="col-md-6 pt-4"  data-aos="fade-up">
      <h2 class="text-white mb-4">Create a new post</h2>

      <div class="row">
        <div class="col-md-12">


          <div class="row">
            <div class="col-md-12">



              <form action="{{route('submitpost')}}" enctype="multipart/form-data" method="post">
                @csrf

                <div class="row form-group">
                  <div class="col-md-12">
                    <label class="text-white" for="subject">Post Title</label> 
                    <input type="subject" name="posttitle" id="subject" placeholder="Give a title to the post" class="form-control">
                  </div>
                </div>

                <div class="row form-group">
                  <div class="col-md-12">
                    <input type="subject" readonly hidden name="postauthor" id="subject" value="{{Auth::user()->name}}" class="form-control">
                  </div>
                </div>

                <div class="row form-group mb-5">
                  <div class="col-md-12">
                    <label class="text-white" for="message">Content of post</label> 
                    <textarea name="postcontent" id="message" cols="30" rows="7" class="form-control" placeholder="Write your post here"></textarea>
                  </div>
                </div>

                <div class="row form-group">

                  <div class="col-md-12">
                  <input type="file" name="img" value="{{old('img')}}" placeholder="Image" value="">
                  </div>
                </div>

                <div class="row form-group">
                  <div class="col-md-12">
                    <input type="submit" value="Create Post" class="btn btn-primary btn-md text-white">
                  </div>
                </div>


              </form>
            </div>

          </div>
        </div>
      </div>
    </div>

  </div>
  </div>
  </div>
 </main>

This is my Route in web.php

Route::post('/addpost/submitpost', 'PostsController@submitpost')->name('submitpost');

And this is my model Post.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
protected $fillable = [
    'posttitle',
    'postauthor',
    'postcontent',
    'img',
    ];

    protected $table = 'posts';
}

Upvotes: 2

Views: 855

Answers (1)

Kenny Horna
Kenny Horna

Reputation: 14251

As you can see in the documentation, uploaded files can be accessed using the file() method.

Try updating this:

$img = $req->input('img')->store('public/img');

To this:

$path = $req->file('img')->store('public/img');
              ^^^^^^^^^^

Also, you can confirm if the request actually has the file in its payload checking if the file exists, to avoid exceptions:

if ($req->hasFile('img')) {
    $path = $req->file('img')->store('public/img');
}

This and more useful methods, can be seen in the File Uploads section of the docs.

Upvotes: 1

Related Questions