本文介绍了未定义变量:用户laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试进入上传页面时,都会不断出现此错误.

I keep on getting this error whenever I try to enter the upload page.

有人可以帮忙吗?

我已经完成了紧凑的部分,以确保将变量传递给视图,而且我认为我的路线应该没问题.

I have already done the compact part to make sure that the variable is being passed to the view and also my route should be ok I think.

我尝试使用dd,但它所做的只是继续向我显示错误

I tried using dd but all it does is keep on showing me the error

错误:未定义的变量:用户(视图:C:\ xampp \ htdocs \ Evaluation \ resources \ views \ upload.blade.php)

Error: Undefined variable: user (View: C:\xampp\htdocs\Evaluation\resources\views\upload.blade.php)

这是我的代码:

upload.blade.php

upload.blade.php

<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">

    {{  csrf_field()  }}

    <input type="hidden" name="user_id" value="{{$user->id}}">
    <div class="form-group">
        <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
        <div class="col-sm-9">
            <input type="file" name="file">

        </div>
    </div>

    <div class="form-group">
        <div class="col-md-6-offset-2">
            <input type="submit" class="btn btn-primary" value="Save">
        </div>
    </div>
</form>

UploadController:

UploadController:

public function upload(){

    return view(‘upload’);
}

public function store(Request $request,$id){

    $this->validate($request, [
        'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

   var_dump('has file '.$request->hasFile('file'));
    if ($request->hasFile('file')) {
        $image = $request->file('file');
        $name = $image->getClientOriginalName();
        $size = $image->getClientSize();
        $id = $request->user_id;
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

        $Image = new Image;
        $Image->name = $name;
        $Image->size = $size;
       //  $Image->user_id = $id;
        //$Image->save();
             $user->find($id);
               dd($user);
       $user->Images()->save($Image);
    }
    return redirect('/home');
}

public function test(){
    $user = user_information::where('id')->get();
    return view('upload', compact('user'));
}

路线:(这是我的路线)

Route: (this are my route)

Route::get('/UploadUser/upload','UploadController@upload’);
Route::post('/UploadUser','UploadController@store');
Route::post('/UploadUser/upload', 'UploadController@test');

另一个问题:尝试上传文件时,我一直遇到此错误,该怎么办?

Another question: I keep on getting this error when i try to upload a file, so what should I do?

这是错误:

图片模型:

class Image extends Model
{

   protected $fillable = array('name','size','user_id');

    public function user_informations() {
        return $this->belongsTo('App\user_information', 'user_id', 'id');
    }
}

图片表格:

   Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('size');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('user_informations');
        $table->timestamps();
    });

用户信息表:

 Schema::create('user_informations', function (Blueprint $table) {
            $table->increments('id');
             $table->engine = 'InnoDB';
            $table->binary('signature');
            $table->String('Name');
            $table->timestamps();
        });

用户信息模型:

class user_information extends Eloquent
{
    protected $fillable = array('signature', 'Name');
    protected $table = 'user_informations';
    protected $primaryKey = 'id';
        public function Images() {
        return $this->hasOne('App\Image','user_id');
    }
}

如何获取图像?这是查看文件夹:

How to get the image?Here is the view folder:

@foreach ($data as $object)
    <b>Name: </b>{{ $object->Name }}<br><br>
 <a href="{{ url('/user/show/'.$object->id.'/edit') }}">Edit</a><br>
    @foreach ($data3 as $currentUser)
    <img src="{{ asset('public/images/' . $currentUser->Image->name ) }}">
    @endforeach
  @if($data3->count())
 @foreach($data3 as $currentUser)
<a href="{!! route('user.upload.image', ['id'=>$currentUser->user_id])  !!}">
    <button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
</a>

@endforeach
@else
 <a href="{!! route('user.upload.image', ['id'=>$object->id])  !!}">
    <button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
@endif
 @endforeach

HomeController:

HomeController:

        public function getInfo($id) {
$data = user_information::where('id',$id)->get();
          $data3=Image::where('user_id',$id)->get();
     return view('test',compact('data','data3'));

推荐答案

由于您没有将用户传递到您的上传视图中,因此请尝试通过以下方式传递它:

Because you didn't pass the user to your upload view, try to pass it like this :

public function upload(){
    $id = 1  //The wanted user or if the user is authenticated use Auth::id()
    $user = User::find($id);
    return view('upload')->withUser($user);
}

或者如果用户通过了身份验证,请在视图中使用Auth:

Or if the user is authenticated use Auth in the view :

<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">

    {{  csrf_field()  }}

    <input type="hidden" name="user_id" value="{{auth()->id()}}">
    <div class="form-group">
        <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
        <div class="col-sm-9">
            <input type="file" name="file">

        </div>
    </div>

    <div class="form-group">
        <div class="col-md-6-offset-2">
            <input type="submit" class="btn btn-primary" value="Save">
        </div>
    </div>
</form>


第二个问题是因为您在路线中


For the second problem it's because in the route you have

Route::post('/UploadUser','UploadController@store');

,您的商店方法签名为

public function store(Request $request,$id){

发生问题的$id参数,因为它未在路由中定义,因此只需将其从方法signatre中删除

The $id parameter that did the problem because it's not defined in the route so simply remove it from the method signatre

public function store(Request $request){

    $this->validate($request, [
        'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);


    if ($request->hasFile('file')) {
        $image = $request->file('file');
        $name = $image->getClientOriginalName();
        $size = $image->getClientSize();
        $id = $request->user_id;  // here id is declared no need for the parameter
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

        $Image = new Image;
        $Image->name = $name;
        $Image->size = $size;
         $Image->user_id = $id;
        $Image->save();
    }
    return redirect('/home');
}


对于第三种情况,您必须从更改路线:


For the third case you have to change the routes from :

Route::get('/UploadUser/upload','UploadController@upload’);

Route::get('/UploadUser/{user}/upload','UploadController@upload’)->name('user.upload.image');

然后在视图中,将id添加到上载按钮的网址中:

And in the view add the id in the upload button url maybe like this :

{!! route('user.upload.image', ['user'=>$currentUser->id])  !!}

然后使用上传方法:

public function upload(user_information $user){ // route model binding here
    // dd($user);  //for testing only :)
    return view('upload')->withUser($user);
}

在视图中更改

<input type="hidden" name="user_id" value="{{auth()->id()}}">

收件人

<input type="hidden" name="user_id" value="{{$user->id()}}">

您很高兴;)

@foreach ($data as $currentUser)
    <b>Name: </b>{{ $currentUser->Name }}<br><br>
    <a href="{{ url('/user/show/'.$currentUser->id.'/edit') }}">Edit</a><br>

    @if($currentUser->Image)
        <img src="{{ asset('public/images/' . $currentUser->Image->name ) }}">
    @endif

    <a href="{!! route('user.upload.image', ['id'=>$currentUser->id])  !!}">

@endforeach

这篇关于未定义变量:用户laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:14