laravel-4

Laravel 4: reading cookies set by javascript

梦想与她 提交于 2019-12-18 19:12:11
问题 If I set a cookie with javascript, how would I read it using Laravel 4? The reason I'm asking is that the docs say: All cookies created by the Laravel framework are encrypted and signed with an authentication code, meaning they will be considered invalid if they have been changed by the client. 回答1: Just use the native PHP command to retrieve cookies: $_COOKIE['cookie']) Or perhaps you can set the cookie via an AJAX command (rather than JS doing it itself) - and have Laravel set the cookie

Laravel 4 adding numbers from foreach loop when count variable is supplied?

梦想与她 提交于 2019-12-18 19:05:28
问题 I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like: $cat[0]['id'] = 1; $cat[0]['name'] = 'First Category'; And so on. In my blade template I have the following code: {{ $i=0 }} @foreach($cats as $cat) {{ $cat['name'] }}<br /> {{ $i++ }} @endforeach Which outputs: 0 First Category 1 Second Category 2 Third Category Notice the numbers preceding the category name. Where are

Laravel Blade: @endsection vs @stop

北战南征 提交于 2019-12-18 18:49:39
问题 In Laravel Blade, we can basically do this: @section('mysection') @endsection @section('mysection') @stop What is the difference between @stop and @endsection ? 回答1: The @endsection was used in Laravel 3 and it was deprecated in Laravel 4 In the Laravel 4 to end a section you have to use @stop You can refer the Changelog here http://wiki.laravel.io/Changelog_%28Laravel_4%29#Blade_Templating 回答2: Authoritative answer by Taylor Otwell @endsection became @stop in L4, just as @yieldSection became

Laravel - multi-insert rows and retrieve ids

拟墨画扇 提交于 2019-12-18 16:30:33
问题 I'm using Laravel 4, and I need to insert some rows into a MySQL table, and I need to get their inserted IDs back. For a single row, I can use ->insertGetId() , however it has no support for multiple rows. If I could at least retrieve the ID of the first row, as plain MySQL does, would be enough to figure out the other ones. 回答1: As user Xrymz suggested, DB::raw('LAST_INSERT_ID();') returns the first. According to Schema api insertGetId() accepts array public int insertGetId(array $values,

Difference between select() and get() in laravel eloquent

我怕爱的太早我们不能终老 提交于 2019-12-18 16:30:31
问题 Is there any Difference between get() and select() method when using laravel eloquent model. Which method is faster? 回答1: Yes there is a difference. select() is only for defining which columns you want. get() is for actually getting the result (> executing the query) and it also allows you to specify columns. DB::table('foo')->select(array('bar')); Will not execute anything. You still need get() for that DB::table('foo')->select(array('bar'))->get(); Now you receive a result with only the bar

Custom Email Headers in Laravel 4

 ̄綄美尐妖づ 提交于 2019-12-18 16:10:33
问题 I can't seem to locate the method within the Laravel 4 docs/Email API where I can add custom headers to an email. For example: Mail::send('emails.welcome', $data, function($message) { $message->to('foo@example.com', 'John Smith')->subject('Welcome!'); $message->headers('X-Tags', 'tag1 tag2 tag3'); }); Does anyone know how this can be done? 回答1: As far as I know there's no way to add custom headers without reaching in to Swift Mailer. Try something like this. $message->getSwiftMessage()-

Laravel 4 - Trouble overriding model's save method

人盡茶涼 提交于 2019-12-18 15:51:39
问题 I'm trying to override my Post class's save() method so that I can validate some of the fields that will be saved to the record: // User.php <?php class Post extends Eloquent { public function save() { // code before save parent::save(); //code after save } } When I try and run a this method in my unit testing I get the following error: ..{"error":{"type":"ErrorException","message":"Declaration of Post::save() should be compatible with that of Illuminate\\Database\\Eloquent\\Model::save()",

Laravel 4 - Trouble overriding model's save method

删除回忆录丶 提交于 2019-12-18 15:51:22
问题 I'm trying to override my Post class's save() method so that I can validate some of the fields that will be saved to the record: // User.php <?php class Post extends Eloquent { public function save() { // code before save parent::save(); //code after save } } When I try and run a this method in my unit testing I get the following error: ..{"error":{"type":"ErrorException","message":"Declaration of Post::save() should be compatible with that of Illuminate\\Database\\Eloquent\\Model::save()",

Eloquent relations - attach (but don't save) to Has Many

我们两清 提交于 2019-12-18 14:38:33
问题 I have the following relations set up: class Page { public function comments() { return $this->hasMany('Comment'); } } class Comment { public function page() { return $this->belongsTo('Page'); } } Pretty bog standard. One page can have many comments, and one comment belongs to a single page. I'd like to be able to create a new page: $page = new Page; and a comment $comment = new Comment; and attach the comment to the page, without saving any of it $page->comments->associate($comment); I've

Returning an Eloquent model as JSON in Laravel 4

蓝咒 提交于 2019-12-18 13:54:15
问题 How do you return an Eloquent model to the browser as JSON? What is the difference between the two methods below? Both seems to work. #1: return Response::json($user->toArray()); #2: return $user->toJson(); 回答1: The actual data sent is the same, however... #1 Sends Content-Type:application/json to the browser #2 Sends Content-Type:text/html #1 is more correct but it depends on your Javascript, see: What is the correct JSON content type? However, it is much simpler to just return the model. It