Any error ?
Did you put some dump / dd to see your code in action?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
first, i'm new to laravel, i just following tutorials from youtube
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' => $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 :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
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 :
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Relations\HasMany;
use app\Models\Topic;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
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()
{
return $this->hasManyThrough(Topic::class, User::class);
}
public function profile()
{
return $this->hasOne(Profile::class);
}
public function replies(): HasMany
{
return $this->hasMany(Reply::class);
}
}
@Hiiro typo
@error('genre')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
Please or to participate in this conversation.