【问题标题】:Call to undefined method Illuminate\Http\Request::angel.jpg()调用未定义的方法 Illuminate\Http\Request::angel.jpg()
【发布时间】:2016-11-18 02:32:13
【问题描述】:

我有一个表格,上传图片有问题:(

我正在尝试上传一些图片,但我不知道我做错了什么:(

 {{ Form::open (['route' => 'titulos.store', 'class'=> 'form', 'files'=> 'true']) }}

    {{ Form::label('title', "Titulo:", ['class' => 'col-sm-2 control-label']) }}

    {{ Form::text('title') }}
        {{ $errors->first('title') }}
            
    <div class="form-group">          
      {{ Form::label('date', "Fecha:", ['class' => 'col-sm-2 control-label']) }}
      <input type="date" name="date" >              
    </div>

    {{ Form::label('description', "Description:", ['class' => 'col-sm-2 control-label']) }}
        {{ Form::textarea('description') }}
        {{ $errors->first('description') }}
    
    <div class="form-group">
        {{ Form::file('image') }}
    </div>

        {{ Form::label('category_id', 'Category:', ['class' => 'col-sm-2 control-label']) }}
            <div class="col-sm-10">
             {{ Form::select('category_id', array('1' => 'TBLeaks', '2' => 'Quejas', '3' => 'Denuncias', '4' => 'Ideas'), null, array('class' => 'form-control')) }}
            </div>

        <div class="row">       
            <div class="col-sm-offset-2 col-sm-10">
        {{ Form::submit('Submit', ['class' => "btn btn-primary"]) }}
            </div>
        </div>
            

        <div class="row">       
            <div class="col-sm-offset-2 col-sm-10">
                <a class="btn btn-success" href="{{ URL::to('admin') }}">Back to Admin</a>
            </div>          
        </div>  
        {{ Form::close() }}
    </div>


@if(Session::has('message'))
    <div class="alert alert-{{ Session::get('class') }}">{{ Session::get('message')}}</div>
@endif  

@停止

在我的商店功能中,我有:

类 TitulosController 扩展 BaseController {

公共函数 store(){

    $rules = array(
        'title'         => 'required',
        'description'   => 'required',           
        'category_id'   => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);

    // proceso de valicion
    if ($validator->fails()) {
        return Redirect::to('titulos/create')
            ->withErrors($validator)
            ->withInput()->withErrors($validator);
    } else {


    //store
    $image = Input::file('image');
    $filename = $image->getClientOriginalName();
    
    if(Input::hasFile('image')){
        Input::file('image')->move(public_path().'/assets/img/', $filename);
    }
    
    $titulo = new Titulo();
    $titulo->id             = Input::get('id');
    $titulo->title          = Input::get('title');    
    $titulo->description    = Input::get('description');
    $titulo->date           = Input::get('date');    
    $titulo->image          = Input::$filename('image');
    $titulo->category_id    = Input::get('category_id');
    
    $titulo->save();

    return Redirect::to('titulos');        
}

我有这个 Titulo 表的模型:

类 Titulo 扩展 Eloquent {

use SoftDeletingTrait; // for soft delete

protected $dates = ['deleted_at']; // for soft delete   

protected $table = 'titulos';

protected $primaryKey = 'id';

protected $fillable = array('image');

public $timestamps = true;

public function __construct() {
    parent::__construct();
    
}

public function category(){
    return $this->belongsTo('Category');
}

}

我有这个用于图像模型:

类图像扩展 Eloquent {

public $timestamps = false;
protected $fillable = array('image');

}

【问题讨论】:

  • 你chmod权限文件夹assets/img/了吗?
  • 是的,我试图从 public/assets/img/angel.jpg 上传。谢谢 :) mydo47

标签: php laravel


【解决方案1】:

在您的商店到公共路径的这一行应该没问题

if(Input::hasFile('image')){
    Input::file('image')->move(public_path().'/assets/img/', $filename);
}

只有你保存的 Tutilo 对象看起来不太好。我认为您在这一行错误地在Input::filename 上添加了$。这将调用图像

$titulo->image = Input::$filename('image');

把你的代码改成这个

$titulo = new Titulo();
$titulo->id             = Input::get('id');
$titulo->title          = Input::get('title');    
$titulo->description    = Input::get('description');
$titulo->date           = Input::get('date');    
$titulo->image          = $filename; // I change this line. assuming you want to store the file name
$titulo->category_id    = Input::get('category_id');

【讨论】:

  • 我在输入中加了$,错误改为:函数名必须是字符串 in:
    $titulo-&gt;image = Input::$file('image');我是这个Laravel世界的新手, 在此先感谢 @cod3rite :D – Chiki 6 分钟前
  • 我的意思是,去掉那个 $ 让它变成Input::file
  • 非常感谢cod3rite我做错了:(,我的商店功能是:
  • 请看这个链接,我上传完整的商店功能"http://www.hiii.com/img"
  • 天啊!!!!你是精灵!!!!非常感谢!!!你不知道我是如何受苦的。再次感谢新西兰 xoxoxoxo :D
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 2020-06-20
  • 2016-03-14
  • 2018-07-04
  • 2021-01-15
相关资源
最近更新 更多