我正在尝试将值插入到我的文件表中,但是收到错误。 FatalThrowableError在null上调用成员函数file()。
Inset.php模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Inset extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'insets';
public function file()
{
return $this->hasMany('App\File');
}
}
File.php模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
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('App\Inpost');
}
}
FilesController.php控制器
<?php
/**
* Created by PhpStorm.
* User: Kacper
* Date: 28.02.2018
* Time: 14:00
*/
namespace App\Http\Controllers;
use App\File;
use App\Inset;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
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
上调用成员函数file()在FilesController.php(第31行)中 FilesController-&gt; insert(object(Request))at call_user_func_array(array(object(FilesController),&#39; insert&#39;), array(object(Request)))在BoundMethod.php(第29行)中 BoundMethod :: Illuminate :: Container {closure}()在BoundMethod.php中(行 87)在BoundMethod :: callBoundMethod(object(Application), 数组(对象(FilesController),&#39;插入&#39;),对象(Closure)) BoundMethod ::ph的BoundMethod.php(第31行)调用(对象(应用程序), array(object(FilesController),&#39; insert&#39;),array(),null)in Container.php(第564行)at Container-&gt; call(array(object(FilesController),&#39; insert&#39;),array())in RoutesRequests.php(第373行)at 应用 - &GT; callControllerCallable(阵列(对象(FilesController), &#39;在RoutesRequests.php(第316行)中插入&#39;),array()) Application-&gt; callControllerAction(array(true,array)&#39;使用&#39; =&gt; &#39; App \ Http \ Controllers \ FilesController @ insert&#39;),array())in in RoutesRequests.php(第275行)at Application-&gt; callActionOnArrayBasedRoute(array(true,array)&#39;使用&#39; =&gt; &#39; App \ Http \ Controllers \ FilesController @ insert&#39;),array())in in RoutesRequests.php(第260行)at Application-&gt; handleFoundRoute(array(true,array)&#39;使用&#39; =&gt; &#39; App \ Http \ Controllers \ FilesController @ insert&#39;),array())in in RoutesRequests.php(第160行)at Application-&gt; RoutesRequests.php中的Laravel \ Lumen \ Concerns {closure}() (第413行)在Application-&gt; sendThroughPipeline(array(), 对象(Closure))在RoutesRequests.php(第166行)中 Application-&gt;在RoutesRequests.php(第107行)中发送(null) index.php中的application-&gt; run()(第28行)
答案 0 :(得分:0)
您的代码
$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();
}
希望这有帮助