Skip to content

Commit

Permalink
feat! create events with images
Browse files Browse the repository at this point in the history
  • Loading branch information
slenderik committed May 30, 2024
1 parent ff74071 commit 1927587
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 31 deletions.
31 changes: 25 additions & 6 deletions app/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace App\Http\Controllers;

use App\Models\Event;
use App\Models\EventImage;
use Illuminate\Http\Request;

class EventController extends Controller
{
public function index()
{
$events = Event::all();
// $events = Event::all();
$events = Event::with('eventImage')->get();
return view('events.index', compact('events'));
}

Expand All @@ -22,7 +24,7 @@ public function create()
{
return view('events.create');
}

public function new(Request $request)
{
// Валидация данных
Expand All @@ -31,11 +33,28 @@ public function new(Request $request)
'title' => 'required|max:255',
'description' => 'required',
'background_colour' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'image_alt' => 'required|max:255',
]);

// Создание новой книги
Event::create($request->all());


// Сохранение фото
$imageName = time().'.'.$request->image->extension();
$request->image->storeAs('images', $imageName, 'public');

// Создание ивента
$event = Event::create($request->all());

// Сохранение пути к изображению в базе данных или другие действия
$eventId = $event->id;
$imageAlt = $request->image_alt;

$imageRequest = [
'event_id' => $eventId,
'image_name' => $imageName,
'image_alt' => $imageAlt,
];
EventImage::create($imageRequest);

// Перенаправление на какую-либо страницу (например, список книг)
return redirect()->route('events.index');
}
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ class Event extends Model
'description',
'background_colour',
];

// Define the relationship
public function eventImage()
{
return $this->hasOne(EventImage::class);
}
}
36 changes: 36 additions & 0 deletions app/Models/EventImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class EventImage extends Model
{
use HasFactory;


/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'event_id',
'image_name',
'image_alt'
];

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;

// Define the inverse relationship
public function event()
{
return $this->belongsTo(Event::class);
}
}
10 changes: 4 additions & 6 deletions public/css/events.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ body {
width: 300px;
height: 320px;
border-radius: 8px;
border-collapse: separate;
}

.event-card__img {
Expand All @@ -55,7 +54,7 @@ body {
background-color: aqua;
}

.event-cont__bottom-part {
.event-card__bottom-part {
height: 20px;
width: 100%;

Expand All @@ -68,11 +67,10 @@ body {
}
/* EVENT CARD */

.event-cont__img-like {

}

.events-list-container {
display: flex;
flex-wrap: wrap;
gap: 30px;

justify-content: space-between;
}
2 changes: 1 addition & 1 deletion resources/views/events/card.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
{{ __('Event') }}
{{ __('Ивент') }}
</h2>
</x-slot>

Expand Down
34 changes: 20 additions & 14 deletions resources/views/events/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Create event</title>
</head>
<body>
<h1>Create a new Book</h1>
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
{{ __('Добавить ивент') }}
</h2>
</x-slot>

<!-- Вывод ошибок валидации -->
@if ($errors->any())
Expand All @@ -17,25 +16,32 @@
</div>
@endif

<form action="{{ route('events.new') }}" method="POST">
<form action="{{ route('events.new') }}" method="POST" enctype="multipart/form-data">
@csrf
<div>
<label for="english_name">Eng. name:</label>
<label for="english_name">Англ. название:</label>
<input type="text" id="english_name" name="english_name" value="{{ old('english_name') }}">
</div>
<div>
<label for="title">Title:</label>
<label for="title">Заголовок:</label>
<input type="text" id="title" name="title" value="{{ old('title') }}">
</div>
<div>
<label for="background_colour">Background colour:</label>
<input type="text" id="background_colour" name="background_colour" value="{{ old('background_colour') }}">
</div>
<div>
<label for="description">Description:</label>
<label for="description">Описание:</label>
<textarea id="description" name="description">{{ old('description') }}</textarea>
</div>
<button type="submit">Create</button>
<div>
<label for="image">Картинка:</label>
<input type="file" id="image" name="image">
</div>
<div>
<label for="image_alt">Опишите картинку:</label>
<input type="text" id="image_alt" name="image_alt">
</div>
<button type="submit">Создать</button>
</form>
</body>
</html>
</x-app-layout>
9 changes: 7 additions & 2 deletions resources/views/events/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
@foreach ($events as $event)

{{-- EVENT CARD --}}
<div class="event-card">
<a href="/event/{{ $event->id }}">
<img src="" alt="THIS IS IMAGE IMAGINE THIS BRO" class="event-card__img">
<div class="event-cont__bottom-part">
@if ($event->eventImage)
<img src="{{ asset('storage/images/'.$event->eventImage->image_name) }}" alt="{{ $event->eventImage->image_alt }}" class="event-card__img">
@else
<img src="{{ asset('default-image-path') }}" alt="Default Image" class="event-card__img">
@endif
<div class="event-card__bottom-part">
<p>{{ $event->title }}</p>
</div>
<svg></svg>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<div class="flex justify-between h-16">
<div class="flex">
<!-- Logo -->
{{-- <div class="shrink-0 flex items-center">
<div class="shrink-0 flex items-center">
<a href="{{ route('dashboard') }}">
<x-application-logo class="block h-9 w-auto fill-current text-gray-800 dark:text-gray-200" />
</a>
</div> --}}
</div>

<!-- Navigation Links -->
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
Expand Down

0 comments on commit 1927587

Please sign in to comment.