LUMEN-致命的呼叫NULL上的成员功能文件()



我正在尝试将值插入我的文件表中,但我会遇到错误。fatalthrowableerror调用null上的成员函数文件((。

intset.php模型

namespace App;
use IlluminateDatabaseEloquentModel;
class Inset extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'insets';
    public function file()
    {
        return $this->hasMany('AppFile');
    }
}

file.php模型

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class File extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'files';
    protected $fillable = ['file_name','file_content', 'insets_post'];
    public $timestamps = false;
    public function inpost()
    {
        return $this->belongsTo('AppInpost');
    }
}

filescontroller.php控制器

<?php
/**
 * Created by PhpStorm.
 * User: Kacper
 * Date: 28.02.2018
 * Time: 14:00
 */
namespace AppHttpControllers;
use AppFile;
use AppInset;
use IlluminateHttpRequest;
use IlluminateSupportFacadesCrypt;
class FilesController
{
    public function showAll(Request $request)
    {
        return File::all();
    }
    public function show($ID_inset)
    {
        return File::findOrFail($ID_inset);
    }
    public function insert(Request $request)
    {
        $inset = Inset::find($request->input('inset_id'));
        $file =$inset ->file()->create([
            'file_name' => $request->input('file_name'),
            'file_content' => Crypt::encrypt($request->input('file_content'))
        ]);
        $file->save();
    }
}

错误:

(1/1(fatalthrowableerror呼叫null

上的成员函数文件((

在filescontroller.php(第31行(中 filescontroller-> insert(object(request((at call_user_func_array(array(object(filescontroller(,'insert'(, boundmethod.php(第29行(中的数组(对象(request((( boundmethod :: Illuminate container {cline}((在boundmethod.php(line( 87(在boundMethod :: callboundmethod(object(application(, array(object(filescontroller(,'insert'(,对象(clos clos(( boundmethod.php(第31行(在boundmethod :: call(object(application(, array(object(filescontroller(,'insert'(,array((,null(in container.php(第564行( container-> call(array(object(filescontroller(,'insert'(,array(((,in rutesrequests.php(第373行( application-> callcontrollerCallable(array(object(filescontroller(, 'insert'(,array(((在artesrequests.php(第316行(中 application-> callControllerAction(array(true,array('uses'=>>( 'app http controllers filescontroller@insert'(,array(((( rutesrequests.php(第275行( application-> callactionOnarrayBasedRoute(array(true,array('use use'=>( 'app http controllers filescontroller@insert'(,array(((( rutesrequests.php(第260行( application-wandsfoundRoute(array(true,array('uses'=>>( 'app http controllers filescontroller@insert'(,array(((( rutesrequests.php(第160行( application-> laravel lumen consults {cline}(( (第413行(在应用程序 -> sendThroughPipeline(array((, artesrequests.php(第166行(中的对象(闭合(( Application-> rutesrequests.php(第107行(中的application-> dispatch(null(at Application-> run((in Index.php(第28行(

您的代码

$inset = Inset::find($request->input('inset_id'));

正在返回null,这就是为什么要抛出此错误。

如果要避免错误,则可以这样做,例如

$inset = Inset::find($request->input('inset_id'));
 if($inset){
    $file =$inset ->file()->create([
        'file_name' => $request->input('file_name'),
        'file_content' => Crypt::encrypt($request->input('file_content'))
    ]);
    $file->save();
 }

希望这有帮助

最新更新