laravel-4

Laravel 4: using controller to redirect page if post does not exist - tried but failed so far

Deadly 提交于 2019-12-05 00:08:10
问题 I'm working with Laravel 4, I have a page that shows posts e.g. example.com/posts/1 shows the first post from the db. What I want to do is redirect the page to the index if someone tries to go to a url that doesn't exist. e.g. if there was no post number 6 then example.com/posts/6 should redirect to example.com/posts Here is what I have, is it on track at all? public function show($id) { $post = $this->post->findOrFail($id); if($post != NULL) { return View::make('posts.show', compact('post'))

How to add data to all log records in Laravel?

守給你的承諾、 提交于 2019-12-04 23:42:14
问题 I would like to add some data to all log records within my Laravel application. I think it would be helpful to know the username of the current user and/or the client IP address. Currently I'm adding it manually by doing: Log::info('Pre-paid activation.', array('username' => Auth::user()->username)); But I would like to know how to do add a listener or something to make all log recorde have the username (if available). 回答1: Since Laravel comes out of box with Monolog, it is pretty straight

How can I update data with Eloquent without id in Laravel 4

不打扰是莪最后的温柔 提交于 2019-12-04 23:41:51
When I want to update the FaqTrans database, but this datase have two primary key (faq_ida and lang) $table->primary(array('lang', 'faq_id')); . so I don't need a id column with auto_increment. However, when I use the following code to update the database, the error message hint me there is no id column. $faq_trans = FaqTrans::where('faq_id','=',$faq_id)->where('lang','=',$lang)->first(); $faq_trans->lang = Input::get('lang'); $faq_trans->body = Input::get('body'); $faq_trans->title = Input::get('title'); $faq_trans->save(); error message SQLSTATE[42S22]: Column not found: 1054 Unknown column

Laravel convert an array to a model

孤人 提交于 2019-12-04 23:40:39
i have an array as follows 'topic' => array ( 'id' => 13, 'title' => 'Macros', 'content' => '<p>Macros. This is the updated content.</p> ', 'created_at' => '2014-02-28 18:36:55', 'updated_at' => '2014-05-14 16:42:14', 'category_id' => '5', 'tags' => 'tags', 'referUrl' => '', 'user_id' => 3, 'videoUrl' => '', 'useDefaultVideoOverlay' => 'true', 'positive' => 0, 'negative' => 1, 'context' => 'macros', 'viewcount' => 60, 'deleted_at' => NULL, ) I would like to use this array and convert/cast it into the Topic Model . Is there a way this can be done. thanks Try creating a new object and passing

Attaching a hasOne model to another Laravel/Eloquent model without specifying id

折月煮酒 提交于 2019-12-04 23:39:12
Background Given we have the following two tables where type_id references a row in questionType : question id | type_id | description ---+---------+------------ 1 | 1 | A nice question .. | .. | .. questionType id | name ---+---------------- 1 | Multiple-choice .. | .. with the following Eloquent models: class Question extends Model { public function type() { return $this->hasOne( 'QuestionType', 'id', 'type_id' ); } } class QuestionType extends Model { } Question 1 How can I add a new question that references an existing question type without manually doing anything with ids? For example the

Route [Controller@method] not defined

和自甴很熟 提交于 2019-12-04 23:32:28
I'm trying to create a form with a corresponding controller method which adds a new record to the DB. Laravel Version is 4.1 app/views/projects.blade.php <tr> {{Form::open(array('action' => 'ProjectController@createProject', 'method' => 'post'))}} <td>{{Form::text('project_number')}}</td> <td>{{Form::text('title')}}</td> <td>{{Form::text('client')}}</td> <td>{{Form::text('comment')}}</td> <td> {{Form::file('xmlfile')}}<br /> {{Form::submit('Hinzufügen',array('class' => 'blue'))}} </td> {{ Form::close() }} </tr> app/controllers/ProjectController <?php class ProjectController extends

How to set form input required attribute in laravel 4

两盒软妹~` 提交于 2019-12-04 23:08:24
I'm using the laravel framework for a project, and I'm implementing a basic form page, where I require certain values to be required , something that can be done very easily in HTML5. <input type="text" name="abc" required> In laravel, without the required attribute, the same would be : {{ Form::text('abc') }} How do I incorporate a required attribute in the above statement? Since simply writing ['required'] did not work, I searched online a bit more and found the answer, so I thought I'd share it here. The third parameter is an array of optional attributes, which, in convention, must be

Why does `catch (Exception $e)` not handle this `ErrorException`?

夙愿已清 提交于 2019-12-04 22:53:14
I get the ErrorException on the function call bellow. How can this be? Why is it not caught? try { static::$function_name($url); } catch (Exception $e) {} The underlying reason for the error is a file_put_contents call. I'm using the Laravel 4 framework, if it makes any difference. I suspect that you need to write this: try { static::$function_name($url); } catch (\Exception $e) {} Note the \ in front of Exception. When you have declared a namespace, you need to specify the root namespace in front of classes like Exception, otherwise the catch block here will be looking for \Your\Namespace

Is it possible to pass a route parameter to controller constructor in Laravel?

梦想与她 提交于 2019-12-04 22:48:20
Is it possible to inject a route-paramter (or an route segment) to the controller-constructor? You find some code to clarify my question. class TestController{ protected $_param; public function __construct($paramFromRoute) { $this->param = $paramFromRoute; } public function testAction() { return "Hello ".$this->_param; } } ---------------------------------------------------- App::bind('TestController', function($app, $paramFromRoute){ $controller = new TestController($paramFromRoute); return $controller; }); ---------------------------------------------------- // here should be some magic

How can I get the raw query string from Laravel's query builder BEFORE executing the query?

一个人想着一个人 提交于 2019-12-04 22:33:41
I have a complex query created by a few conditions, and I would like to get the final SQL query from the builder object is about to execute. Can I do that? You can get it doing: $query = DB::table('brands') ->join('products','a','=','c') ->whereNull('whatever'); echo $query->toSql(); But Laravel will not show you parameters in your query, because they are bound after preparation of the query. So you can also do: print_r( $query->getBindings() ); For debugging this might come quite handy as it returns the SQL with the bindings, so you can instantly put it into the database console. /** *