dailyadd dailyadd - 2 months ago 6
PHP Question

Cant perform UPDATE operation - To edit a project

I cannot edit a project which im creating like this in my controller:

public function newProject(Request $request)
{
$data = $request->all();

$attributes = [];
$attributes['title'] = $data['title'];
$attributes['start_date'] = date("Y-m-d h:i:s", strtotime($data['start_date']));
$attributes['end_date'] = date("Y-m-d h:i:s", strtotime($data['end_date']));
$attributes['created_by'] = Auth::user()->id;
$attributes['description'] = $data['description'];
$attributes['air'] = '10';
$attributes['water'] = '19';
$attributes['lat'] = $data['lat'];
$attributes['lng'] = $data['lng'];


// var_dump($attributes);
// return;

$project = Projects::create($attributes);

if($project)
return redirect('home')->with('success', 'Project added successfully');

var_dump($data);
return;
}


Now, i want to edit the project that i created with the above function. I tried the same procedure just making changes to the route which is:

Route::post('projects/new', [
'uses' => 'ProjectsController@newProject',
'as' => 'projects.new',
]);


note: This is my function and route that create a project. ANY HINTS HOW TO EDIT?

UPDATE:

MY BLADE TEMPLATE FILE

@extends('layouts.app')

@section('content')
<div class="container">

<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">Add Project</div>


<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('projects.update') }}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<label for="title" class="col-md-4 control-label">Title</label>

<div class="col-md-7">
<input id="title" type="text" class="form-control" name="title" value="{{ $project->title }}">

@if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">Start Date</label>

<div class="col-md-7">
<input id="date" type="date" class="form-control" name="start_date" value="{{ $project->start_date }}">

@if ($errors->has('date'))
<span class="help-block">
<strong>{{ $errors->first('date') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
<label for="date" class="col-md-4 control-label">End Date</label>

<div class="col-md-7">
<input id="date" type="date" class="form-control" name="end_date" value="{{ $project->end_date }}">

@if ($errors->has('date '))
<span class="help-block">
<strong>{{ $errors->first('date') }}</strong>
</span>
@endif
</div>
</div>


<div class="form-group{{ $errors->has('text') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">Description</label>
<div class="col-md-7">
<textarea class="form-control" rows="3" name="description" >{{ $project->description }} </textarea>

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('textarea') }}</strong>
</span>
@endif
</div>
</div>

<label class="col-md-7 control-label"></label>

<span>
<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample" >
Air
</a>
</span>
<span>
<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample2" aria-expanded="false" aria-controls="collapseExample2">
Water
</a>
</span>

<span>
<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample3" aria-expanded="false" aria-controls="collapseExample">
Location
</a>
</span>



<div class="collapse" id="collapseExample">
<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-4 control-label">Temperature</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="temperature" class="form-control" placeholder=" °C" value="{{$project->temperature}}">
</div>


</div>
</div>




<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-4 control-label">Radiation</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="radiation" class="form-control" placeholder=".col-xs-1" vlaue="{{$project->radiation}}">
</div>


</div>
</div>




<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-4 control-label">Dust</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="dust" class="form-control" placeholder=".col-xs-1" value="{{$project->dust}}">
</div>


</div>
</div>


<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-4 control-label">Noise</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="noise" class="form-control" placeholder=".col-xs-1" value="{{$project->noise}}">
</div>


</div>
</div>

</div>

<p> </p>

<div class="collapse" id="collapseExample2">
<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Natrium</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="natrium" class="form-control" placeholder=".col-xs-1" value="{{$project->natrium}}">
</div>


</div>
</div>




<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Phosporus</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="phosporus" class="form-control" placeholder=".col-xs-1" value="{{$project->phosphorus}}">
</div>


</div>
</div>



<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Arsinic</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="arsinic" class="form-control" placeholder=".col-xs-1" value="{{$project->arsinic}}">
</div>


</div>
</div>



<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Barium</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="barium" class="form-control" placeholder=".col-xs-1" value="{{$project->barium}}">
</div>


</div>
</div>


<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Cadmium</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="cadmium" class="form-control" placeholder=".col-xs-1" value="{{$project->cadium}}">
</div>


</div>
</div>


<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Mercury</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="mercury " class="form-control" placeholder=".col-xs-1" value="{{$project->mercury}}">
</div>


</div>
</div>


<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Selenium</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="selenium" class="form-control" placeholder=".col-xs-1" value="{{$project->selenium}}">
</div>


</div>
</div>



<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Nickel </label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="nickel" class="form-control" placeholder=".col-xs-1" value="{{$project->nickel}}">
</div>


</div>
</div>



<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Thallium</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="thallium" class="form-control" placeholder=".col-xs-1" value="{{$project->thallium}}">
</div>


</div>
</div>




<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Berillyum</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="berillyum" class="form-control" placeholder=".col-xs-1" value="{{$project->berillyum}}">
</div>


</div>
</div>




<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Phenols</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="phenols" class="form-control" placeholder=".col-xs-1" value="{{$project->phenols}}">
</div>


</div>
</div>



<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Oil</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="oil" class="form-control" placeholder=".col-xs-1" value="{{$project->oil}}">
</div>


</div>
</div>

<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Temperature</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="w_temperature" class="form-control" placeholder="°C" value="{{$project->w_temperature}}">
</div>


</div>
</div>


<div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
<label for="text" class="col-md-7 control-label">Oxygen</label>
<div class="row">
<div class="col-xs-1">
<input type="text" name="oxygen" class="form-control" placeholder="mg/L" value="{{$project->oxygen}}">
</div>


</div>
</div>


</div>



<label for="text" class="col-md-4 control-label">Location</label>
<div class="row">
<div class="col-md-7">

<h1>Location</h1>
<p></p>

<!--map div-->
<div id="map"></div>


<!--our form-->
<h2 class="text-left">Coordinates</h2>
<p>
<input class="form-control" type="text" id="lat" name="lat" value="{{$project->lat}}" />
<input class="form-control" type="text" id="lng" name="lng" value="{{$project->lng}}"/>
</p>


</div>


</div>



<div class="form-group">
<div class="col-md-7 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i> Add Project
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>



</div>

@endsection
@section('footer')

@endsection

Answer

I would recommend going the simple way

Controller function

public function update($id, Request $request)
{
    $data = $request->all();

    $project = Projects::findOrFail($id);
    $project->update($data);

    return redirect(/home);

}

Route

Route::patch('projects/{id}', [  // this is the project id 
    'uses' => 'ProjectsController@update',
    'as' => 'projects.update',
]);

Form (assuming you are using laravel collective forms)

{!! Form::model($project, [
    'method' => 'PATCH',
    'url' => ['/project', $project->id], // this is where you submit the id of the project 
    'class' => 'form-horizontal',
    'files' => true
]) !!}

                <div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
            {!! Form::label('name', 'Name', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::text('name', null, ['class' => 'form-control']) !!}
                {!! $errors->first('name', '<p class="help-block">:message</p>') !!}
            </div>
        </div>


    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-3">
            {!! Form::submit('Update', ['class' => 'btn btn-primary form-control']) !!}
        </div>
    </div>
{!! Form::close() !!}

This is the best way to go! i wont recommend using the same function unless you are sure what you want.

UPDATE

 <form class="form-horizontal" role="form" method="POST" action="{{ url('projects', $project->id) }}"> 

    {{ csrf_field() }}
 <input name="_method" type="hidden" value="PATCH"> // add this
// here you put the rest of the  form

</form>

if you dont have this $project variable available than please show me the function that shows the edit page because i need to know how you will pass the id of the project you want to update (user input or not)