Questions tagged [laravel]

The Laravel framework is an open-sourced PHP web framework that allows developers to create dynamic and scalable web applications. The source code of Laravel is hosted on GitHub and released under the MIT license.

Filter by
Sorted by
Tagged with
827 votes
40 answers

How do I get the query builder to output its raw SQL query as a string?

Given the following code: DB::table('users')->get(); I want to get the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM users. ...
meiryo's user avatar
  • 11.2k
662 votes
17 answers

How can I remove a package from Laravel using PHP Composer?

What is the correct way to remove a package from Laravel using PHP Composer? So far I've tried: Remove declaration from file composer.json (in the "require" section) Remove any class ...
igaster's user avatar
  • 13k
601 votes
23 answers

How to create custom helper functions in Laravel

I would like to create helper functions to avoid repeating code between views in Laravel. For example: view.blade.php <p>Foo Formated text: {{ fooFormatText($text) }}</p> They're ...
Calebe Oliveira's user avatar
584 votes
26 answers

How to Create Multiple Where Clause Query Using Laravel Eloquent?

I'm using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it's not elegant. Example: $results = User::where('this', '=', 1) ...
veksen's user avatar
  • 6,863
486 votes
25 answers

Rollback one specific migration in Laravel

I want to rollback only : Rolled back: 2015_05_15_195423_alter_table_web_directories I run php artisan migrate:rollback, 3 of my migration are rolling back. Rolled back: ...
code-8's user avatar
  • 54.9k
476 votes
20 answers

Laravel Add a new column to existing table in a migration

I can't figure out how to add a new column to my existing database table using the Laravel framework. I tried to edit the migration file using... <?php public function up() { Schema::create('...
kim larsen's user avatar
  • 5,121
475 votes
18 answers

How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue?

I'm using VueJS and Laravel for my project. This issue started to show lately and it shows even in the old git branches. This error only shows in the Chrome browser.
3UMF's user avatar
  • 5,212
459 votes
22 answers

Laravel requires the Mcrypt PHP extension

I am trying to use the migrate function in Laravel 4 on OSX. However, I am getting the following error: Laravel requires the Mcrypt PHP extension. As far as I understand, it's already enabled (see ...
Patrick Reck's user avatar
  • 11.3k
445 votes
37 answers

PDOException SQLSTATE[HY000] [2002] No such file or directory

I believe that I've successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrate or php artisan db:seed) I get an error ...
Daniel Hollands's user avatar
416 votes
20 answers

Displaying HTML with Blade shows the HTML code

I have a string returned to one of my views, like this: $text = '<p><strong>Lorem</strong> ipsum dolor <img src="images/test.jpg"></p>' I'm trying to display ...
lesssugar's user avatar
  • 15.5k
414 votes
22 answers

How to set up file permissions for Laravel?

I'm using Apache Web Server that has the owner set to _www:_www. I never know what is the best practice with file permissions, for example when I create new Laravel 5 project. Laravel 5 requires /...
Robo Robok's user avatar
  • 21.2k
413 votes
27 answers

No Application Encryption Key Has Been Specified

I'm trying to use the Artisan command like this: php artisan serve It displays: Laravel development server started: However, it won't automatically launch and when I manually ...
Carlos F's user avatar
  • 4,651
407 votes
32 answers

Get the Last Inserted Id Using Laravel Eloquent

I'm currently using the below code to insert data in a table: <?php public function saveDetailsCompany() { $post = Input::All(); $data = new Company; $data->nombre = $post['name'];...
SoldierCorp's user avatar
  • 7,610
403 votes
15 answers

Laravel Eloquent Query: Using WHERE with OR AND OR?

How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1) For more complicated queries am I supposed to use raw SQL?
Farzher's user avatar
  • 14k
401 votes
33 answers

Laravel Checking If a Record Exists

I am new to Laravel. How do I find if a record exists? $user = User::where('email', '=', Input::get('email')); What can I do here to see if $user has a record?
Ben's user avatar
  • 5,627
377 votes
25 answers

Could not open input file: artisan

When trying to create a new laravel project, The following error appears on the CLI: Could not open input file: artisan Script php artisan clear-compiled handling the post-install-cmd event returned ...
wessodesigner's user avatar
375 votes
17 answers

Laravel - Eloquent or Fluent random row

How can I select a random row using Eloquent or Fluent in Laravel framework? I know that by using SQL, you can do order by RAND(). However, I would like to get the random row without doing a count on ...
DigitalWM's user avatar
  • 4,416
