เมื่อพยายามที่จะอัพโหลดรูปภาพทางไปยังฐานข้อมูลฉันองนี้เกิดข้อผิดพลาด"การสร้างปริยายจากวัตถุว่างเปล่าค่า"

0

คำถาม

ฉันได้ข้อผิดพลาดนี้"กำลังสร้างโดยปริยายจากวัตถุว่างเปล่าค่า"เมื่อไหร่ที่ฉันพยายามทำการอัปโหลดโพรไฟล์ของภาพตอนที่ลงทะเบียนผู้ใช้สิ่งที่ฉันต้องการจะทำคือการอัปโหลดไปที่รูปภาพทาง"profile-photos/PP_1637044275.jpg"เพื่อที่ฐานข้อมูลไม่ใช่ของภาพชื่ออะไรช่วยจะเป็นเกียรติอย่างยิ่ง

นี่คือ Controller/ชั้นเรียนการกระทำ

  class CreateNewUser implements CreatesNewUsers
 {
    use PasswordValidationRules;

  /**
 * Validate and create a newly registered user.
 *
 * @param  array  $input
 * @return \App\Models\User
 */
public function create(array $input)
{

    $request = request();

    //Handle profile photo Upload
    if ($request->hasFile('photo')) {
        // Get filename with extention 
        $filenamewithExt = $request->file('photo')->getClientOriginalName();
        // Get just filename
        $filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
        // Get just Extention
        $extention = $request->file('photo')->getClientOriginalExtension();
        // Filename to store
        $filenameToStore = $filename.'_'.time().'.'.$extention;
        // Upload Image
        $path = $request->file('photo')->storeAs('profile-photos', 
        $filenameToStore);

        $user = new User;
        $user->profile_photo_path = $path;
       

    }

    
 
    if ($request->hasFile('photo')) {
        
        $user->profile_photo_path = $path;
  
       
    }

นี่คือมุมมอง

     <!-- PROFILE INFO -->
    <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
    @csrf
    <div x-show.transition.in="step === 1">
    <div class="mb-5 text-center">
    <div class="mx-auto w-32 h-32 mb-2 border rounded-full relative bg-gray-100 mb-4 
    shadow-inset">
      <img id="image" class="object-cover w-full h-32 rounded-full" :src="image" />
    </div>

    <label
      for="fileInput"
      type="button"
      class="cursor-pointer inine-flex justify-between items-center focus:outline-none 
      border py-2 px-4 rounded-lg shadow-sm text-left text-gray-600 bg-white hover:bg- 
      gray-100 font-medium"
    >
      <svg xmlns="http://www.w3.org/2000/svg" class="inline-flex flex-shrink-0 w-6 h-6 - 
       mt-1 mr-1" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" 
        stroke-linecap="round" stroke-linejoin="round">
        <rect x="0" y="0" width="24" height="24" stroke="none"></rect>
        <path d="M5 7h1a2 2 0 0 0 2 -2a1 1 0 0 1 1 -1h6a1 1 0 0 1 1 1a2 2 0 0 0 2 2h1a2 
      2 0 0 1 2 2v9a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-9a2 2 0 0 1 2 -2" />
        <circle cx="12" cy="13" r="3" />
      </svg>
      Browse Photo
    </label>

    <div class="mx-auto w-48 text-gray-500 text-xs text-center mt-1">Click to add 
    profile picture</div>

    <input name="photo" id="fileInput" accept="image/*" class="hidden" type="file" 
    @change="let file = document.getElementById('fileInput').files[0];
      var reader = new FileReader();
      reader.onload = (e) => image = e.target.result;
      reader.readAsDataURL(file);">
   </div>
2

คำตอบที่ดีที่สุด

1

เกิดข้อผิดพลาดของคุณอาจจะกระตุ้นโดยบรรทัด:

   if ($request->hasFile('photo')) {
     
        $user->profile_photo_path = $path;
      
    }

พยายามลองนี้อย่างที่คุณช่วยชีวิตคน profile_photo_path ก่อนหน้านี้และนั่นมันของคุณ$ของผู้ใช้ตัวแปรอาจจะยังไม่ระบุที่นี่

แก้ไข:หากคุณเป็นการบันทึกภาพไปเป็นของสาธารณชนดิสก์คุณสามารถทำเรื่องนี้:

$user->profile_photo_path = 'profile-photos/'.$filenameToStore;

งั้นแสดงภาพของคุณเบลดมุมมองโดย

<img src="{{asset($user->profile_photo_path)}}">
2021-11-17 08:32:29

ขอบคุณมากฉันซาบซึ้งมัน
Adam

แน่นอน โชคดี!
fufubrocat
0

นี่คือสิ่งที่ฉันแก้ปัญหาจริงกะจะทำให้เสร็จก็จะบันทึกภาพ/โปรไฟล์รูปไปที่ฐานข้อมูลกับเส้นทางแบบโพรไฟล์-ภาพถ่าย/รูปภาพชื่อไม่ใช่แค่ภาพ/ชื่อแฟ้ม

   if ($request->hasFile('photo')) {
    // Get filename with extention 
    $ImageNameWithExt = $request->file('photo')->getClientOriginalName();
    // Get just filename
    $ImageName = pathinfo($ImageNameWithExt, PATHINFO_FILENAME);
    // Get just Extention
    $Extentions = $request->file('photo')->getClientOriginalExtension();
    // Filename to store
    $filenameToStore = $ImageName.'_'.time().'.'.$Extentions;
    // Upload Image
    $paths = $request->file('photo')->storeAs('public/profile-photos', 
    $filenameToStore);

    $path = 'profile-photos';
    $user = new User;
    $user->profile_photo_path =  $path . '/' . $filenameToStore;


}

        $imageWithPath = $user->profile_photo_path;

        $user =  User::create([
        'profile_photo_path'=> $imageWithPath,
        'username'          => $input['username'],
2021-11-17 04:08:15

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่