0
votes

I am building a project where Users kan upload Projects and other Users can comment on eachothers Projects

Now I was able to upload Projects and build a commenting system but now I am no longer able to upload projects I am only able to comment on projects that were already stored in my db.

Someone knows why I am no longer able to store Projects?

My routes:

// add comment
Route::post('projects/{id}','CommentController@store');
//Project routes REST methode
Route::post('projects/store', 'ProjectsController@store');
Route::resource('projects', 'ProjectsController');

My 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]);

}

My CommentController

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']);

}

My Project model

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();
}
}

My Comment model:

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;
}
1
The problem is that $input['body'] doesn't exists.. Check your view if the input has this name body or other..Svetoslav
Thanks for you comment, but this problem is solved!Moussa Chaabar

1 Answers

3
votes

I fixed it by re-arranging my routes like this

//Project routes REST methode
Route::post('projects/store', 'ProjectsController@store');
Route::resource('projects', 'ProjectsController');
// add comment
Route::post('projects/{id}','CommentController@store');