373 votes
2 answers

Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean?

I've found the concept and meaning behind these methods to be a little confusing, is it possible for somebody to explain to me what the difference between has and with is, in the context of an example ...
user avatar
358 votes
9 answers

How to use multiple databases in Laravel

I want to combine multiple databases in my system. Most of the time the database is MySQL; but it may differ in future i.e. Admin can generate such a reports which is use source of heterogeneous ...
Chintan7027's user avatar
  • 7,125
355 votes
6 answers

How to query all the GraphQL type fields without writing a long query?

Assume you have a GraphQL type and it includes many fields. How to query all the fields without writing down a long query that includes the names of all the fields? For example, If I have these ...
BlackSigma's user avatar
  • 3,555
351 votes
13 answers

Eloquent Collection: Counting and Detect Empty

This may be a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(...)->get() is empty, as well as ...
bitinn's user avatar
  • 9,198
343 votes
30 answers

How to Get the Current URL Inside @if Statement (Blade) in Laravel 4?

I am using Laravel 4. I would like to access the current URL inside an @if condition in a view using the Laravel's Blade templating engine but I don't know how to do it. I know that it can be done ...
user2443854's user avatar
  • 3,439
341 votes
31 answers

How to Set Variables in a Laravel Blade Template

I'm reading the Laravel Blade documentation and I can't figure out how to assign variables inside a template for use later. I can't do {{ $old_section = "whatever" }} because that will echo "whatever" ...
duality_'s user avatar
  • 17.8k
339 votes
11 answers

Proper Repository Pattern Design in PHP?

Preface: I'm attempting to use the repository pattern in an MVC architecture with relational databases. I've recently started learning TDD in PHP, and I'm realizing that my database is coupled much ...
Jonathan's user avatar
  • 18.3k
338 votes
29 answers

How to fix Error: laravel.log could not be opened?

