laravel-5.1

Create new eloquent instance with relationships

扶醉桌前 提交于 2020-01-15 03:40:14
问题 How do I create a new instance of an eloquent model with relationships. This is what I am trying: $user = new User(); $user->name = 'Test Name'; $user->friends()->attach(1); $user->save(); But I get Call to undefined method Illuminate\Database\Query\Builder::attach() 回答1: Try to attach friend after save since the attach() method needs an ID to exist on the parent model. ID's aren't (usually) generated until model is saved (when a primary key or other identifier for that model is created in

Laravel 5: The difference between Auth::guard($this->getGuard())->login($user); and auth()->login($user);

旧巷老猫 提交于 2020-01-14 22:54:04
问题 What is the difference between: Auth::guard($this->getGuard())->login($user); and auth()->login($user); ? For example, in PasswordController.php we can have: protected function resetPassword($user, $password) { $user->password = $password; $user->save(); Auth::guard($this->getGuard())->login($user); } or protected function resetPassword($user, $password) { $user->password = $password; $user->save(); auth()->login($user); } (in this case, we create a mutator in Users.php to bcrypt password AND

Load in models dynamically in Laravel 5.1

三世轮回 提交于 2020-01-14 18:44:27
问题 I'm new to Laravel and frameworks in general, and am having trouble with something I assume has an easy answer I'm building an admin panel, and I want to load in tables based on the route given. In my Routes file I have: Route::get('/admin/{table}', 'AdminController@table'); In my AdminController I have: public function table() { if (file_exists(app_path() . '/' . $table . '.php')){ $data = $table::all(); } else { abort(404); } return view('admin.pages.' . $table, compact($data)); } When I go

Load in models dynamically in Laravel 5.1

懵懂的女人 提交于 2020-01-14 18:43:11
问题 I'm new to Laravel and frameworks in general, and am having trouble with something I assume has an easy answer I'm building an admin panel, and I want to load in tables based on the route given. In my Routes file I have: Route::get('/admin/{table}', 'AdminController@table'); In my AdminController I have: public function table() { if (file_exists(app_path() . '/' . $table . '.php')){ $data = $table::all(); } else { abort(404); } return view('admin.pages.' . $table, compact($data)); } When I go

Proper laravel storage permissions

本秂侑毒 提交于 2020-01-13 14:57:08
问题 I have set my local laravel 5 storage folder to permissions 755 and to user www-data , as I use apache2 on my machine. However I was getting blank screens instead of stack traces on errors so I changed the permissions to 777 which resolved the issue. However I feel this is a (terrible) bandaid as really it is allowing any user to modify this directory with all permissions rather than the correct user with limited permissions. I don't know if this issue will affect the development or

Before executing Action in Laravel 5.1

随声附和 提交于 2020-01-13 09:18:08
问题 I am writing the below code in store and update method: $v = Validator::make($request->all(), [ 'field' => 'required|max:100|min:5' ]); if ($v->fails()) { return redirect('route name') ->withErrors($v) ->withInput(); } Is there any inbuilt action method that executes before executing any action method ? if so, is it valid for individual action method or for the controller? 回答1: You may use a middleware or override callAction , https://laravel.com/api/5.6/Illuminate/Routing/Controller.html

Laravel 5.1 AWS SDK proper credentials setup: exception 'Aws\Exception\CredentialsException'

独自空忆成欢 提交于 2020-01-13 06:31:12
问题 I am working on a Laravel 5.1 App Using this package: "aws/aws-sdk-php-laravel": "~3.0" I am trying to properly setup a local environment and a production environment. I keep getting this error when trying to send mail on production server: (My .env file is gitignored and only exists locally) production.ERROR: exception 'Aws\Exception\CredentialsException' with message 'Error retrieving credentials from the instance profile metadata server. Ran php artisan vendor:publish My .env file look

form validation exception not catching by Exception in laravel 5.1?

戏子无情 提交于 2020-01-07 06:49:08
问题 In laravel5, I have catching all error at app/Exceptions/Handler@render function and it was working fine. code given below, public function render($request, Exception $e) { $error_response['error'] = array( 'code' => NULL, 'message' => NULL, 'debug' => NULL ); if ($e instanceof HttpException && $e->getStatusCode() == 422) { $error_response['error']['code'] = 422; $error_response['error']['message'] = $e->getMessage(); $error_response['error']['debug'] = null; return new JsonResponse($error

Laravel - inserting foreign keys causes Integrity constraint violation

梦想与她 提交于 2020-01-07 04:44:06
问题 I have two related models Domain and Server . I'm trying to insert data to my tables using a form. here is my store function : public function store(Request $request, Domain $domain, Server $server) { $domain->create($request->all()); $server->domain()->associate($domain); $server->save(); $server->create($request->only(['srv_hostname','srv_ip','srv_port'])); return redirect()->route('domains.index'); } the table servers has a FK domain_id that points to the PK domain.id Once I submit my form

laravel upload files in many inputs

混江龙づ霸主 提交于 2020-01-07 04:38:06
问题 I'm trying to upload files in 4 inputs files i get the solution from here but the problem the last file4 input file uploaded in all fields in database in my blade form {!! Form::file('file1', null,['class'=>'form-control']) !!} {!! Form::file('file2', null,['class'=>'form-control']) !!} {!! Form::file('file3', null,['class'=>'form-control']) !!} {!! Form::file('file4', null,['class'=>'form-control']) !!} in my controller $input = $request->all(); $files =[]; if ($request->file('file1'))