将请求保存到数据库

时间:2017-04-15 15:21:55

标签: php laravel laravel-5

我想将laravel 5.2 app表单中的请求保存到我的数据库中:

当我使用单词testdy提交请求时,我得到一个例外:

  

KeywordController.php第49行中的FatalThrowableError:Class   找不到“App \ Keyword”

我的控制器中的第49行是我想要构建我的模型的时候:$keyword = new Keyword($request);

您可以在下面找到KeywordController的代码:

<?php

namespace App\Http\Controllers;

use App\Keyword;
use App\Http\Requests\KeywordRequest;
use Response;
use Sentinel;

class KeywordController extends JoshController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        // Grab all the Keywords
     //   $Keywords = Keyword::all();
        // Show the page
        return View('admin.keyword.index'); //, compact('Keywords'));
    }

    public function create()
    {
        // $Keywordcategory = KeywordCategory::lists('title', 'id');
        return view('admin.keyword.create'); //, compact('Keywordcategory'));
    }

    public function store(KeywordRequest $request)
    {
        //dd($request);
        $keyword = new Keyword($request); //line 49 of my controller

        $keyword->user_id = Sentinel::getUser()->id;
        $keyword->save();

        if ($keyword->id) {
            return redirect('admin/keyword')->with('success', trans('Keyword/message.success.create'));
        } else {
            return Redirect::route('admin/keyword')->withInput()->with('error', trans('Keyword/message.error.create'));
        }
    }

    public function show(Keyword $keyword)
    {
        $comments = Keyword::all();

        return view('admin.keyword.show', compact('Keyword', 'comments', 'tags'));
    }

    public function edit(Keyword $keyword)
    {
        $Keywordcategory = KeywordCategory::lists('title', 'id');

        return view('admin.keyword.edit', compact('Keyword', 'Keywordcategory'));
    }

    public function update(KeywordRequest $request, Keyword $Keyword)
    {
        if ($request->hasFile('image')) {
            $file = $request->file('image');
            $filename = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension() ?: 'png';
            $folderName = '/uploads/Keyword/';
            $picture = str_random(10).'.'.$extension;
            $Keyword->image = $picture;
        }

        if ($request->hasFile('image')) {
            $destinationPath = public_path().$folderName;
            $request->file('image')->move($destinationPath, $picture);
        }
        $Keyword->retag($request['tags']);

        if ($Keyword->update($request->except('image', '_method', 'tags'))) {
            return redirect('admin/keyword')->with('success', trans('Keyword/message.success.update'));
        } else {
            return Redirect::route('admin/keyword')->withInput()->with('error', trans('Keyword/message.error.update'));
        }
    }

    public function getModalDelete(Keyword $Keyword)
    {
        $model = 'Keyword';
        $confirm_route = $error = null;
        try {
            $confirm_route = route('delete/Keyword', ['id' => $Keyword->id]);

            return View('admin/layouts/modal_confirmation', compact('error', 'model', 'confirm_route'));
        } catch (GroupNotFoundException $e) {
            $error = trans('Keyword/message.error.delete', compact('id'));

            return View('admin/layouts/modal_confirmation', compact('error', 'model', 'confirm_route'));
        }
    }

    public function destroy(Keyword $Keyword)
    {
        if ($Keyword->delete()) {
            return redirect('admin/keyword')->with('success', trans('Keyword/message.success.delete'));
        } else {
            return Redirect::route('admin/keyword')->withInput()->with('error', trans('Keyword/message.error.delete'));
        }
    }
}

我的请求,请参阅dd($request);,如下所示:

