Laravel Check If Related Model Exists

In php 7.2+ you can’t use count on the relation object, so there’s no one-fits-all method for all relations. Use query method instead as @tremby provided below: $model->relation()->exists() generic solution working on all the relation types (pre php 7.2): if (count($model->relation)) { // exists } This will work for every relation since dynamic properties return … Read more

laravel throwing MethodNotAllowedHttpException

You are getting that error because you are posting to a GET route. I would split your routing for validate into a separate GET and POST routes. New Routes: Route::post(‘validate’, ‘MemberController@validateCredentials’); Route::get(‘validate’, function () { return View::make(‘members/login’); }); Then your controller method could just be public function validateCredentials() { $email = Input::post(’email’); $password = Input::post(‘password’); … Read more

How to select from subquery using Laravel Query Builder?

In addition to @delmadord’s answer and your comments: Currently there is no method to create subquery in FROM clause, so you need to manually use raw statement, then, if necessary, you will merge all the bindings: $sub = Abc::where(..)->groupBy(..); // Eloquent Builder instance $count = DB::table( DB::raw(“({$sub->toSql()}) as sub”) ) ->mergeBindings($sub->getQuery()) // you need to … Read more

Migration: Cannot add foreign key constraint

Add it in two steps, and it’s good to make it unsigned too: public function up() { Schema::create(‘priorities’, function($table) { $table->increments(‘id’, true); $table->integer(‘user_id’)->unsigned(); $table->string(‘priority_name’); $table->smallInteger(‘rank’); $table->text(‘class’); $table->timestamps(‘timecreated’); }); Schema::table(‘priorities’, function($table) { $table->foreign(‘user_id’)->references(‘id’)->on(‘users’); }); }

How Can I Remove “public/index.php” in the URL Generated Laravel?

Option 1: Use .htaccess If it isn’t already there, create an .htaccess file in the Laravel root directory. Create a .htaccess file your Laravel root directory if it does not exists already. (Normally it is under your public_html folder) Edit the .htaccess file so that it contains the following code: <IfModule mod_rewrite.c> RewriteEngine On RewriteRule … Read more

Laravel requires the Mcrypt PHP extension

Do you have MAMP installed? Use which php in the terminal to see which version of PHP you are using. If it’s not the PHP version from MAMP, you should edit or add .bash_profile in the user’s home directory, that is : cd ~ In .bash_profile, add following line: export PATH=/Applications/MAMP/bin/php/php5.4.10/bin:$PATH Edited: First you should … Read more