Laravel 4 Eloquent ORM - Symfony \ Component \ Debug \ Exception \ FatalErrorException Cannot redeclare class WorkProcess

StackOverflow https://stackoverflow.com/questions/20743317

سؤال

Here's the scenario!!!

Schema for (work_processes)

Schema::create('work_processes', function($table){
    $table->increments('id');
    $table->enum('wp_type',array('M','F','D')); //M => Maintenance, F => Field, D => Drilling 
    $table->string('wp_name',50);
    $table->integer('wp_owner_id');
    $table->integer('wp_editor_id');
    $table->text('wp_link');
    $table->enum('wp_frequency',array('B','M','Q')); //B => Bi-Monthly, M => Monthly, Q => Quarterly 
    $table->date('wp_startdate');
    $table->date('wp_enddate');
    $table->enum('wp_active',array('Y','N'));
    $table->timestamp('deleted_at')->nullable();
    $table->timestamps();
});}

Schema for (wp_audit_questions)

Schema::create('wp_audit_questions', function($table){
    $table->increments('id');
    $table->integer('wp_id');
    $table->text('wp_audit_question');
    $table->timestamps();
});

Model 1 as (WorkProcess)

class WorkProcess extends Eloquent
{
    protected $table = 'work_processes';
    protected $guarded = array('id');
    protected $softDelete = true;

    public function wpauditquestions()
    {
        return $this->hasMany('WpAuditQuestion');
    }
}

Model 2 as (WpAuditQuestion)

class WpAuditQuestion extends Eloquent
{
    protected $table = 'wp_audit_questions';
    public function workprocess()
    {
        return $this->belongsTo('WorkProcess', 'wp_id');
    }
}

'Controller as (WorkProcessController)

class WorkProcessController extends BaseController
{
    public function ShowWpAuditQuestionEditForm($wpid)
    {
        $wp = WorkProcess::with(array('wpauditquestions' => function($query){
            $query->where('wp_id', $wpid);
        }))->get();
        return View::make('wpauditquestion')->with(array('edit_mode' => 1, 'wpauditquestion' => $wpauditquestion));
    }
}

'Controller as (WpAuditQuestionController)

class WpAuditQuestionController extends BaseController
{
    public function ShowWPAuditQuestionForm()
    {
        $wpauditquestion = new WpAuditQuestion();
                return View::make('wpauditquestion', compact('wpauditquestion'));
     }
}

Routes.php

//model binding
Route::model('workprocess', 'WorkProcess');
Route::model('wpauditquestion', 'WpAuditQuestion');

Route::get('wpauditquestion/edit/{wpid}', array('uses' => 'WorkProcessController@ShowWpAuditQuestionEditForm', 'as' => 'wpauditquestion.edit'));

Problem:

This script generates this error message. e.g. MY_SERVER/wpauditquestion/edit/1

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Cannot redeclare class WorkProcess

however, when I don't use the get() or any other method like paginate(5) etc, it does dump some data.

I have also tried this but same result.

$wp = WorkProcess::has('wpauditquestions')->get();  

can someone please guide me what wrong I'm doing. I'm using laravel 4.0.7 with WAMP.

Also please guide me how to save this model after editing it e.g. if I have a form like this.

{{ Form::open(array('action' => 'WorkProcessController@PostWorkProcessEditForm', 'method' => 'put')) }}
{{-- Work Process Name --}}
{{ Form::hidden('wp_id') }}
<ol>
@for($i = 0; $i < 5; $i++)
    <p>
        <li> 
            {{ Form::label('wp_audit_question', 'Audit Question') }}
            {{ Form::text('wp_audit_question', Input::old('wp_audit_question')) }}
    </p>
@endfor
</ul>
<p>{{ Form::submit('Submit Form', array('id' => 'Submit_Form')) }}</p>
{{ Form::close() }}  

Thanks and Regards

هل كانت مفيدة؟

المحلول 2

Issue resolved! The problem was the model name 'WorkProcess' was clashing with something as I've my view name as 'workprocess.blade.php' as well. So I changed model name to 'WPModel' and its working fine now.

نصائح أخرى

Did you name your migration work_process which was turned into WorkProcess? It can be that your migration has same class name as your model.

in case you did something like changing a class name.

Let's say you changed the class name of your model from 'users' to 'user',

You could try running the command 'composer dump-autoload'.

It worked for me though my case was not exactly the same as yours.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top