KeywordRequest {#323 ▼
  #container: Application {#3 ▶}
  #redirector: Redirector {#330 ▼
    #generator: UrlGenerator {#114 ▼
      #routes: RouteCollection {#26 ▶}
      #request: Request {#40 ▶}
      #forcedRoot: null
      #forceSchema: null
      #cachedRoot: null
      #cachedSchema: null
      #rootNamespace: "App\Http\Controllers"
      #sessionResolver: Closure {#115 ▶}
      #dontEncode: array:14 [▶]
    }
    #session: Store {#264 ▼
      #id: "6db1ef2a6ede612fa681e299c7731866afce7b34"
      #name: "laravel_session"
      #attributes: array:4 [▼
        "_token" => "HbguKauEYhU00n9YcCvw8lloKWbd51I6FOhHRORB"
        "_previous" => array:1 [▶]
        "flash" => array:2 [▶]
        "cartalyst_sentinel" => "5jqJ2R6MTFm4DkOefwhknOD3WY4Jaa0T"
      ]
      #bags: []
      #metaBag: MetadataBag {#255 ▼
        -name: "__metadata"
        -storageKey: "_sf2_meta"
        #meta: &120 array:3 [▼
          "u" => 1492269234
          "c" => 1492243279
          "l" => "0"
        ]
        -lastUsed: 1492269215
        -updateThreshold: 0
      }
      #bagData: array:1 [▶]
      #handler: FileSessionHandler {#265 ▼
        #files: Filesystem {#117}
        #path: "/home/ubuntu/workspace/storage/framework/sessions"
        #minutes: 120
      }
      #started: true
    }
  }
  #redirect: null
  #redirectRoute: null
  #redirectAction: null
  #errorBag: "default"
  #dontFlash: array:2 [▼
    0 => "password"
    1 => "password_confirmation"
  ]
  #json: null
  #convertedFiles: []
  #userResolver: Closure {#232 ▼
    class: "Cartalyst\Sentinel\Laravel\SentinelServiceProvider"
    this: SentinelServiceProvider {#77 …}
    use: {▼
      $app: Application {#3}
    }
    file: "/home/ubuntu/workspace/vendor/cartalyst/sentinel/src/Laravel/SentinelServiceProvider.php"
    line: "425 to 427"
  }
  #routeResolver: Closure {#233 ▼
    class: "Illuminate\Routing\Router"
    this: Router {#24 …}
    use: {▼
      $route: Route {#149 ▼
        #uri: "admin/keyword/create"
        #methods: array:1 [▶]
        #action: array:6 [▶]
        #defaults: []
        #wheres: array:1 [▶]
        #parameters: []
        #parameterNames: []
        #compiled: CompiledRoute {#239 ▶}
        #router: Router {#24 …}
        #container: Application {#3}
      }
    }
    file: "/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
    line: "693 to 695"
  }
  +attributes: ParameterBag {#325 ▼
    #parameters: []
  }
  +request: ParameterBag {#322 ▼
    #parameters: array:2 [▼
      "_token" => "HbguKauEYhU00n9YcCvw8lloKWbd51I6FOhHRORB"
      "keyword" => "tesdy"
    ]
  }
  +query: ParameterBag {#324 ▼
    #parameters: []
  }
  +server: ServerBag {#328 ▼
    #parameters: array:42 [▶]
  }
  +files: FileBag {#327 ▼
    #parameters: []
  }
  +cookies: ParameterBag {#326 ▼
    #parameters: array:2 [▼
      "XSRF-TOKEN" => "HbguKauEYhU00n9YcCvw8lloKWbd51I6FOhHRORB"
      "laravel_session" => "6db1ef2a6ede612fa681e299c7731866afce7b34"
    ]
  }
  +headers: HeaderBag {#329 ▼
    #headers: array:16 [▼
      "host" => array:1 [▶]
      "content-length" => array:1 [▶]
      "cache-control" => array:1 [▶]
      "origin" => array:1 [▶]
      "upgrade-insecure-requests" => array:1 [▶]
      "user-agent" => array:1 [▶]
      "content-type" => array:1 [▶]
      "accept" => array:1 [▶]
      "referer" => array:1 [▶]
      "accept-encoding" => array:1 [▶]
      "accept-language" => array:1 [▶]
      "cookie" => array:1 [▶]
      "x-forwarded-proto" => array:1 [▶]
      "x-forwarded-port" => array:1 [▶]
      "x-forwarded-for" => array:1 [▶]
      "connection" => array:1 [▶]
    ]
    #cacheControl: array:1 [▼
      "max-age" => "0"
    ]
  }
  #content: ""
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: null
  #requestUri: null
  #baseUrl: null
  #basePath: null
  #method: "POST"
  #format: null
  #session: Store {#264 ▶}
  #locale: null
  #defaultLocale: "en"
}

"keyword" => "tesdy"参数在数据库中具有相同的名称。

为什么$keyword = new Keyword($request);会抛出异常的任何建议?

感谢您的回复!

更新

我的app/Keyword.php文件如下所示:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Keywords extends Model
{
    protected $table = 'keywords';

    protected $guarded = ['id'];
}

2 个答案:

答案 0 :(得分:4)

只需将Keyword中的班级名称更改为Keywords。类名应该等于它的文件名。

答案 1 :(得分:4)

模型名称应为Keyword,更改它,这将解决问题:

class Keyword extends Model