【问题标题】:Call to a member function store() on null (PostsController)在 null (PostsController) 上调用成员函数 store()
【发布时间】:2020-09-08 04:11:44
【问题描述】:

我正在尝试将一些元素和带有表单的照片保存在数据库中,我在表单上添加了“enctype="multipart/form-data”,并执行了命令“php artisan storage:链接”,但是当我单击“上传”按钮时,Laravel 向我返回此错误:文件 PostsController 上的“调用成员函数 store() on null”

这是我的文件 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'));
}
}

这是我的视图文件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>

这是我在 web.php 中的路线

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

这是我的模型 Post.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    protected $table = 'posts';
}

【问题讨论】:

  • 看起来$req 对象没有img。你能在控制器中做一个$req-&gt;all() 并在这里发布内容吗?

标签: php laravel forms upload


【解决方案1】:

正如您在the documentation 中看到的,可以使用file() 方法访问上传的文件。

尝试更新这个:

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

到这里:

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

另外,您可以确认请求是否确实在其有效负载中包含该文件,检查该文件是否存在,以避免异常:

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

这个以及更多有用的方法,可以在文档的File Uploads 部分看到。

【讨论】:

    猜你喜欢
    • 2020-10-28
    • 2017-04-08
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2021-04-25
    相关资源
    最近更新 更多