Laravel/ページ遷移の指定について
解決したいこと
今課題の練習で疑似Twitterを作成しているのですが、
投稿した記事の編集画面を作成し画像の縦並び・・・を押すと遷移するようにした後、
いいねやコメント機能を作成し動作確認をしていたところできていた編集画面への遷移ができなくなってい待っていました。
自分で確認してもわからなくなってしまい行き詰ったためケアレスミスなどかなとは思うのですが、ご指摘・ご教授を頂けたら幸いです。
発生している問題
Model
app/Models/Tweet.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\softDeletes;
class Tweet extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'text'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function favorites()
{
return $this->hasMany(Favorite::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function getUserTimeLine(Int $user_id)
{
return $this->where('user_id', $user_id)->orderBy('created_at', 'DESC')->paginate(50);
}
public function getTweetCount(Int $user_id)
{
return $this->where('user_id', $user_id)->count();
}
// 詳細画面
public function getTweet(Int $tweet_id)
{
return $this->with('user')->where('id', $tweet_id)->first();
}
// 一覧画面
public function getTimeLines(Int $user_id, Array $follow_ids)
{
$follow_ids[] = $user_id;
return $this->whereIn('user_id', $follow_ids)->orderBy('created_at', 'DESC')->paginate(50);
}
public function tweetStore(Int $user_id, Array $data)
{
$this->user_id = $user_id;
$this->text = $data['text'];
$this->save();
return;
}
public function getEditTweet(Int $user_id, Int $tweet_id)
{
return $this->where('user_id', $user_id)->where('id', $tweet_id)->first();
}
public function tweetUpdate(Int $tweet_id, Array $data)
{
$this->id = $tweet_id;
$this->text = $data['text'];
$this->update();
return;
}
public function tweetDestroy(Int $user_id, Int $tweet_id)
{
return $this->where('user_id', $user_id)->where('id', $tweet_id)->delete();
}
}
Controllers
app/Http/Controllers/TweetsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Tweet;
use App\Models\Comment;
use App\Models\Follower;
class TweetsController extends Controller
{
public function index(Tweet $tweet, Follower $follower)
{
$user = auth()->user();
$follow_ids = $follower->followingIds($user->id);
$following_ids = $follow_ids->pluck('followed_id')->toArray();
$timelines = $tweet->getTimelines($user->id, $following_ids);
return view('tweets.index', [
'user' => $user,
'timelines' => $timelines
]);
}
public function create()
{
$user = auth()->user();
return view('tweets.create', [
'user' => $user
]);
}
public function store(Request $request, Tweet $tweet)
{
$user = auth()->user();
$data = $request->all();
$validator = Validator::make($data, [
'text' => ['required', 'string', 'max:140']
]);
$validator->validate();
$tweet->tweetStore($user->id, $data);
return redirect('tweets');
}
public function show(Tweet $tweet, Comment $comment)
{
$user = auth()->user();
$tweet = $tweet->getTweet($tweet->id);
$comments = $comment->getComments($tweet->id);
return view('tweets.show', [
'user' => $user,
'tweet' => $tweet,
'comments' => $comments
]);
}
public function edit(Tweet $tweet)
{
$user = auth()->user();
$tweets = $tweet->getEditTweet($user->id, $tweet->id);
if (!isset($tweets)) {
return redirect('tweets');
}
return view('tweets.edit', [
'user' => $user,
'tweets' => $tweets
]);
}
public function update(Request $request, Tweet $tweet)
{
$data = $request->all();
$validator = Validator::make($data, [
'text' => ['required', 'string', 'max:140']
]);
$validator->validate();
$tweet->tweetUpdate($tweet->id, $data);
return redirect('tweets');
}
public function destroy(Tweet $tweet)
{
$user = auth()->user();
$tweet->tweetDestroy($user->id, $tweet->id);
return back();
}
}
View
resources/views/tweets/edit.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Update</div>
<div class="card-body">
<form method="POST" action="{{ route('tweets.update', ['tweets' => $tweets]) }}">
@csrf
@method('PUT')
<div class="form-group row mb-0">
<div class="col-md-12 p-3 w-100 d-flex">
<img src="{{ asset('storage/profile_image/' .$user->profile_image) }}" class="rounded-circle" width="50" height="50">
<div class="ml-2 d-flex flex-column">
<p class="mb-0">{{ $user->name }}</p>
<a href="{{ url('users/' .$user->id) }}" class="text-secondary">{{ $user->screen_name }}</a>
</div>
</div>
<div class="col-md-12">
<textarea class="form-control @error('text') is-invalid @enderror" name="text" required autocomplete="text" rows="4">{{ old('text') ? : $tweets->text }}</textarea>
@error('text')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-12 text-right">
<p class="mb-4 text-danger">140文字以内</p>
<button type="submit" class="btn btn-primary">
ツイートする
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
resources/views/tweets/show.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center mb-5">
<div class="col-md-8 mb-3">
<div class="card">
<div class="card-haeder p-3 w-100 d-flex">
<img src="{{ asset('storage/profile_image/' .$tweet->user->profile_image) }}" class="rounded-circle" width="50" height="50">
<div class="ml-2 d-flex flex-column">
<p class="mb-0">{{ $tweet->user->name }}</p>
<a href="{{ url('users/' .$tweet->user->id) }}" class="text-secondary">{{ $tweet->user->screen_name }}</a>
</div>
<div class="d-flex justify-content-end flex-grow-1">
<p class="mb-0 text-secondary">{{ $tweet->created_at->format('Y-m-d H:i') }}</p>
</div>
</div>
<div class="card-body">
{!! nl2br(e($tweet->text)) !!}
</div>
<div class="card-footer py-1 d-flex justify-content-end bg-white">
@if ($tweet->user->id === Auth::user()->id)
<div class="dropdown mr-3 d-flex align-items-center">
<a href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-ellipsis-v fa-fw"></i>
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<form method="POST" action="{{ url('tweets/' .$tweet->id) }}" class="mb-0">
@csrf
@method('DELETE')
<a href="{{ url('tweets/' .$tweet->id .'/edit') }}" class="dropdown-item">編集</a>
<button type="submit" class="dropdown-item del-btn">削除</button>
</form>
</div>
</div>
@endif
<div class="mr-3 d-flex align-items-center">
<a href="{{ url('tweets/' .$tweet->id) }}"><i class="far fa-comment fa-fw"></i></a>
<p class="mb-0 text-secondary">{{ count($tweet->comments) }}</p>
</div>
<div class="d-flex align-items-center">
@if (!in_array($user->id, array_column($tweet->favorites->toArray(), 'user_id'), TRUE))
<form method="POST" action="{{ url('favorites/') }}" class="mb-0">
@csrf
<input type="hidden" name="tweet_id" value="{{ $tweet->id }}">
<button type="submit" class="btn p-0 border-0 text-primary"><i class="far fa-heart fa-fw"></i></button>
</form>
@else
<form method="POST" action="{{ url('favorites/' .array_column($tweet->favorites->toArray(), 'id', 'user_id')[$user->id]) }}" class="mb-0">
@csrf
@method('DELETE')
<button type="submit" class="btn p-0 border-0 text-danger"><i class="fas fa-heart fa-fw"></i></button>
</form>
@endif
<p class="mb-0 text-secondary">{{ count($tweet->favorites) }}</p>
</div>
</div>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-8 mb-3">
<ul class="list-group">
@forelse ($comments as $comment)
<li class="list-group-item">
<div class="py-3 w-100 d-flex">
<img src="{{ asset('storage/profile_image/' .$comment->user->profile_image) }}" class="rounded-circle" width="50" height="50">
<div class="ml-2 d-flex flex-column">
<p class="mb-0">{{ $comment->user->name }}</p>
<a href="{{ url('users/' .$comment->user->id) }}" class="text-secondary">{{ $comment->user->screen_name }}</a>
</div>
<div class="d-flex justify-content-end flex-grow-1">
<p class="mb-0 text-secondary">{{ $comment->created_at->format('Y-m-d H:i') }}</p>
</div>
</div>
<div class="py-3">
{!! nl2br(e($comment->text)) !!}
</div>
</li>
@empty
<li class="list-group-item">
<p class="mb-0 text-secondary">コメントはまだありません。</p>
</li>
@endforelse
<li class="list-group-item">
<div class="py-3">
<form method="POST" action="{{ route('comments.store') }}">
@csrf
<div class="form-group row mb-0">
<div class="col-md-12 p-3 w-100 d-flex">
<img src="{{ asset('storage/profile_image/' .$user->profile_image) }}" class="rounded-circle" width="50" height="50">
<div class="ml-2 d-flex flex-column">
<p class="mb-0">{{ $user->name }}</p>
<a href="{{ url('users/' .$user->id) }}" class="text-secondary">{{ $user->screen_name }}</a>
</div>
</div>
<div class="col-md-12">
<input type="hidden" name="tweet_id" value="{{ $tweet->id }}">
<textarea class="form-control @error('text') is-invalid @enderror" name="text" required autocomplete="text" rows="4">{{ old('text') }}</textarea>
@error('text')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-12 text-right">
<p class="mb-4 text-danger">140文字以内</p>
<button type="submit" class="btn btn-primary">
ツイートする
</button>
</div>
</div>
</form>
</div>
</li>
</ul>
</div>
</div>
</div>
@endsection
勉強中でまだまだ知識がなく初歩的なところかと思われますが、なにか気づいたり見ていただける方、是非ご教授ご指摘をお願いいたします
0