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.
208,542
questions
827
votes
40
answers
1.3m
views
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.
...
662
votes
17
answers
1.1m
views
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 ...
601
votes
23
answers
374k
views
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 ...
584
votes
26
answers
1.1m
views
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)
...
486
votes
25
answers
533k
views
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: ...
476
votes
20
answers
1.0m
views
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('...
475
votes
18
answers
768k
views
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.
459
votes
22
answers
311k
views
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 ...
445
votes
37
answers
750k
views
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 ...
416
votes
20
answers
427k
views
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 ...
414
votes
22
answers
724k
views
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 /...
413
votes
27
answers
635k
views
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: http://127.0.0.1:8000
However, it won't automatically launch and when I manually ...
407
votes
32
answers
796k
views
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'];...
403
votes
15
answers
637k
views
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?
401
votes
33
answers
877k
views
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?
377
votes
25
answers
812k
views
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 ...
375
votes
17
answers
405k
views
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 ...
373
votes
2
answers
589k
views
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 ...
358
votes
9
answers
366k
views
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 ...
355
votes
6
answers
301k
views
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 ...
351
votes
13
answers
453k
views
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 ...
343
votes
30
answers
607k
views
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 ...
341
votes
31
answers
528k
views
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" ...
339
votes
11
answers
91k
views
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 ...
338
votes
29
answers
684k
views
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 ...
333
votes
21
answers
537k
views
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->...
327
votes
4
answers
564k
views
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/...
325
votes
15
answers
213k
views
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\...
324
votes
39
answers
446k
views
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] ...
323
votes
32
answers
526k
views
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 ...
322
votes
31
answers
443k
views
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 ...
318
votes
12
answers
431k
views
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('...
318
votes
5
answers
355k
views
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, ...
315
votes
11
answers
540k
views
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::...
306
votes
34
answers
630k
views
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?
299
votes
25
answers
401k
views
"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 ...
297
votes
30
answers
377k
views
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 ...
290
votes
13
answers
337k
views
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");
...
282
votes
11
answers
341k
views
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 ...
278
votes
11
answers
418k
views
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 ...
278
votes
23
answers
329k
views
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 ...
273
votes
22
answers
315k
views
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, ...
269
votes
3
answers
759k
views
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:
->...
267
votes
5
answers
78k
views
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 ...
266
votes
24
answers
710k
views
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
...
264
votes
8
answers
253k
views
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 ...
263
votes
16
answers
491k
views
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 ...
259
votes
22
answers
624k
views
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.
256
votes
18
answers
696k
views
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?
255
votes
41
answers
329k
views
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 ...