简体   繁体   中英

Laravel Eloquent Model - can't access database

I'm trying to write a simple site that can write and read of a simple mysql database, using Laravel, but I've run into a full stop as Laravel doesn't seem to be recognising my model. Either that or I am doing something wrong.

My model:

class Submission extends Eloquent
{
    public static $timestamps = true;
    protected $fillable = array('location', 'twitter', 'instagram', 'website');
}

My form:

@extends('layout')

@section('content')
{{ Input::old('twitter') }} <br />
{{ Input::old('instagram') }} <br />
{{ Input::old('website') }} <br />
{{ Form::open(array('url' => '/submission', 'files' => true)) }}
    <div class="form-group"> 
        {{ Form::label('twitter', 'Twitter') }}
        {{ Form::text('twitter', '', array('class' => 'form-control')) }}
    </div>
    <div class="form-group"> 
        {{ Form::label('instagram', 'Instagram') }}
        {{ Form::text('instagram', '', array('class' => 'form-control')) }}
    </div>
    <div class="form-group"> 
        {{ Form::label('website', 'Website') }}
        {{ Form::text('website', '', array('class' => 'form-control')) }}
    </div>
    {{ Form::button('Submit image', array('class' => 'btn btn-success', 'type' => 'submit')) }}
    <input type="hidden" name="post" value="yes">
{{ Form::close() }}
@stop

My controller:

public function postView()
    {
        $submission = new Submission;
        $submission->twitter = Input::get('twitter');
        $submission->instagram = Input::get('instagram');
        $submission->website = Input::get('website');

        $submission->save();

        return Redirect::to('submission')->withInput();
    }

My database looks like: id location twitter instagram website created_at updated_at

I know that my database config is correct as I can retrieve information using DB::table('submissions')->get(); so from what I can tell it's Laravel that's not recognising my model?

EDIT: Turns out that changing public static $timestamps = true; to public $timestamps = true; fixed it.

Does this works :

DB::table('submissions')->get();

as it should return everything in the table. If it works then Eloquent can't find your table you can try to put that in your model:

protected $table = 'submissions';

It will define explicitly the table name in the model, even if it seems correct in your case

Otherwise you need to tell what exactly laravel answers when you made a request.

  • Do you have an error message in your browser ?
  • Do you have an error message in your log file (check the app/storage/logs folder)
  • Is your database configuration ok ? (app/config/database.php)
  • Did you create the table using a laravel migration ?

Hope it helps

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