1

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

Can anybody help?

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.

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

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

Here are my codes:

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:

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?

Here is the error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (form.images, CONSTRAINT images_user_id_foreign FOREIGN KEY (user_id) REFERENCES usere_information (id)) (SQL: insert into images (name, size, user_id, updated_at, created_at) values (download.png, 4247, 1, 2017-10-25 08:54:57, 2017-10-25 08:54:57))

Image model:

class Image extends Model
{

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

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

Images table:

   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();
    });

User_information table:

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

User_information model:

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');
    }
}

enter image description here

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:

        public function getInfo($id) {
$data = user_information::where('id',$id)->get();
          $data3=Image::where('user_id',$id)->get();
     return view('test',compact('data','data3'));
8
  • 1
    You posted upload.blade.php, but your code invokes create1. Are you sure you are calling the right template? Commented Oct 25, 2017 at 8:13
  • @Dkna pass an $id variable into your test() method. Commented Oct 25, 2017 at 8:16
  • @Maraboc maybe we clear the chat on top a bit, and also can i not put the id number manually, but instead automatically? Like maybe have a for loop so that I won't have to do it for all? Example maybe like if i go into user _information id = 3, then the user_id of Images is 3 Commented Oct 25, 2017 at 12:33
  • yes i know it's simply for testing purose only if the problem is solved the we will pass the correct ID ;) ok cleaning :) Commented Oct 25, 2017 at 12:37
  • But is it possible to make it find the id automatically instead of manually like what you had said just now? Sorry but am I confusing you? Commented Oct 25, 2017 at 12:39

4 Answers 4

1

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);
}

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');

and the your store method signature is

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

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’);

to

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

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

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

Then in the upload method :

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

In the view change

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

To

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

And you are good to go ;)

@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
Sign up to request clarification or add additional context in comments.

Comments

0

You have miss to pass id in your where condition,

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

and you have to pass your user data into this,

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

Hope it helps,

2 Comments

thanks for replying i have tried your code but the error remain the same, Undefined variable: user, and the view is upload, just now i took the wrong templete
yeah but i edited my answer look forward i passed user variable into your upload method your upload view template not getting $user->id thats why it throwing error.
0

On your upload function, you have to pass the user variable because you use the $user in the view. So the controller will be

public function upload() {
    $user = Auth::user();
    return view('upload', compact('user'));
}

do not forget to change the $user based on your need.

Comments

0

You have to pass an $id variable into your test() method. Then please comment below what's the error next so I can follow you through.

Update

Since you don't want to pass an id. You can use:

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

OR

Try to use first() instead of get().

More option: I have noticed, you're using the upload() method here, why not passing the $user there? like so:

public function upload(){
    $user = Auth::user();
    return view(‘upload’, compact('user'));
}

2 Comments

Hi tks for replying, I seem to still get the error undefined user when i try your code and also i can't use first() since i am getting all the id and not just the first
@Dkna Ok great. then use get() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.