简体   繁体   中英

“Class 'Quote_Tag' not found” Laravel Eloquent Many to Many

I have a simple laravel 4 application which has 3 MySql tables: quote, quote_tag, and tag. I am trying to add a tag for a specific quote using Eloquent. Once I submit the form with the new tag, my store function seemingly fails to create the associative entity Eloquent model, Quote_Tag, and returns the error message:

"Class 'Quote_Tag' not found"

What am I doing wrong here?

Routes.php:

Route::get('/{idQuote}/tag', 'TagController@create');
Route::post('/{idQuote}/tag', 'TagController@store');

TagController.php:

public function create($idQuote)
    {
        $quote = Quote::find($idQuote);
        return View::make('tags.create')->with('quote', $quote);
    }
public function store($idPersona, $idQuote)
    {
        $tag = new Tag;
        $tag->tagText = Input::get('tagText');
        $tag->save();

        $quote_tag = new Quote_Tag;
        $quote_tag->idTag = $tag->idTag;
        $quote_tag->idQuote = $idQuote;
        $quote_tag->save();

        $tags = Tag::all();
        $quote_tags = Quote_Tag::all();
        return View::make('tags.index')->with('tags', $tags)->with('quote_tags', $quote_tags);
    }

views/tags/create.blade.php:

{{ Form::open([ 'url' => '{/{idQuote}/tag']) }} 
        {{ Form::label('tagText', 'Tag Text: ') }}
        {{ Form::text('tagText') }}
        {{ Form::label('idQuote', 'Quote ID: ') }}
        {{ Form::text('idQuote', $quote->idQuote) }}
        {{ Form::submit('Submit Tag') }}</div>
{{ Form::close() }}

views/tags/index.blade.php:

@foreach($tags as $tag)
        <li>{{ $tag }}</li>
@endforeach
@foreach($quote_tags as $quote_tag)
        <li>{{ $quote_tag }}</li>
@endforeach

models/Quote.php:

class Quote extends Eloquent {
    public $timestamps = false;
    protected $table = 'quote';
    protected $primaryKey = 'idQuote';

    public function tags()
    {
        return $this->belongsToMany('Tag', 'quote_tag', 'idQuote', 'idTag');
    }
}

models/Quote_Tag.php:

class Quote_Tag extends Eloquent {
    public $timestamps = false;
    protected $table = 'quote_tag';
    protected $primaryKey = 'idQuote_Tag';

    public function quote()
    {
        return $this->hasOne('Quote', 'idQuote');
    }
    public function tag()
    {
        return $this->hasOne('Tag', 'idTag');
    }
}

models/Tag.php:

class Tag extends Eloquent {
    public $timestamps = false;
    protected $table = 'tag';
    protected $primaryKey = 'idTag';

    public function quotes()
    {
        return $this->belongsToMany('Quote', 'quote_tag', 'idQuote', 'idTag');
    }
}

MySQL tables:

CREATE TABLE `mountain`.`quote` (
  `idQuote` INT NOT NULL AUTO_INCREMENT,
  `quoteText` TEXT NOT NULL,
  `quoteSource1` VARCHAR(100) NULL,
  `quoteSource2` VARCHAR(100) NULL,
  `idPersona` INT NULL,
  PRIMARY KEY (`idQuote`),

CREATE TABLE `mountain`.`tag` (
  `idTag` INT NOT NULL AUTO_INCREMENT,
  `tagText` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idTag`));

CREATE TABLE `mountain`.`quote_tag` (
  `idQuote_Tag` INT NOT NULL AUTO_INCREMENT,
  `idQuote` INT NOT NULL,
  `idTag` INT NOT NULL,
  PRIMARY KEY (`idQuote_Tag`),
  INDEX `idTag_idx` (`idTag` ASC),
  INDEX `idQuote_idx` (`idQuote` ASC),
  CONSTRAINT `idTag`
    FOREIGN KEY (`idTag`)
    REFERENCES `mountain`.`tag` (`idTag`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `idQuote`
    FOREIGN KEY (`idQuote`)
    REFERENCES `mountain`.`quote` (`idQuote`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

@thesingularity

Do a

composer dump-autoload -o

because your error message says "Class 'Quote_Tag' not found" and your declarations looks correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM