laravel 将[title]添加到可填充属性以允许在[App\Profile]上进行批量分配

czq61nw1  于 2023-02-05  发布在  其他
关注(0)|答案(2)|浏览(173)

我试图创建编辑配置文件,但当我点击编辑配置文件按钮,我得到下面的错误:
Illuminate \ Database \ Eloquent \ MassAssignmentException将[title]添加到可填充属性以允许在[App\Profile]上进行批量分配
bj.php文件夹:

<@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-4">
           <img src="https://scontent-cdt1-1.cdninstagram.com/vp/dcca3b442819fc8b9b63f09b2ebde320/5DA9E3CB/t51.2885-19/s150x150/40101184_290824334847414_1758201800999043072_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com" class="rounded-circle">
        </div>

        <div class="col-8">
            <div class="d-flex align-items-baseline">

                <div class="h4 mr-3 pt-2">{{ $user->username }}</div>
                <button class="btn btn-primary">S'abonner</button>
            </div>
            <div class="d-flex">
                <div class="mr-3">{{ $user->posts->count() }} article(s) en vente
            </div>
            <a href=" {{ route('profile.edit', ['username' => $user->username]) }}">Modifier Profile</a>
            <div class="mt-3">
                <div class="font-weight-bold">
                    {{ $user->profile->title }}
                </div>
                <div class="font-weight-bold">
                    {{ $user->profile->description }}
                </div>
            </div>

        </div>
    </div>

    <div class="row mt-5">
        @foreach ($user->posts as $post)

        <div class="col-4">
            <a href="{{ route('posts.show', ['post' => $post->id]) }}"> <img src="{{ asset('storage') . '/' . $post->image }}" class="w-100"></a>
        </div>
        @endforeach
    </div>


</div>
@endsection

配置文件控制器:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class ProfileController extends Controller
{
    public function show(User $user)
    {
        return view('profile.show', compact('user'));
    }

    public function edit(User $user)
    {
        return view('profile.edit', compact('user'));
    }

    public function update(User $user)
    {
        $data = request()->validate([
            'title' => 'required',
            'description' => 'required'
        ]);

        $user->profile->update($data);

        return redirect()->route('profile.show', ['user' => $user]);
    }
}

编辑. php:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Modifier profile</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('profile.update', ['user' => $user]) }}" enctype="multipart/form-data">
                        @csrf
                        @method('PATCH')

                        <div class="form-group">
                            <label for="title">Titre</label>

                            <div class="col-md-6">
                                <input id="title" type="text" class="form-control @error('title') is-invalid @enderror" name="title" value="{{ old('title') ?? $user->profile->title }}"  autocomplete="title" autofocus>

                                @error('title')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="description">Description</label>

                            <div class="col-md-6">
                                <textarea id="description" type="text" class="form-control @error('description') is-invalid @enderror" name="description" autocomplete="description" autofocus>{{ old('description') ?? $user->profile->description }}</textarea>

                                @error('description')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group">    
                            <div class="custom-file">

                                <input type="file" name="image" class="custom-file-input @error('image') is-invalid @enderror" id="validatedCustomFile" >
                                <label class="custom-file-label" for="validatedCustomFile">Choisir une image</label>
                                @error('image')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Modifier profile
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    protected $guarder = [];

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

我在这里做错了什么?我怎样才能摆脱这个错误?

pwuypxnk

pwuypxnk1#

您有一个拼写错误,请在您的模型中添加以下内容,而不是$guarder

protected $guarded = [];

我不建议使用空的guarded,而是使用$fillable

unhi4e5o

unhi4e5o2#

在相同的控制器模型中,使用

protected $guarded = [];

protected $fillable = [];

相关问题