简体   繁体   中英

laravel project storing data to my database doesn't work

i have problem that store function does not work, this is forum website application using laravel 8. When user want to add a discussion: create discussion and when they finish input the data, nothing happenned, and nothing added on database basically, my code look like:

First the on the TopicController:

public function store(CreateTopicRequest $request)
    {
        $topic = new Topic;
        $topic = auth()->user()->$topic->create()([
            'genre_id' => $request->genre,
            'title' => $request->title,
            'slug' => Str::slug($request->title),
            'description' => $request->description
        ]);

        session()->flash('success', 'Discussion posted!');

        return redirect()->route(('topic.index'));
    }

for Topic Model:

class Topic extends Model
{
    use HasFactory;

    protected $table = 'topics';
    protected $primaryKey = 'topicID';
    protected $fillable = ['genre', 'title', 'description', 'created_at', 'updated_at'];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }
}

for User Model:

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     *
     */
    protected $table = 'users';
    protected $primaryKey = 'id';
    protected $fillable = [

        'nama',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function topics(): HasMany
    {
        return $this->hasMany(Topic::class);
    }

    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

my create discussion source code, just incase

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">


  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

  <link rel="shortcut icon" href="http://127.0.0.1:8000/image/favicon.ico">
  <title>Forum - Create Discussions</title>

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/trix/1.3.1/trix.css">
    </head>

<body>

  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container-fluid">
      <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-music-note-beamed" viewBox="0 0 16 16">
        <path d="M6 13c0 1.105-1.12 2-2.5 2S1 14.105 1 13c0-1.104 1.12-2 2.5-2s2.5.896 2.5 2zm9-2c0 1.105-1.12 2-2.5 2s-2.5-.895-2.5-2 1.12-2 2.5-2 2.5.895 2.5 2z" />
        <path fill-rule="evenodd" d="M14 11V2h1v9h-1zM6 3v10H5V3h1z" />
        <path d="M5 2.905a1 1 0 0 1 .9-.995l8-.8a1 1 0 0 1 1.1.995V3L5 4V2.905z" />
      </svg>
      &nbsp;
      &nbsp;
      &nbsp;
      &nbsp;
      &nbsp;
      &nbsp;
      &nbsp;

      <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="/topic">Home</a>
          </li>
          &nbsp;
          &nbsp;
          <li class="nav-item">
            <a class="nav-link " href="/playlistpop">Playlist</a>
          </li>
          &nbsp;
          &nbsp;

          <li class="nav-item">
            <a class="nav-link" href="http://127.0.0.1:8000/about">About</a>
          </li>
        </ul>
                <li class="nav-item dropdown">
          <a id="navbarDropdown" class="nav-link dropdown-toggle" href="/profile" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
            Andi
          </a>

          <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
            <a class="dropdown-item" href="/profile">
              Profile
            </a>
            <hr>
            <a class="dropdown-item" href="/logout">
              Logout
            </a>


          </div>
        </li>
              </div>


    </div>
  </nav>

  <div class="col-md">
    <div class="container">
    <br>
    <div class="row justify-content-center">
        <div class="col-md-15">

            <div class="card">
                <div class="card-header">Add Discussions </div>

                <div class="card-body">
                    <form action="http://127.0.0.1:8000/topic" method="POST">
                        <input type="hidden" name="_token" value="0PrvW3jQoQVM79yGTTissffS2JG69IhcCgYt34dT">                        <div class=form-group>
                            <label for="genre">Genre</label>
                            <br>
                            <select name="genre" id="genre" class="form-control">
                                                                <option value="">Pop</option>
                                                                <option value="">Rock</option>
                                                                <option value="">Jazz</option>
                                                                <option value="">Classic</option>
                                                            </select>
                        </div>
                        <br>
                        <div class="form-group">
                            <label for="floatingInput">Judul</label>
                            <br>
                            <input type="text" id="title" name="title" class="form-control" placeholder="Judul" required="required">
                        </div>
                        <br>
                        <div class="form-group">
                            <label for="description">Description</label>
                            <br>
                            <input id="description" type="hidden" name="description">
                            <trix-editor input="description"></trix-editor>
                        </div>

                        <br>
                        <button type="submit" class="btn btn-success">Submit</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

      </div>
  </div>















  <!-- Optional JavaScript; choose one of the two! -->

  <!-- Option 1: Bootstrap Bundle with Popper -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

  <!-- Option 2: Separate Popper and Bootstrap JS -->
  <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    -->

  <script src="http://127.0.0.1:8000/js/app.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.3.1/trix.js">

    </script>
    </body>

</html>

The topic model is supposed to have a user_id column While TopicController code here should look like this

use app\Models\Topic;
public function store(CreateTopicRequest $request)
    {
        $topic = Topic::create([
            'genre_id' => $request->genre,
            'title' => $request->title,
            'slug' => Str::slug($request->title),
            'description' => $request->description,
             'user_id' => auth()->user()->id
        ]);

        session()->flash('success', 'Discussion posted!');

        return redirect()->route(('topic.index'));
    }

Remember to add user_id column to fillable in Topic Model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM