Laravel 6 storing data problem even though there are no errors

100 views Asked by At

I am trying to make a user able to post. I had some errors, but now I do not have any errors, but the store method does not work even though it does not show any errors. This is the form in home.balde.php

<form method="POST" action="{{ url('form-store') }}">
                        @csrf
                        <img class="avatar" src="images/uploads/avatars/{{ $user->avatar }}" alt="pic" width="50px">
                        <label for="ftext">
                            <input type="text" id="text" name="text" placeholder="What's happening?" style="background-color: transparent; border-color: transparent; color: white;" required>
                        </label>
                        <br>
                        <div class="row">
                            <div class="col">
                                <label for="ftopic">
                                    <input type="text" id="topic" name="topic" placeholder="Topic" style="background-color: transparent; border-color: transparent; color: white;" required>
                                </label>
                            </div>
                            <div class="col">
                                <label for="fhashtag">
                                    <input type="text" id="hashtag" name="hashtag" placeholder="Hashtag" style="background-color: transparent; border-color: transparent; color: white;" required>
                                </label>
                            </div>
                            <div class="col">
                                <div class="text-end">
                                    <button type="submit" class="tweetBtn">Tweet</button>
                                </div>
                            </div>
                        </div>
                    </form>

This is the model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tweets extends Model
{
    protected $fillable = ['content', 'topic', 'hashtag'];
}

This is HomeController

    public function store(Request $request)
    {
        if (!auth()->check()) {
            abort(403, 'Only authenticated users can create new posts.');
        }

        $data = request()->validate([
            'content' => 'required',
            'topic' => 'required|email',
            'hashtag' => 'required'
            ]);
            
            $check = Tweets::create($data);
            return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
    }

And these are the routes in web.php

Route::get('form', 'HomeController@index')->name('form');
Route::post('form-store', 'HomeController@store')->name('form-store');

When I click submit the page will just refresh and nothing happens also in database

2

There are 2 answers

1
Rajen Trivedi On

Check this code...

<label for="ftext">
     <input type="text" id="text" name="text" placeholder="What's happening?" style="background-color: transparent; border-color: transparent; color: white;" required>
</label>

Instead of name="text" it should be name="content"

1
jef On

In home.blade.php there is no <input name='content'>

Since in your HomeController validation topic should be email. Do type=email so it will be <input type="email" id="topic" name="topic" ...>

Anyway this is how I will code In HomeController. Maybe you can try it.

public function store(Request $request) {
    if (!auth()->check()) {
        abort(403, 'Only authenticated users can create new posts.');
    }

    $validator = Validator::make($request->all(), [
        'content' => 'required',
        'topic' => 'required|email',
        'hashtag' => 'required'
    ]);

    if ($validator->fails()) {
        abort(400, 'IDK Some Validation Error');
    }
    Tweets::create($request->all());
    return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}