laravel-4

How to update column value in laravel

微笑、不失礼 提交于 2019-12-18 11:11:11
问题 I have a page model. It has following columns in database table: id title image body I want to update only "image" column value. Here's my code: public function delImage($path, $id) { $page = Page::find($id); $page->where('image', $path)->update(array('image' => 'asdasd')); \File::delete($path); } it throws me an error, that i am trying to use where() on a non-object. How can i correctly update my "image" column value? 回答1: You may try this: Page::where('id', $id)->update(array('image' =>

laravel 4 - how to Limit (Take and Skip) for Eloquent ORM?

与世无争的帅哥 提交于 2019-12-18 11:08:05
问题 TL;DR Can you limit an Eloquent ORM query like using take() and skip() so that the resulting mysql query is also limited, and it doesn't have to return the entire dataset? If so, how would you modify: $test = User::find(1)->games->toArray(); To include limit 3 offset 2 ? Tables: users games userGames -- id -- id -- user_id -- name -- name -- game_id -- steam_id Models: class User extends Eloquent { public function games() { return $this->belongsToMany('Game', 'userGames', 'user_id', 'game_id'

Displaying the Error Messages in Laravel after being Redirected from controller

匆匆过客 提交于 2019-12-18 11:03:17
问题 How can I display the validation message in the view that is being redirected in Laravel ? Here is my function in a Controller public function registeruser() { $firstname = Input::get('firstname'); $lastname = Input::get('lastname'); $data = Input::except(array('_token')) ; $rule = array( 'firstname' => 'required', 'lastname' => 'required', ) ; $validator = Validator::make($data,$rule); if ($validator->fails()) { $messages = $validator->messages(); return Redirect::to('/')->with('message',

Displaying the Error Messages in Laravel after being Redirected from controller

别等时光非礼了梦想. 提交于 2019-12-18 11:02:13
问题 How can I display the validation message in the view that is being redirected in Laravel ? Here is my function in a Controller public function registeruser() { $firstname = Input::get('firstname'); $lastname = Input::get('lastname'); $data = Input::except(array('_token')) ; $rule = array( 'firstname' => 'required', 'lastname' => 'required', ) ; $validator = Validator::make($data,$rule); if ($validator->fails()) { $messages = $validator->messages(); return Redirect::to('/')->with('message',

How can I make Laravel return a custom error for a JSON REST API

五迷三道 提交于 2019-12-18 10:33:26
问题 I'm developing some kind of RESTful API. When some error occurs, I throw an App::abort($code, $message) error. The problem is: I want him to throw a json formed array with keys "code" and "message", each one containing the above mentioned data. Array ( [code] => 401 [message] => "Invalid User" ) Does any one knows if it's possible, and if it is, how I do it? 回答1: go to your app/start/global.php . This will convert all errors for 401 and 404 to a custom json error instead of the Whoops

Laravel: Difference App::bind and App::singleton

自古美人都是妖i 提交于 2019-12-18 10:26:19
问题 I get a bit confused over all the nice things laravel has to offer in terms of the IOC container and facades. Since I'm not an experienced programmer it gets overwhelming to learn. I was wondering, what is the difference between these two examples: A facade to 'Foo' and registered in the container via App::bind() A facade to 'Foo' and registered in the container via App::singleton() In my best understanding Foo::method() will be rewritten as $app->make['foo']->method() so in the first example

Saving related records in laravel

左心房为你撑大大i 提交于 2019-12-18 10:16:46
问题 I have users, and users belong to a dealership. Upon user registration, I'm trying to save a new user, and a new dealership. User database has a dealership_id column, which I want to be populated with the ID of the newly created dealership. This is my current code in the UserController store method. public function store() { $user = new User(); $user->email = Input::get('email'); $user->password = Input::get('password'); $dealership = new Dealership(); $dealership->name = Input::get(

Get previous attribute value in Eloquent model event

我的梦境 提交于 2019-12-18 10:15:00
问题 Is there a way to see the old/previous value of a model's attribute in its saving or updating event? eg. Is something like the following possible: User::updating(function($user) { if ($user->username != $user->old->username) doSomething(); }); 回答1: Ok, I found this quite by chance, as it's not in the documentation at present... There is a getOriginal() method available which returns an array of the original attribute values: User::updating(function($user) { $original = $user->getOriginal();

How to organize different versioned REST API controllers in Laravel 4?

你说的曾经没有我的故事 提交于 2019-12-18 10:13:59
问题 I know there's a way to create versioned URLs for REST APIs with routes, but what's the best way to organize controllers and controller files? I want to be able to create new versions of APIs, and still keep the old ones running for at least some time. 回答1: I ended up using namespaces and directories under app/controllers: /app /controllers /Api /v1 /UserController.php /v2 /UserController.php And in UserController.php files I set the namespace accordingly: namespace Api\v1; or namespace Api

Using Laravel Homestead: 'no input file specified'

☆樱花仙子☆ 提交于 2019-12-18 10:00:17
问题 I am new to using Laravel, and Homestead, and would appreciate any help or a point in the right direction. I have successfully been able to get to the "You have arrived" screen when I run "php artisan serve" but when I try to do the same thing via Vagrant, I get "no input file specified". My Homestead.yaml file looks like this: authorize: /Users/me/.ssh/id_rsa.pub keys: - /Users/me/.ssh/id_rsa folders: - map: /Users/me/code/exampleproject to: /home/vagrant/code/exampleproject sites: - map: