我正在构建一个项目,用户可以上传项目,其他用户可以评论每个项目
现在我可以上传项目并构建评论系统,但现在我无法上传项目,我只能评论已存储在我的数据库中的项目。
有人知道我为什么不能存储项目吗?
我的路线:
// add comment
Route::post('projects/{id}','CommentController@store');
//Project routes REST methode
Route::post('projects/store', 'ProjectsController@store');
Route::resource('projects', 'ProjectsController');
我的ProjectsController:
public function index()
{
$projects = Project::all();
//return $projects;
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store(Request $request)
{
// getting all of the post data
$file = array('image' => Input::file('image'));
// setting up rules
$rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
// doing the validation, passing post data, rules and the messages
$validator = Validator::make($file, $rules);
if ($validator->fails())
{
// send back to the page with the input data and errors
return Redirect::to('projects/create')->withInput()->withErrors($validator);
}
else
{
// checking file is valid.
if (Input::file('image')->isValid())
{
$destinationPath = 'uploads/projects'; // upload path
$extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renameing image
Input::file('image')->move($destinationPath, $fileName); // uploading file to given path
// sending back with message
Session::flash('success', 'Upload successfully');
}
else
{
// sending back with error message.
Session::flash('error', 'uploaded file is not valid');
return Redirect::to('projects/create');
}
}
$input = Request::all();
$project = new Project;
$project->user_id = Auth::id();
$project->title = $input['title'];
//$project->tags = $input['tags'];
$project->summary = $input['summary'];
$project->file_name = $fileName;
$project->published_at = Carbon::now();
$project->save();
return Redirect::to('projects');
}
public function show($id)
{
$input = Request::all();
$project = Project::all()->load("User");
//$project_comments = Comment::where('on_projects', '=', $id)->join('users', 'users.id', '=', 'comments.from_user')->get();
$project_comments = DB::table('comments')
->select('body', 'name')
->where('on_projects', '=', $id)
->join('users', 'users.id', '=', 'comments.from_user')
->get();
return view('projects.show', ['project' => Project::findOrFail($id), 'comments' => $project_comments]);
}
我的评论控制器
public function store()
{
$input = Request::all();
$comment = new Comment;
$comment->body = $input['body'];
$comment->on_projects = $input['project_id'];
$comment->from_user = Auth::user()->id;
$comment->save();
return redirect('projects/'.$input['project_id']);
}
我的项目模型
class Project extends Model
{
protected $fillable = [
'user_id',
'title',
//'tags',
'summary',
'file_name',
'published_at'
];
public function User()
{
return $this->belongsTo('App\User');
}
/**
* Haal de tags op die gerelateerd zijn aan desbetreffende project
*/
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
}
我的评论模型:
class Comment extends Model
{
//comments table in database
protected $guarded = [];
protected $table = 'comments';
public function user()
{
return $this->belongsTo('App\User');
}
// returns post of any comment
public function post()
{
return $this->belongsTo('App\Project','project_id');
}
public $timestamps = false;
}
答案 0 :(得分:3)
我通过重新安排这样的路线来修复它
//Project routes REST methode
Route::post('projects/store', 'ProjectsController@store');
Route::resource('projects', 'ProjectsController');
// add comment
Route::post('projects/{id}','CommentController@store');