Ogólnie jakiś czas temu wszystko działało, ale zachciało mi się przerobić żeby wyświetlało mi sweetalert.
Środowisko Laravel10.
Mój routing:
<?php
use App\Http\Livewire\LandingComponent;
use App\Http\Controllers\NewsletterController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
Route::get('/',LandingComponent::class)->name('landing');
Route::post('/newsletter/subscribe',NewsletterController::class)->name('newsletter.subscribe');
NewsletterController:
<?php
namespace App\Http\Controllers;
use App\Models\Subscriber;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use RealRashid\SweetAlert\Facades\Alert;
use RealRashid\SweetAlert\SweetAlertServiceProvider;
// class NewsletterController extends Controller
// {
// public function subscribe(Request $request)
// {
// // Walidacja danych
// $validator = Validator::make($request->all(), [
// 'name' => 'required|max:50',
// 'email' => 'required|email|unique:subscribers,email|max:70',
// ]);
// // Sprawdzenie poprawności walidacji
// if ($validator->fails()) {
// return redirect()->back()->withErrors($validator)->withInput();
// }
// // Zapis subskrybenta
// $subscriber = new Subscriber();
// $subscriber->name = $request->input('name');
// $subscriber->email = $request->input('email');
// $subscriber->save();
// Alert::success('Sukces', 'Pomyślnie zarejestrowano!')->autoClose(3000);
// // Przekierowanie na stronę potwierdzenia subskrypcji
// return redirect()->route('landing');
// }
// }
class NewsletterController extends Controller
{
public function subscribe(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:subscribers',
]);
Subscriber::create($request->all());
return redirect()->back()->with('success', 'Zapisano na newsletter.');
}
}
Model Subscriber.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Subscriber extends Model
{
use HasFactory;
}
Subscribers table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSubscribersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('subscribers', function (Blueprint $table) {
$table->id();
$table->string('name', 50);
$table->string('email', 70)->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('subscribers');
}
}
Gdzieś jest jakiś błąd i nic mi się nie wyświetla, nie widzę go, nie wiem co z tym mam dalej zrobić. Nic się nie pojawia w tabelach.