[英]Laravel - voyager BREAD
I started my simple laravel project with posts.我用帖子开始了我的简单 Laravel 项目。 First I created my auth, than my posts and everything works fine.
首先我创建了我的身份验证,而不是我的帖子,一切正常。 But I installed voyager admin panel(without dummy) in my project and I add BREAD to posts table, but when I try to Edit some of my posts it shows me an error:
Call to undefined method App\\Post::getTranslationsOf() (View: C:\\engineering\\xampp\\htdocs\\lsappdev\\vendor\\tcg\\voyager\\resources\\views\\posts\\edit-add.blade.php)
.但是我在我的项目中安装了 voyager 管理面板(没有虚拟)并将 BREAD 添加到帖子表中,但是当我尝试编辑我的一些帖子时,它显示了一个错误:
Call to undefined method App\\Post::getTranslationsOf() (View: C:\\engineering\\xampp\\htdocs\\lsappdev\\vendor\\tcg\\voyager\\resources\\views\\posts\\edit-add.blade.php)
。 Why is this error showing?为什么会显示这个错误? Here is my
PostsController
:这是我的
PostsController
:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Post;
use DB;
class PostsController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('posts.index')->with('posts', $posts);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'cover_image' => 'image|nullable|max:1999'
]);
// Handle File Upload
if($request->hasFile('cover_image')){
// Get filename with the extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$post = new Post;
$post->title = $request->input('title');
$post->description = $request->input('title');
$post->user_id = auth()->user()->id;
$post->cover_image = $fileNameToStore;
$post->save();
return redirect('/posts');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = Post::find($id);
return view('posts.show')->with('post', $post);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post = Post::find($id);
//Check if post exists before deleting
if (!isset($post)){
return redirect('/posts')->with('error', 'No Post Found');
}
// Check for correct user
if(auth()->user()->id !==$post->user_id){
return redirect('/posts')->with('error', 'Unauthorized Page');
}
return view('posts.edit')->with('post', $post);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'cover_image' => 'image|nullable|max:1999'
]);
$post = Post::find($id);
// Handle File Upload
if($request->hasFile('cover_image')){
// Get filename with the extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
// Delete file if exists
Storage::delete('public/cover_images/'.$post->cover_image);
}
$post->title = $request->input('title');
$post->body = $request->input('description');
if($request->hasFile('cover_image')){
$post->cover_image = $fileNameToStore;
}
$post->save();
return 123;
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post = Post::find($id);
//Check if post exists before deleting
if (!isset($post)){
return redirect('/posts')->with('error', 'No Post Found');
}
// Check for correct user
if(auth()->user()->id !==$post->user_id){
return redirect('/posts')->with('error', 'Unauthorized Page');
}
if($post->cover_image != 'noimage.jpg'){
// Delete Image
Storage::delete('public/cover_images/'.$post->cover_image);
}
$post->delete();
return redirect('/posts');
}
}
And here is my Post.php
model:这是我的
Post.php
模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravelista\Comments\Commentable;
class Post extends Model
{
use Commentable;
protected $table = 'posts';
protected $primaryKey = 'id';
protected $fillable = ['title', 'description', 'cover_image', 'user_id'];
public function user(){
return $this->belongsTo(User::class);
}
}
Please if anybody know a solution for this error help!请如果有人知道此错误的解决方案帮助! Everything worked fine before I installed Voyager in my project.
在我的项目中安装 Voyager 之前,一切正常。
You are using the voyager model.您正在使用航海者模型。 So in your post model at the top
所以在你顶部的帖子模型中
Use \TCG\Voyager\Traits\Translatable;
And inside braces和大括号内
Use Translatable;
Hope it helps希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.