Home > OS >  Laravel-Undefined variable in Laravel 9.x
Laravel-Undefined variable in Laravel 9.x

Time:12-01

I have a problem while I was trying to use the $user variable, I'm pretty new to laravel but from what I can gather my controller file isn't able to pass the variable $user to my blade file. I have tried many solutions but I'm still not able to solve this problem. These are the files that I use:

Blade file:

<body>
 <div >
  <img src="{{$user->image}}" id="imageName" >
</div>
  </div>
  </div>
  <form action="{{ route ('update_profile', $user->id) }}" method="POST" enctype="multipart/form-data">
  @csrf
 <input type="file" name="picture" id="my-file">
<input type="submit" name="Upload">
</form>
<br>
    <div >
<button >
  Connect
</button>
    <button >
  Message
</button>
    </div>
  </div>
<br>
  <div >
    <h1 >Jessica Jones</h1>
    <p >123 Address</p>

    <p >Rentee</p>
  </div>

  <div >
    <p >Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  <div >
    <button >
  Show more
</button>
</div>
  </div>

</div>
</div>
</body>

<script>
  document.getElementById("my-file").onchange = function() {
  if (this.files && this.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
        // e.target.result is a base64-encoded url that contains the image data
      document.getElementById('imageName').setAttribute('src', e.target.result);
    };
    reader.readAsDataURL(this.files[0]);
  }
}
</script>

</html>

<?php

Controller file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Profile;

class ProfileController extends Controller
{

  public function update_profile(Request $request,$id) {
    $rquest->validate([
      'picture'=>'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);
    $user = User::where('id', $id)->first();
    unlink($user->image);


    $image_name = $request->image->extension();
    $request->image->move(public_path('public/images/profile/'),$image_name);
    $path = 'images/profile/'.$image_name;
    $user->image = $path;
    $user->save();
    return view('profile', compact ('user'));
 }
}

Model file:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'size',
    ];
}

This is the error that I encountered

enter image description here

I have tried adding compact('user') to the return view line in the controller, tried using ['user' => $user] at the same return line and also clearing the route cache but they all resulted in the same error.

CodePudding user response:

try to use instead compact($user) send data to blade using with('user', $user)

return view('profile')->with('user', $user);

update

how you ar doing update from your user and you use method save() you can´t return result.

if you do this:

$user->update([
            'name' => $request->name,
            'email' => $request->email,
            'updated_at' => now()
        ]);

    return $this->success('profile','Profile updated successfully!');

you can return message or result. Also you can use this:

$user = User::find($id);
            $user->name = $request->get('name');
            $user->email = $request->get('email');
            $user->password = \Hash::make($request->get('password'));
            
            $user->update();  

            if($user){
                return redirect()->back()->with('message', 'User updated!');
            }else{
                return redirect()->back()->with('error', ' Error!');
            }

CodePudding user response:

You are saving the user Instead of Update. Please update the user.

$user = User::where("id",$id)->firstOrFail();
$user->name = $request->name;
$user->mobile_no = $request->mobile_no;
$user->user_role = $request->user_role;
$user->updated_at = Carbon::now();
$updateUser = $user->update();
return view('profile',compact("user"));

More important

  • Please share all error messages in text form, along with your attempts to resolve the problem.
  • Before your return the user, please see what's in the user variable.( dd($user); ).
  • The HTML source in your error message does not match the HTML you posted in your question.
  • Please use validate method properly.
  • Related