Search code examples
phpmysqllaravelframeworkslaravel-5.3

Can't get data to save in MySQL using Laravel


I'm trying to get the ticket data to save in the database and when I submit the form I get no error, but it does not insert the data into the database. Added my Route just adding random text because the post

Controller Code

public function store(Request $request)
{
    $this->validate($request, [
       'name' => 'required',
        'title' => 'required|min:15',
        'category' => 'required',
        'priority' => 'required',
        'message' => 'required|min:100',
    ]);

    $ticket = new Ticket([
        'user_id' => Auth::user()->id,
        'category_id' => $request->input('category'),
        'ticket_id' => strtoupper(str_random(10)),
        'name' => $request->input('name'),
        'title' => $request->input('title'),
        'priority_id' => $request->input('priority'),
        'message' => $request->input('message'),
    ]);

    $ticket->status_id = '1';
    $ticket->save();

    return 'Success';
}

Model Code

class Ticket extends Model
{

    protected $fillable = [
      'user_id', 'category_id', 'ticket_id', 'name', 'title', 'priority_id', 'message', 'status_id',
    ];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function priority()
    {
        return $this->belongsTo(Priority::class);
    }

    public function status()
    {
        return $this->belongsTo(Status::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

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

Routes

Route::get('/', 'HomeController@index')->name('home');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/compliance', 'HomeController@Compliance')->name('compliance');
Route::get('/messages', 'HomeController@Messages')->name('messages');
Route::get('/tickets', 'TicketsController@userTickets')->name('tickets');
Route::get('/tickets/create', 'TicketsController@create')->name('tickets/create');
Route::post('/tickets/store', 'TicketsController@store');

Solution

  • Thanks for all the help I figured it I had two mistakes in the controller this took me 2 days to figure it out LOL

            $this->validate($request, [
            'name' => 'required', // I removed this
            'title' => 'required|min:15',
            'category' => 'required',
            'priority' => 'required',
            'message' => 'required|min:100',
        ]);
    

    and in the $ticket

           $ticket = new Ticket([
            'user_id' => Auth::user()->id,
            'category_id' => $request->input('category'),
            'ticket_id' => strtoupper(str_random(10)),
            'name' => $request->input('name') // I replaced this with 'name' => Auth::user()->name,
            'title' => $request->input('title'),
            'priority_id' => $request->input('priority'),
            'message' => $request->input('message'),
        ]);