laravel-4

Laravel 4 and Eloquent ORM - How to select the last 5 rows of a table

China☆狼群 提交于 2019-12-22 08:39:25
问题 I'm trying to figure out how to select the last 5 rows of a table to display on a home screen when there might be gaps in the ids. The database this is for has 1,000s of rows and I don't want to have to call all of them to take the last 5 every time I go to my app's home screen. The problem is that rows sometimes are deleted in the database for various reasons so, for example, if the last row's id is 4023 the second to last row's id might be 4020, so I can't just use the length and count

Laravel - How to use a vendor class?

扶醉桌前 提交于 2019-12-22 08:12:10
问题 I want to use Mobile Detect on m routes.php file. I have added the package as a require in composer.json and it's installed in the vendor file. How can I use it now? I tried this answer and no luck because the class wasn't found: Laravel 4 using vendor classes { "name": "laravel/laravel", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "require": { "laravel/framework": "4.2.*", "mobiledetect/mobiledetectlib": "*" }, "autoload": { "classmap": [

Laravel Auth::attempt failing each time

烂漫一生 提交于 2019-12-22 08:10:00
问题 Hi guys I've created a test user on my laravel app. The details are user: joe@gmail.com pass: 123456 When I go through the registration process everything works as expected and an entry is made into the users table of the database Once this is finished I redirect the user to the dashboard. public function postCreate(){ //Rules $rules = array( 'fname'=>'required|alpha|min:2', 'lname'=>'required|alpha|min:2', 'email'=>'required|email|unique:users', 'password'=>'required|alpha_num|between:6,12

How to make less to work with basset in laravel 4?

烂漫一生 提交于 2019-12-22 08:09:15
问题 How to enable Less compiling with basset? My basset config file collection: 'collections' => array( 'application' => function($collection) { $collection->apply('UriRewriteFilter'); $directory = $collection->directory('../app/assets/stylesheets', function($collection) { $collection->add('less/design.less')->apply('Less'); }); $directory->apply('CssMin'); } ); Everything else in config is untouched. In the source code i get this: <link rel="stylesheet" type="text/css" href="http://localhost/Job

Laravel : BadMethodCallException Method [find] does not exist

…衆ロ難τιáo~ 提交于 2019-12-22 06:50:22
问题 when trying to extract some values from the database using the model object User I get the following error : BadMethodCallException Method [find] does not exist Here are my files : Model User <?php use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableInterface; class User extends Eloquent implements UserInterface, RemindableInterface { /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The attributes excluded from the model's

Pass many optional parameters to route in Laravel 4

被刻印的时光 ゝ 提交于 2019-12-22 05:46:11
问题 I need to built URLs like this: http://www.example.com/param1/param2/param3/.../paramN in a search page, user searchs by any possible options so making a URL like it in Laravel would be like this: Route::get('{param1?}/{param2?}/{param3?}/.../{paramN?}', array( ... ) ); Is there any other way? Or maybe pass / as a part of parameter to have this: low_range-1000/high_range-5000/weight-2/height-4/red/ so above line become just one parameter to route. any help? 回答1: well, I found the solution.

How to order by using appended attribute in Laravel

拥有回忆 提交于 2019-12-22 05:37:14
问题 I created an appends attribute in Laravel Model, from the code below. protected $appends = array('total'=>''); And I set the returned value. public function getTotalAttribute(){ return ProductPart::where('product_id',$this->id)->count(); } Then I want to order records from database by using total attribute I tried to use Product::orderBy('total','desc')->get() but it didn't work. Does anybody has some suggestions to this? 回答1: the orderBy takes an actual database field not an appended one try

Self Join in Eloquent

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 05:36:16
问题 How would you write a self join in eloquent? Would I need to define the relationship on the model? Here's my statement: SELECT t2.title FROM products t1, products t2 WHERE t1.id = $id AND t2.color_id = t1.color_id AND t2.id != $id 回答1: You can simply define a relation to itself. public function parent() { return $this->belongsTo(self::class, 'color_id'); } public function children() { return $this->hasMany(self::class, 'color_id'); } 来源: https://stackoverflow.com/questions/30592793/self-join

Multiple select edit form selected values

試著忘記壹切 提交于 2019-12-22 05:09:16
问题 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:

Laravel belongsTo Relationship

故事扮演 提交于 2019-12-22 05:08:12
问题 Ok, I am a little confused with the belongsTo relationship for Models. I have a Feeds model that extends Eloguent. I have created a relationship function called User. public function user(){ return $this->belongsTo('User'); // and I also tried return $this->belongsTo('User', 'user_id'); } On the view I am trying to do : @foreach($feeds as $feed) {{$feed->user()->first_name}} {{$feed->user()->last_name}} @endforeach but I am getting this error Undefined property: Illuminate\Database\Eloquent