【发布时间】:2020-04-16 22:07:01
【问题描述】:
我正在尝试创建一个仅显示来自已登录用户的歌词的列表,但没有成功。需要帮助。
抱歉图片链接,但我的声誉仍然很低,无法添加图片。
如您所见,我有两个歌词菜单,一个仅供管理员用户访问,另一个用于显示经过身份验证的用户的歌词列表。
我该怎么做?
歌词控制器
{
public function __construct(){
$this->middleware('auth');
$this->middleware('can:admin-content');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$lyrics = Lyric::orderBy('lyric', 'ASC')->paginate('10');
return view('admin.lyrics.index', [
'lyrics' => $lyrics,
]);
}
/**
* Show the form for creating a new resource
*
* @return \Illuminate\Http\Response
*/
public function create()
{
// load the create form (app/views/lyrics/create.blade.php)
return view('admin.lyrics.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = $request->only([
'title',
// 'artist',
'info',
'video_url',
'lyric'
]);
$data['slug'] = Str::slug($data['title'], '-');
$validator = Validator::make($data, [
'title' => ['required', 'string', 'max:100'],
'slug' => ['required', 'string', 'max:100', 'unique:lyrics'],
// 'artist' => ['required', 'string', 'max:200'],
'info' => ['string', 'max:100'],
'video_url' => ['required', 'string', 'max:100', 'unique:lyrics'],
'lyric' => ['required', 'string'],
]);
if ($validator->fails()) {
return redirect()->route('lyrics.create')
->withErrors($validator)
->withInput();
}
// $artist = new Singer;
// $artist->artist = $data['artist'];
// $artist->save();
$lyric = new Lyric;
$lyric->title = trim($data['title']);
$lyric->slug = $data['slug'];
$lyric->info = $data['info'];
$lyric->video_url = $data['video_url'];
$lyric->lyric = $data['lyric'];
$lyric->save();
Session::flash('message', 'Música adicionada com sucesso!');
return redirect()->route('lyrics.index');
}
/**
* Display the specified resource
*
*
.
*
* @param \App\Lyric $lyric
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$lyric = Lyric::find($id);
return view('admin.lyrics.show', [
'lyric' => $lyric
]);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Lyric $lyric
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$lyric = Lyric::find($id);
if ($lyric) {
return view('admin.lyrics.edit', [
'lyric' => $lyric
]);
}
return redirect()->route('lyrics.index');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Lyric $lyric
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$lyric = Lyric::find($id);
if ($lyric) {
$data = $request->only([
'title',
// 'artist',
'info',
'video_url',
'lyric'
]);
if ($lyric['title'] !== $data['title']) {
$data['slug'] = Str::slug($data['title'], '-');
$validator = Validator::make($data, [
'title' => ['required', 'string', 'max:100'],
'info' => ['string', 'max:100'],
'video_url' => ['required', 'string', 'max:100', 'url'],
'slug' => ['required', 'string', 'max:100', 'unique:lyrics'],
'lyric' => ['string'],
]);
} else {
$validator = Validator::make($data, [
'title' => ['required', 'string', 'max:100'],
'info' => ['string', 'max:100'],
'video_url' => ['required', 'string', 'max:100', 'url'],
'lyric' => ['string'],
]);
}
if ($validator->fails()) {
return redirect()->route('lyrics.edit', [
'lyric' => $id
])
->withErrors($validator)
->withInput();
}
$lyric->title = trim($data['title']);
$lyric->info = $data['info'];
$lyric->video_url = $data['video_url'];
$lyric->lyric = $data['lyric'];
if (!empty($data['slug'])) {
$lyric->slug = $data['slug'];
}
$lyric->save();
}
Session::flash('message', 'Música alterada com sucesso!');
return redirect()->route('lyrics.index');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Lyric $lyric
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$lyric = Lyric::find($id);
$lyric->delete();
Session::flash('message', 'Música excluída com sucesso!');
return redirect()->route('lyrics.index');
}
编辑:我刚刚按照建议向用户迁移添加了外键
路由:web.php
Route::get('/', 'Site\HomeController@index')->name('home');
Auth::routes();
Route::prefix('painel')->group(function(){
Route::get('/', 'Admin\HomeController@index')->name('admin');
/* Login Routing */
Route::get('/login', 'Admin\Auth\LoginController@index')->name('login');
Route::post('/login', 'Admin\Auth\LoginController@authenticate');
/* Logout Route */
Route::post('/logout', 'Admin\Auth\LoginController@logout')->name('logout');
Route::get('/logout', 'Admin\Auth\LoginController@logout')->name('logout-get');
/* Register Routing */
Route::get('/register', 'Admin\Auth\RegisterController@index')->name('register');
Route::post('/register', 'Admin\Auth\RegisterController@register');
/* Users Routing */
Route::resource('/users', 'Admin\UserController');
/* Profile Routing */
Route::get('/profile', 'Admin\ProfileController@index')->name('profile');
Route::put('/profilesave', 'Admin\ProfileController@save')->name('profile.save');
/* Lyrics Routing */
Route::resource('/lyrics', 'Admin\LyricController');
});
User.php(模型):
public function lyrics()
{
return $this->hasMany(Lyric::class)->withTimestamps();
}
Lyric.php(模型):
protected $guarded = ['id', 'singer_id', 'created_at', 'updated_at'];
public function singer()
{
return $this->belongsTo(Singer::class)->withTimestamps();
}
public function user()
{
return $this->belongsTo(User::class);
}
【问题讨论】: