数据未显示
我是Laravel的新手,我正在使用Laravel 9。我正在创建偏头痛日记网络应用程序。我设法创建了一个自定义登录并注册系统,并创建了一个页面以显示日记条目和日记条目表格。当前,该页面显示了具有其他几行的用户名和用户ID。为了显示日记条目,我目前正在尝试显示日记日期以使其正常工作,一旦我显示日期,我就应该能够显示其余的日期。
但是,完成表格后,我会按预期重定向到日记页面,但数据库的日期未显示。我检查了修补程序并存储了数据,当我使用DDD时,似乎没有将数据发送到视图,所以我怀疑这与我将数据传递到视图中有关的是
我当前有2个控制器的视图,一个是对处理获取请求以控制用户授权(并发布登录和注册的请求),而另一个与登录和注册无关的邮政请求。
用户与日记之间的关系是用户可以拥有许多日记条目,而日记只能有一个用户,
这是DDD()底部显示的内容 底部
的
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthorisationController;
use App\Http\Controllers\EntryController;
/*
|--------------------------------------------------------------------------
| 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('diary', [AuthorisationController::class, 'diary'])->name('diary');
Route::get('loginpage', [AuthorisationController::class, 'loginPage'])->name('loggingin');
Route::post('computeLogin', [AuthorisationController::class, 'completeLogin'])->name('login.custom');
Route::get('register', [AuthorisationController::class, 'registerPage'])->name('registerUser');
Route::post('computeRegister', [AuthorisationController::class, 'computeRegistration'])->name('register.compute');
Route::get('signout', [AuthorisationController::class, 'logOut'])->name('logout');
Route::get('/addDiary', [AuthorisationController::class, 'addDiary'])->name('addDiary');
Route::post('/addDiary/entry', [EntryController::class, 'addDiaryEntry'])->name('addDiaryEntry');
DDD
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'username',
'password',
];
public $timestamps = false;
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
];
public function diaries()
{
return $this->hasMany(Diary::class);
}
}
剪切
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Diary extends Model
{
use HasFactory;
protected $fillable = [
'date',
'user_id',
'stress',
'low_water_intake',
'chocolate',
'cheese',
'yeast_goods',
'yoghurt',
'fruit',
'nuts',
'olives',
'tomato',
'soy',
'vinegar',
'medication',
'comment'
];
public function user()
{
return $this->belongsTo(User::class);
}
}
<?php
/*
need to customise look
check for other bugs
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use App\Models\User;
use App\Models\Diary;
use Illuminate\Support\Facades\Auth;
class AuthorisationController extends Controller
{
# index
public function loginPage()
{
return view('auth.loginpage');
}
# customLogin
public function completeLogin(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required',
]);
$successful = 'Welcome User!';
$failedLogin = 'Error! Entered details are not valid, please try again or register an account!';
# diary
$verifications = $request->only('username', 'password');
if (Auth::attempt($verifications)) {
return redirect('diary')->with('success', $successful);
}
return redirect('loginpage')->with('failed', $failedLogin);
}
# registration
public function registerPage()
{
return view('auth.registerpage');
}
public function computeRegistration(Request $request)
{
$request->validate([
'username' => 'required|unique:users',
'password' => 'required|min:6',
'password1' => 'required|min:6',
]);
$info = $request->all();
if($info['password'] == $info['password1']){
User::create([
'username' => $info['username'],
'password' => Hash::make($info['password'])
]);
$successRegistration = 'You have created an account!';
$verifications = $request->only('username', 'password');
if (Auth::attempt($verifications)) {
return redirect('diary')->with('successReg', $successRegistration);
}
} else {
$notMatching = 'Passwords do not match';
return redirect('register')->with('notMatching', $notMatching);
}
}
public function diary(User $user){
if(Auth::check()){
ddd('wont show data', $user->diaries);
$data = $user->diaries->all();
return view('diary', [
'diaries' => $data,
]);
}
$noAccess = 'You are not signed in, either sign in or register!';
return redirect('loginpage')->with('noAccess', $noAccess);
}
public function addDiary()
{
if(Auth::check()){
return view('addDiary');
}
$noAccess = 'You are not signed in, either sign in or register!';
return redirect('loginpage')->with('noAccess', $noAccess);
}
public function logOut(){
Session::flush();
Auth::logout();
$loggedOut = 'You have logged out, want to log back in?';
return redirect('loginpage')->with('loggedOut', $loggedOut);
}
}
控制帖子与登录无关并注册)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Diary;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class EntryController extends Controller
{
public function addDiaryEntry(Request $request)
{
$rules = [
'date' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
return redirect()->back();
}
$data = $request->all();
$diaryEntry = new Diary();
$diaryEntry->user_id = Auth::user()->id;
$diaryEntry->date = $data['date'];
$diaryEntry->stress = $data['stress'];
$diaryEntry->low_water_intake = $data['dehydrated'];
$diaryEntry->chocolate = $data['chocolate'];
$diaryEntry->cheese = $data['cheese'];
$diaryEntry->yeast_goods = $data['yeast'];
$diaryEntry->yoghurt = $data['yoghurt'];
$diaryEntry->fruit = $data['fruit'];
$diaryEntry->nuts = $data['nuts'];
$diaryEntry->olives = $data['olives'];
$diaryEntry->tomato = $data['tomato'];
$diaryEntry->soy = $data['soy'];
$diaryEntry->vinegar = $data['vinegar'];
$diaryEntry->medication = $data['takenMeds'];
$diaryEntry->comment = $data['comment'];
if (!$diaryEntry->save())
{
return redirect()->back();
}
$diaryEntry->save();
return redirect('diary');
}
}
dairy.blade.php
@extends('layout')
@section('maincontent')
<main>
@foreach ($diaries as $diary)
<div class="container">
<h4>
Date
</h4>
<p>
{{ $diary->date->toDateString() }}
</p>
</div>
@endforeach
<p>
Welcome {{ auth()->user()->username }}!
</p>
<p>
Welcome user with id {{ auth()->user()->id }}!
</p>
@if (session('success'))
<small>
{{ session('success') }}
</small>
@elseif (session('successReg'))
<small>
{{ session('successReg') }}
</small>
@endif
<p style="margin:1em;">
Success.
</p>
<button>
<a href="/addDiary">Add Diary Entry</a>
</button>
</main>
@endsection
adddiary.blade.php
@extends('layout')
@section('maincontent')
<main>
<div class="container">
<form method="POST" action="{{ route('addDiaryEntry') }}">
@csrf
<div class="form-group row">
<label for="text" class="col-4 col-form-label">Date</label>
<div class="col-8">
<input id="date" name="date" type="date" class="form-control" required="required">
</div>
</div>
<div class="form-group row">
<label class="col-4">Stressed</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="stress" id="stress" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Dehydrated</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="dehydrated" id="dehydrated" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Chocolate</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="chocolate" id="chocolate" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Cheese</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="cheese" id="cheese" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Yeast Products</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="yeast" id="yeast" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Yoghurt</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="yoghurt" id="yoghurt" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Citrus Fruits</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="fruit" id="fruit" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Nuts</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="nuts" id="nuts" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Olives</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="olives" id="olives" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Tomato</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="tomato" id="tomato" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Soy</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="soy" id="soy" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Vinegar</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="vinegar" id="vinegar" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label for="takenMeds" class="col-4 col-form-label">Medication Taken</label>
<div class="col-8">
<textarea id="takenMeds" name="takenMeds" cols="40" rows="5" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<label for="comments" class="col-4 col-form-label">Comment</label>
<div class="col-8">
<textarea id="comments" name="comment" cols="40" rows="5" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</main>
@endsection
I am new to Laravel and I am using Laravel 9. I am creating a migraine diary web app. I managed to create a custom log in and register system and created a page to display the diary entries and a diary entry form. Currently that page shows the username and user id with a couple of other lines. For showing the diary entries, I am currently trying to show the diary date to get it working, once I can show the date, I should be able to show the rest.
However, after completing the form I am redirected to the diary page as intended but the date from the database is not showing. I checked tinker and the data is being stored and when I used ddd it seems that no data is being sent to the view so I suspect it is something to do with how I am passing data into the view
I currently have 2 controllers, one to handle get requests so as to control user authorisation (and post requests for log in and register) and the other for post requests not related to log in and registration.
The relationship between users and the diary is that a user can have many diary entries, while a diary can only have one user
this is what shows at the bottom of ddd()
snip of bottom of ddd
routes (web.php)
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthorisationController;
use App\Http\Controllers\EntryController;
/*
|--------------------------------------------------------------------------
| 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('diary', [AuthorisationController::class, 'diary'])->name('diary');
Route::get('loginpage', [AuthorisationController::class, 'loginPage'])->name('loggingin');
Route::post('computeLogin', [AuthorisationController::class, 'completeLogin'])->name('login.custom');
Route::get('register', [AuthorisationController::class, 'registerPage'])->name('registerUser');
Route::post('computeRegister', [AuthorisationController::class, 'computeRegistration'])->name('register.compute');
Route::get('signout', [AuthorisationController::class, 'logOut'])->name('logout');
Route::get('/addDiary', [AuthorisationController::class, 'addDiary'])->name('addDiary');
Route::post('/addDiary/entry', [EntryController::class, 'addDiaryEntry'])->name('addDiaryEntry');
User model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'username',
'password',
];
public $timestamps = false;
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
];
public function diaries()
{
return $this->hasMany(Diary::class);
}
}
Diary model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Diary extends Model
{
use HasFactory;
protected $fillable = [
'date',
'user_id',
'stress',
'low_water_intake',
'chocolate',
'cheese',
'yeast_goods',
'yoghurt',
'fruit',
'nuts',
'olives',
'tomato',
'soy',
'vinegar',
'medication',
'comment'
];
public function user()
{
return $this->belongsTo(User::class);
}
}
Authorisation Controller
<?php
/*
need to customise look
check for other bugs
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use App\Models\User;
use App\Models\Diary;
use Illuminate\Support\Facades\Auth;
class AuthorisationController extends Controller
{
# index
public function loginPage()
{
return view('auth.loginpage');
}
# customLogin
public function completeLogin(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required',
]);
$successful = 'Welcome User!';
$failedLogin = 'Error! Entered details are not valid, please try again or register an account!';
# diary
$verifications = $request->only('username', 'password');
if (Auth::attempt($verifications)) {
return redirect('diary')->with('success', $successful);
}
return redirect('loginpage')->with('failed', $failedLogin);
}
# registration
public function registerPage()
{
return view('auth.registerpage');
}
public function computeRegistration(Request $request)
{
$request->validate([
'username' => 'required|unique:users',
'password' => 'required|min:6',
'password1' => 'required|min:6',
]);
$info = $request->all();
if($info['password'] == $info['password1']){
User::create([
'username' => $info['username'],
'password' => Hash::make($info['password'])
]);
$successRegistration = 'You have created an account!';
$verifications = $request->only('username', 'password');
if (Auth::attempt($verifications)) {
return redirect('diary')->with('successReg', $successRegistration);
}
} else {
$notMatching = 'Passwords do not match';
return redirect('register')->with('notMatching', $notMatching);
}
}
public function diary(User $user){
if(Auth::check()){
ddd('wont show data', $user->diaries);
$data = $user->diaries->all();
return view('diary', [
'diaries' => $data,
]);
}
$noAccess = 'You are not signed in, either sign in or register!';
return redirect('loginpage')->with('noAccess', $noAccess);
}
public function addDiary()
{
if(Auth::check()){
return view('addDiary');
}
$noAccess = 'You are not signed in, either sign in or register!';
return redirect('loginpage')->with('noAccess', $noAccess);
}
public function logOut(){
Session::flush();
Auth::logout();
$loggedOut = 'You have logged out, want to log back in?';
return redirect('loginpage')->with('loggedOut', $loggedOut);
}
}
Entry Controller (this controls posts not related to log in and register)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Diary;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class EntryController extends Controller
{
public function addDiaryEntry(Request $request)
{
$rules = [
'date' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
return redirect()->back();
}
$data = $request->all();
$diaryEntry = new Diary();
$diaryEntry->user_id = Auth::user()->id;
$diaryEntry->date = $data['date'];
$diaryEntry->stress = $data['stress'];
$diaryEntry->low_water_intake = $data['dehydrated'];
$diaryEntry->chocolate = $data['chocolate'];
$diaryEntry->cheese = $data['cheese'];
$diaryEntry->yeast_goods = $data['yeast'];
$diaryEntry->yoghurt = $data['yoghurt'];
$diaryEntry->fruit = $data['fruit'];
$diaryEntry->nuts = $data['nuts'];
$diaryEntry->olives = $data['olives'];
$diaryEntry->tomato = $data['tomato'];
$diaryEntry->soy = $data['soy'];
$diaryEntry->vinegar = $data['vinegar'];
$diaryEntry->medication = $data['takenMeds'];
$diaryEntry->comment = $data['comment'];
if (!$diaryEntry->save())
{
return redirect()->back();
}
$diaryEntry->save();
return redirect('diary');
}
}
dairy.blade.php
@extends('layout')
@section('maincontent')
<main>
@foreach ($diaries as $diary)
<div class="container">
<h4>
Date
</h4>
<p>
{{ $diary->date->toDateString() }}
</p>
</div>
@endforeach
<p>
Welcome {{ auth()->user()->username }}!
</p>
<p>
Welcome user with id {{ auth()->user()->id }}!
</p>
@if (session('success'))
<small>
{{ session('success') }}
</small>
@elseif (session('successReg'))
<small>
{{ session('successReg') }}
</small>
@endif
<p style="margin:1em;">
Success.
</p>
<button>
<a href="/addDiary">Add Diary Entry</a>
</button>
</main>
@endsection
addDiary.blade.php
@extends('layout')
@section('maincontent')
<main>
<div class="container">
<form method="POST" action="{{ route('addDiaryEntry') }}">
@csrf
<div class="form-group row">
<label for="text" class="col-4 col-form-label">Date</label>
<div class="col-8">
<input id="date" name="date" type="date" class="form-control" required="required">
</div>
</div>
<div class="form-group row">
<label class="col-4">Stressed</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="stress" id="stress" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Dehydrated</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="dehydrated" id="dehydrated" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Chocolate</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="chocolate" id="chocolate" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Cheese</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="cheese" id="cheese" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Yeast Products</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="yeast" id="yeast" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Yoghurt</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="yoghurt" id="yoghurt" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Citrus Fruits</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="fruit" id="fruit" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Nuts</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="nuts" id="nuts" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Olives</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="olives" id="olives" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Tomato</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="tomato" id="tomato" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Soy</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="soy" id="soy" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label class="col-4">Vinegar</label>
<div class="col-8">
<div class="custom-control custom-checkbox custom-control-inline">
<input name="vinegar" id="vinegar" type="checkbox" class="custom-control-input" value="1">
</div>
</div>
</div>
<div class="form-group row">
<label for="takenMeds" class="col-4 col-form-label">Medication Taken</label>
<div class="col-8">
<textarea id="takenMeds" name="takenMeds" cols="40" rows="5" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<label for="comments" class="col-4 col-form-label">Comment</label>
<div class="col-8">
<textarea id="comments" name="comment" cols="40" rows="5" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</main>
@endsection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论