@if ($post_img)
temporaryUrl() }}">
@endif
Post
Компонент Livewire
namespace App\Livewire\Pages;
// ...Other use namespace calls.
use Livewire\WithFileUploads;
class GroupPage extends Component
{
use WithFileUploads;
public $group_id = '';
public $content = '';
public $post_img;
public function store_post()
{
$validated = $this->validate([
'content' => 'required|string',
'post_img' => 'nullable|image|max:2048', // Add validation rule here
]);
$user_id = Auth::id();
$post = Post::create([
'content' => $validated['content'],
'group_id' => $this->group_id,
'user_id' => $user_id,
]);
if ($this->post_img) {
$filename = $this->post_img->hashName();
$path = $this->post_img->storeAs('uploads/posts');
Asset::create([
'path' => $path,
'type' => 'image',
'user_id' => $user_id,
'post_id' => $post->id(),
]);
}
$this->reset('content');
$this->post_img = null;
session()->flash('success', 'Post created!');
}
// ...
}
Модель актива:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Asset extends Model
{
protected $fillable = ['path', 'type', 'user_id', 'post_id'];
public function user()
{
return $this->belongsTo(User::class);
}
public function post()
{
return $this->belongsTo(Post::class);
}
}
Миграция активов:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('assets', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('path');
$table->enum('type', ['image', 'video', 'file']);
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->foreignId('post_id')->constrained('posts')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('assets', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['post_id']);
});
Schema::dropIfExists('assets');
}
};
Подробнее здесь: https://stackoverflow.com/questions/786 ... e-database
Мобильная версия