laravel-4

Laravel 4: How to add scope to DB::table?

亡梦爱人 提交于 2019-12-05 08:23:21
With Eloquent models adding scopes is easy: public function scopeMyScope($query) { // Do stuff to that $query } But how to add scope to DB::table ? I use this query to get page views: $views = DB::table('page_views') ->where('id', $this->id) ->where('agent', 'NOT LIKE', '%bot%') ->count(DB::raw('distinct session, DATE(created_at)')); I also show the most popular pages etc with other queries, but with the same where conditions. So I would like to only define my where conditions once and reuse them in all other page view DB::table queries. DB::table doesn't have support for scopes. What you

Foreach inside a Form::select in Laravel 4

别等时光非礼了梦想. 提交于 2019-12-05 08:08:25
问题 I try to walk a variable using the helpers of Laravel but it trhows an error, and when I do the same with html tags it works correctly. {{ Form::select('categoria', array( @foreach($enlaceid as $categoria) $categoria->id => $categoria->nombre {{-- I tryed too with englobing with {{ }} )) }} @endforeach <select name="categoria"> @foreach($enlaceid as $categoria) <option value= " {{ $categoria->id }} "> {{$categoria->nombre}} </option> @endforeach </select> 回答1: Use the lists() method and pass

Laravel 4 Relationship: a message belong to two user

帅比萌擦擦* 提交于 2019-12-05 08:07:05
问题 I am making user to user messaging system in laravel 4. So in a simplified version i have to table First table Users user_id | user_name Second table Messages message_id | message_from_user_id | message_to_user_id | message_content | So after reading the documentation I learned that User has many message and message belongs to User. But how do I actually relate them in coding? I am quite confused how will I use the foreign key in this case for a message that will have two user? 回答1: Check

Test logged in user has correct id in Laravel 4

筅森魡賤 提交于 2019-12-05 08:03:57
I'm trying to work out how to test that the logged in user in Laravel 4 can visit the correct user account. At the moment this is what is in my routes.php file. Route::get('user/{id}', array('before' => 'auth', function(){ // logged in user // but can visit any account!! })); How would I restrict this to so user/1 could only see the profile if that is the current logged in users id? Auth::user()->id Returns the logged in id to test against but I can't work out how to write a filter that checks it's equal to the {id} in the url. Please help! Thanks. Got some help through the Laravel irc channel

Multiple select edit form selected values

余生颓废 提交于 2019-12-05 07:56:33
Struggling with an issue in Laravel 4, in a "contact" model edit form, I can get all fields current values except those from the multiple select which is to establish a relation with another model "company". It's a many-to-many relationship. I'm getting the list of companies, but none are selected even if a relation exists. Here's my edit form: {{ Form::model($contact, array('route' => array('crm.contacts.update', $contact->id), 'id' => 'edit-contact')) }} <div class="control-group"> {{ Form::label('first_name', 'First Name', array( 'class' => 'control-label' )) }} {{ Form::text('first_name')

load form in bootstrap modal dynamically using laravel

眉间皱痕 提交于 2019-12-05 07:54:00
I am working on app, which required a form in bootstrap modal and also load the form dynamically. i am facing the problem all of the page is loaded in modal again. Anybody here provide any example for this..? // Controller public function loadJsModalForm() { return View::make('frmModals.frmProject'); } // index.blade @section('Modal') <!-- Modal --> <div class="modal fade" id="ProjectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content" id="frmAddProject"></div> </div> </div> @stop // form Modal {{ Form::open

Laravel: route to storage folder

守給你的承諾、 提交于 2019-12-05 07:50:37
问题 What I'm trying to achieve is to make a route to the storage folder so that I can access it even if it's not under the public directory. For example, user's avatars are located in app\storage\user\avatars\avatar.jpg and I would like to make a route so that I can access those images from something like http://localhost/user/avatars/avatar.jpg . How can I achieve this? 回答1: Firstly, i would recommend moving the avatars folder to somewhere more publicly accessible. But as its Laravel, you can

Can Delayed Queues in Laravel be used as an alternative to CRON jobs

ⅰ亾dé卋堺 提交于 2019-12-05 07:40:22
问题 Laravel 4 has a great list of features in terms of Queues. This question is with respect to the Queue method Queue.later() API Documentation the first parameter being the delay . A Cron is basically made use of to execute recurring tasks. If the below snippet were to be made more generic with the time being configurable as well can: This be used as an alternative to CRON jobs? Would this be fail safe approach to use assuming we use IronMQ - class SendEmail { public function fire($job, $data)

Laravel 4 - custom 404 error handling only for missing pages

倾然丶 夕夏残阳落幕 提交于 2019-12-05 07:15:37
问题 In Laravel 4.1 I want to redirect the user to my customised 404 page if the page is not exists. That's easy because I already have this: App::missing(function($exception) { return Redirect::to('error404', 303)->with(array('error' => array('url' => Request::url()))); } But the problem is that if any linked file (image, js, css, etc.) is missing on the page I don't get 404 error for that file, but this is showing on console: Resource interpreted as Image but transferred with MIME type text/html

Laravel - Artisan gives wrong base url

一笑奈何 提交于 2019-12-05 07:12:45
My app/config/app.php has 'url' => 'http://dev.domain.com/something/somethingElse' Then I have a function that can be called from the application and also from an artisan command. But URL::route('myRoute') returns different results. When called from the application it returns http://dev.domain.com/something/somethingElse/myRoute , but in the artisan command http://dev.domain.com/myRoute . URL::to has same behaviour. Note: I do not have any other app.php file defined for other environments that could overwrite the global one. Any ideas why this happens ? Thanks! A Laravel-generated URL consists