Laravel 9 存取器没有返回值

Laravel 9 Accessor not Returning Value

我有一个 Reminder 模型,它有一个 sound_path 列。我为模型中的该列创建了一个访问器,但它返回 null 并且我仔细检查了数据库在该列中是否有值。我做错了什么?

注意:当然我可以直接在soundPathUrl mutator 中调用$this->sound_path 而无需从一开始就创建访问器,但我很想知道为什么如果我调用访问器没有返回任何值。

Reminder 型号

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Spatie\Translatable\HasTranslations;

class Reminder extends BaseModel
{
    use HasFactory, SoftDeletes, HasTranslations;

    public $translatable = [
        'title__ml',
    ];

    protected $casts = [
        'available_days_for_reminder' => 'json',
        'is_multiple_days_allowed' => 'boolean'
    ];

    protected $appends = ['sound_path_url'];

    /**
     * Get the sound path
     * 
     * @param string  $value
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    protected function soundPath(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $value
        );
    }

    /**
     * Get sound path download URL
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    protected function soundPathUrl(): Attribute
    {
        return new Attribute(
            get: fn () => asset('storage/' . $this->soundPath),
        );
    }
}

Reminder 控制器

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\Reminder\ReminderCollection;
use App\Http\Resources\Reminder\ReminderResource;
use App\Models\Reminder;
use Exception;
use Illuminate\Http\Request;

class ReminderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $reminders = Reminder::paginate(5);

        return ReminderCollection::collection($reminders);
    }
}

ReminderCollection API 资源

namespace App\Http\Resources\Reminder;

use Illuminate\Http\Resources\Json\JsonResource;

class ReminderCollection extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title__ml,
            'sound' => $this->sound_path_url
        ];
    }
}

Response

的截图

箭头处应该是sound_path的值。

  1. 似乎 soundPath 访问器什么都不做,它 returns 相同的值似乎是空的。 soundPath 值从何而来?首先,确保 soundPath 本身在该函数中具有任何值。

  2. 作为函数调用soundPath

    protected function soundPathUrl(): Attribute
    {
        return new Attribute(
            get: fn () => asset('storage/' . $this->soundPath()),
        );
    }
}

您的问题是您在 soundPathUrl 方法中调用 $this->soundPath 而不是 $this->sound_path...

所以,你应该有这个:

/**
 * Get sound path download URL
 *
 * @return \Illuminate\Database\Eloquent\Casts\Attribute
 */
protected function soundPathUrl(): Attribute
{
    return new Attribute(
        get: fn () => asset('storage/' . $this->sound_path),
    );
}

检查 documentation,您将看到您仍然必须使用 snake_case 调用您的属性。