G
G
Gevorg11222020-02-20 20:48:25
PHP
Gevorg1122, 2020-02-20 20:48:25

Laravel - Call to a member function hashName() on null?

When adding a record to the database, it is not possible to save the selected file from the form and get a link for writing to the database, what could be the problem?

route:

Route::post('/post', '[email protected]')->name('store');

controller
public function store(Request $request)
    {
$post = new Post();
        $post->title = $request->title;
        $post->short_title = Str::length($request->title)>30 ? Str::substr($request->title, 0, 30) . '...' : $request->title;
        $post->descr = $request->descr;
        $post->author_id = rand(1,4);
            $path = Storage::putFile('public', $request->file('img'));
              $url = Storage::url($path);
              $post->img = $url;
             $post->save();
            return redirect()->route('home');
}

The form:
<form method="post" action="{{ route('store') }}">
    @csrf
    <div class="form-group"><input class="form-control" name="title" type="text"></div>
    <div class="form-group"><textarea class="form-control" name="descr" type="text"></textarea></div>
    <div class="form-group">
      <input class="form-control" name="img" type="file">
    </div>
    <input type="submit" value="Создать пост" class="btn btn-primary" name="" id="">
  </form>


Gives the following error:
Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function hashName() on null

C:\Users\User\Desktop\open server\OSPanel\domains\blog\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php:244


php artisan storage:linkmade

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Kulaxyz, 2020-02-21
@Gevorg1122

Must be added to the form tag enctype="multipart/form-data"
Read more here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question