I'm pretty new at laravel, in fact and I'm trying to create my very first project. for some reason I keep getting this error (I haven't even started coding yet) Error in exception handler: The stream ...
frankelot's user avatar
  • 13.7k
333 votes
21 answers

Get Specific Columns Using “With()” Function in Laravel Eloquent

I have two tables, User and Post. One User can have many posts and one post belongs to only one user. In my User model I have a hasMany relation... public function post(){ return $this->...
Awais Qarni's user avatar
  • 17.5k
327 votes
4 answers

PHP7 : install ext-dom issue

I'm running laravel 5.4 on Ubuntu 16.04 server with PHP7. trying to install cviebrock/eloquent-sluggable package throw some error: pish@let:/home/sherk/ftp/www$ sudo composer require cviebrock/...
alex's user avatar
  • 7,581
325 votes
15 answers

Laravel 5 - artisan seed [ReflectionException] Class SongsTableSeeder does not exist

When I run php artisan db:seed I am getting the following error: [ReflectionException] Class SongsTableSeeder does not exist What is going on? My DatabaseSeeder class: <?php use Illuminate\...
Sasha's user avatar
  • 8,531
324 votes
39 answers

Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Migration error on Laravel 5.4 with php artisan make:auth [Illuminate\Database\QueryException] ...
abSiddique's user avatar
  • 11.7k
323 votes
32 answers

Error “Target class controller does not exist” when using Laravel 8

Here is my controller: <?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class RegisterController extends Controller { public ...
Nguyễn Việt Dũng's user avatar
322 votes
31 answers

Facebook OAuth "The domain of this URL isn't included in the app's domain"

Let me first start with saying I've searched for an answer to this question for quite some time... I'm trying to setup Facebook OAuth to work with my application that is being developed locally on my ...
dangel's user avatar
  • 7,258
318 votes
12 answers

Laravel Migration Change to Make a Column Nullable

I created a migration with unsigned user_id. How can I edit user_id in a new migration to also make it nullable()? Schema::create('throttle', function(Blueprint $table) { $table->increments('...
user391986's user avatar
  • 29.6k
318 votes
5 answers

How to sort a Laravel query builder result by multiple columns?

I want to sort multiple columns in Laravel 4 by using the method orderBy() in Laravel Eloquent. The query will be generated using Eloquent like this: SELECT * FROM mytable ORDER BY coloumn1 DESC, ...
Sophy's user avatar
  • 8,855
315 votes
11 answers

How do you check "if not null" with Eloquent?

How do you check if a field is not null with Eloquent? I tried Model::where('sent_at', 'IS NOT', DB::raw('null'))->... but it gives IS NOT as a binding instead of a comparison. This is what DB::...
Marwelln's user avatar
  • 28.5k
306 votes
34 answers

Laravel: How to Get Current Route Name? (v5 ... v7)

In Laravel v4 I was able to get the current route name using... Route::currentRouteName() How can I do it in Laravel v5 and Laravel v6?
Md Rashedul Hoque Bhuiyan's user avatar
299 votes
25 answers

"Please provide a valid cache path" error in laravel

I duplicated a working laravel app and renamed it to use for another app. I deleted the vendor folder and run the following commands again: composer self-update composer-update npm install bower ...
user3718908x100's user avatar
297 votes
30 answers

How to remove /public/ from a Laravel URL [duplicate]

I want to remove the /public/ fragment from my Laravel 5 URLs. I don't want to run a VM, this just seems awkward when switching between projects. I don't want to set my document root to the public ...
user1537360's user avatar
  • 4,761
290 votes
13 answers

Laravel Eloquent: Ordering results of all()

I'm stuck on a simple task. I just need to order results coming from this call $results = Project::all(); Where Project is a model. I've tried this $results = Project::all()->orderBy("name"); ...
MatterGoal's user avatar
  • 16.1k
282 votes
11 answers

Add a custom attribute to a Laravel / Eloquent model on load?

I'd like to be able to add a custom attribute/property to an Laravel/Eloquent model when it is loaded, similar to how that might be achieved with RedBean's $model->open() method. For instance, at ...
coatesap's user avatar
  • 10.8k
278 votes
11 answers

Disable Laravel's Eloquent timestamps

I'm in the process of converting one of our web applications from CodeIgniter to Laravel. However at this moment we don't want to add the updated_at / created_at fields to all of our tables as we have ...
projectxmatt's user avatar
  • 3,101
278 votes
23 answers

Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error

After the latest update of PHP Intelephense that I get today, the intelephense keep showing an error for an undefined symbol for my route (and other class too), there is no error like this before and ...
Adrian Edy Pratama's user avatar
273 votes
22 answers

How to place the ~/.composer/vendor/bin directory in your PATH?

I'm on Ubuntu 14.04 and I've been trying all possible methods to install Laravel to no avail. Error messages everything I try. I'm now trying the first method in the quickstart documentation, that is, ...
which1ispink's user avatar
  • 3,018
269 votes
3 answers

Laravel 4: how to "order by" using Eloquent ORM [duplicate]

Simple question - how do I order by 'id' descending in Laravel 4. The relevant part of my controller looks like this: $posts = $this->post->all() As I understand you use this line: ->...
Josh's user avatar
  • 6,009
267 votes
5 answers

What Are the Differences Between PSR-0 and PSR-4?

Recently I've read about namespaces and how they are beneficial. I'm currently creating a project in Laravel and trying to move from class map autoloading to namespacing. However, I can't seem to ...
Varun Nath's user avatar
  • 5,588
266 votes
24 answers

How to select specific columns in laravel eloquent

lets say I have 7 columns in table, and I want to select only two of them, something like this SELECT `name`,`surname` FROM `table` WHERE `id` = '1'; In laravel eloquent model it may looks like this ...
devnull Ψ's user avatar
  • 3,789
264 votes
8 answers

Safely remove migration In Laravel

In Laravel, there appears to be a command for creating a migration, but not removing. Create migration command: php artisan migrate:make create_users_table If I want to delete the migration, can I ...
Globalz's user avatar
  • 4,474
263 votes
16 answers

Laravel - create model, controller and migration in single artisan command

I can create a model and resource controller (binded to model) with the following command php artisan make:controller TodoController --resource --model=Todo I want to also create a migration with ...
arun's user avatar
  • 4,635
259 votes
22 answers

Select Last Row in the Table

I would like to retrieve the last file inserted into my table. I know that the method first() exists and provides you with the first file in the table but I don't know how to get the last insert.
Carlos Suñol's user avatar
256 votes
18 answers

Laravel: Get base URL

Simple question, but the answer seems quite hard to come by. In Codeigniter, I could load the URL helper and then simply do echo base_url(); to get my site's URL. Is there an equivalent in Laravel?
Inigo's user avatar
  • 8,150
255 votes
41 answers

Migration: Cannot add foreign key constraint

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error: [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 ...
user0129e021939232's user avatar

2 3 4 5