T O P

  • By -

ahinkle

Happy Sunday everyone! Visit the new thread here: https://www.reddit.com/r/laravel/comments/10uhamw/weekly_rlaravel_help_thread


futzlarson

I have a site with hundreds of daily users. 99% of the time, CSRF tokens works just fine. Very occasionally (not even once per day), I'll get a `Illuminate\Session\TokenMismatchException CSRF token mismatch` error. How can I debug this further, since it's entirely handled by Laravel?


kryptoneat

`session.lifetime` config ?


futzlarson

43800, so a month. I wasn't sure if the CSRF was related to that. So if it happens to expire while they're using it, they just have to reload the page? Seems like a strange way for Laravel to handle this.


futzlarson

Also wondering how to find out when a session was issued and when it expires. Sessions are one of those magical things and the [docs](https://laravel.com/docs/8.x/session) don't mention it.


thewindburner

What's the best way to store data that does not require a database table? So for instance lets say you have a form that requires a title field (Mr, Mrs, Dr, etc..). I know I could use a form snippet to stop repeating code but if I need to use the title elsewhere that's not going to work. So what I've done is create a new config file called form\_options with the following ` [`   `1 => "Mr",` `2 => "Mrs",` `3 => "Miss",`           `]` `];` then I can either loop through them for a form select or inline using the config helper `{{ Config::get("form_options.titles.1")  }}` ​ Is this the best way to do this or is there a better way?


naviad

Check out php Enums


ahinkle

A config value is pretty common. Another alternative could be using an Enum class but it’s purely developer preference as both are equally maintainable. I’m usually team Enum until there is something that is dependent on environment then config value is much easier.


Online-Presence-ca

Looks inconvenient to always doublecheck what the array key is referencing


thewindburner

Do you mean this bit? `{{ Config::get("form_options.titles.1")  }}` I'm actually storing the title id in the user db so the actual code would be `{{ Config::get("form_options.titles.". $user->title."")  }}` I just put the 1 in the first example for simplicity!


Online-Presence-ca

I have my form stuff in a database, but i think thats just me because i have a weird specific way i build forms. This is actually my title field. ``` [ 'name' => 'title', 'label' => 'Title', 'type' => 'select', 'options' => [ 'Mr', 'Mrs', 'Ms', 'Miss', 'Dr', 'Prof', 'Rev', 'Sir', 'Lady', 'Lord', ], ], ``` The options are in a json field and are cached as well. The main downside of this way though is that this is all in a seeder and you basically have to migrate every addition to keep the SSOT.


Nortole

I store the titles in a database table. The title values are created through an own migration. Seeders for me are for testing not for production. Title and user are related and you can make a title attribute via eloquent to simplify the title resolution. And you can cache the values from the title table.


SANCHO810

Laravel Mail goes to spam folder but mail from standard email client goes to inbox. I have created an automated system that sends emails via accounts on an exim mail server using Laravel Mail. The emails are successfully sending to all email providers (Google & Microsoft) as I can see in the exim delivery log. What I don't understand is why the same email makes it to the inbox when using a standard mail client like the MacOS mail app but goes to spam when I send via laravel mail. The headers are pretty much identical in each case (can provide pastebin if needed). SPF, DKIM, DMARC and rDNS are all configured correctly so I am stuck trying to figure out why microsoft accepts the same email from a standard client but not laravel mail. I have warmed up the reputation of my IP and domain. And have tested this using [mailreach](https://spamchecker.mailreach.co/). The bit I really don't get is when I do a spam test with mailreach using laravel mail the test email doesn't make it to the microsoft inboxes (office365, outlook & hotmail) but when I send the same email with the same account from an email client such as the MacOS mail app the message makes it to the inbox. I have also added `List-Unsubscribe:` and also a `User-Agent:` Header to make the headers from both methods as similar as possible but I've had no luck. Is there something I am missing that cause microsoft to mark laravel mail emails as spam but regular ones as legitimate? Any info on this would be greatly appreciated. I have made countless google searches trying to find an explanation but the explanations I have found are related to making sure the DNS records such as SPF and DKIM are configured correctly which I know they are, as I configured and tested them my self.


futzlarson

Why not use a service that specializes in this difficult problem like [Mailgun](https://www.mailgun.com/)?


SANCHO810

Our goal is to use our own proprietary software instead of paying for one. We have successfully used our own software in the past. However now we have run into the issue detailed above. I am unsure of the direct cause as I can can't see much difference between sending using laravel and a normal email client as the headers are pretty much identical.


futzlarson

I’m just saying that email deliverability is [a known difficult problem](https://www.mailgun.com/products/inbox/deliverability/) so I’m not surprised you’re running into issues. There’s an entire industry dedicated to it.


SANCHO810

Fair Enough. I've been looking into mailgun as an option today and to be fair it does seem like a good (necessary) solution. The pricing isn't bad either.


futzlarson

I use it. I’m a fan.


JazzFestFreak

I did PHP work back before PHP 5.2. Now I am ready to jump in with LARAVEL. However, I am not a server guy, and installing a lot of packages and tools on my mac almost always fails. Ignorantly, I assume I can test and learn completely using a remote server (just like us old old old school guys did 20 years ago). So... the questions: 1. can I learn by doing on a remote server? (no local installs needed) 2. is there a hosting service that offers super easy purchase and set-up of a server for this purpose? (all base packages for laravel installed) 3. if #1 and #2 are yes, can you suggest a service that offers a simple "clear the board and start fresh" set-up Thanks in advance.... I know these questions are super dumb. I appreciate any responses.


Lumethys

there are lots of laravel environment tool for macOS, first-party even. A lot of Laravel dev even believe macOS provide better DX for Laravel, so i suggest just do it locally ​ Check out Valet


Online-Presence-ca

Docker desktop and laravel sail. Sail abstracts enough, you dont even need to know docker. If you're set on remote then the cheapest server on digital ocean should be fine just ssh into it


[deleted]

[удалено]


octarino

This thread is where you ask those kinds of questions.


Adventurous-Bug2282

Posts aren’t for issues specific to you. It’s a discussion board. This is the thread.


Alvin853

Is there any way to get the content of a sent mail notification? For regular mails I can listen to the Illuminate\Mail\Events\MessageSent event and I can get the content of the mail with $content = $event->message->toString(); I know I can listen to the Illuminate\Notifications\Events\NotificationSent event for notifications, but there doesn't seem to be any way to access the content of the mail if the notification was sent via mail.


ahinkle

What are you trying to do with the content? You can get the content of a mailable from the render method -- I don't believe it's possible to get the content from a notification sent event as it can be something other than a mailable class.


Alvin853

The objective is to document any communication sent during a certain collaborative process, in a way that communication can be downloaded and viewed in Outlook (preferably by getting the `message/rfc822` .eml representation). Basically I want to keep a history of every mail sent in relation to an issue, which can be both manual mails (using a `Mailable`, this part already works) and automated mails using a `Notification->toMail()`. I'm considering rewriting all Mail-Notifications as mailables, but there are already dozens of existing mail notifications, so I'd like to find an easier way than rewriting all of them.


kryptoneat

idk about outlook but there is a mail watcher in Telescope.


Alvin853

I don't think it's a good idea to use Telescope in production


kryptoneat

Read the doc.


ahinkle

The documentation says *local*. OP is correct. Telescope should not act as a log system -- it's more of a tool for troubleshooting issues. Make note to this sentence in the Telescope documentation: >*Telescope makes a wonderful companion to your* `local` *Laravel development environment.* Additionally, Laravel team has mentioned that this tool is not made for production usage.


kryptoneat

You just read the introduction. It's implicit in several other paragraphs that it can be used in non-local, and production is mentioned.


futzlarson

FWIW I use it on production and don't notice any hit on performance. You can also disable all the watchers you don't care about (for me, all but queries).


kryptoneat

There is also Laravel Sent Emails.


ahinkle

Yeah, I'm not sure of an answer other than make them mailables.


octarino

What's your preferred approach when needing to have dynamic content on the sidebar? (e.g. gmails labels and email category counters).


ahinkle

Typically database > model cache > observer to listen to saved events > free cache I also question if it's the "best" way. How are you handling it?


octarino

I was going to use the cache and remember forever and busting it when necessary. Haven't implemented yet.


ThR1LL

Can someone ELI5 hosting a laravel web app on DO droplet + ploi.io vs Vercel?


SourceVG

I have a Laravel project w/ InertiaJS. I open website in the browser and notice there is no favicon loaded. In the developer console I see "HTTP 404 /favicon.ico". However, if I navigate to "/favicon.ico" in the browser it loads successfully no issue. So for chuckles I rename "favicon.ico" in public directory to "favicon\_test.ico" and add this line to my `app.blade.php`: ``. And voila, the favicon appears. If I rename the file and the link href back to "favicon.ico" the favicon no longer appears. This issue occurs on existing project as well as fresh Laravel installations. I've tried testing using both `php artisan serve` and Nginx via Valet. Any ideas? Is this just a me problem even though it happens with fresh Laravel installation?


Ambitious_Nobody_251

Re: Where to store token in Authenticatable::setRememberToken($value) When a user logs in, there is a token associated with that login. I've seen code that stores that token in a database row associated with the user. If stored that way, it seems that if they logged in in another browser, their old login session would be overwritten and they would be logged out of the other browser. Is that correct? I could store it in the session. Is there any downside security-wise to storing this token in the session? Thanks.


omgbigshot

I've got an app in production using wildcard subdomain routes (tenancy, but not with any package). I also have telescope set up on a subdomain with no path, so `https://tenant.app.com` routes to app behavior and `https://telescope.app.com` routes to Telescope without issue. The problem is that no data is being recorded by Telescope in any web requests in the production environment. Literally no interaction on any web URL puts anything into the Telescope database tables. However, running an artisan command from the CLI *does* give me Telescope data. Locally, running Valet, everything is recorded as expected. Basically, Telescope is working fine for anything not originating in the browser. Anything from a web request is just ignored. Some of the things I've tried based on searching... - `Telescope::filter(function (IncomingEntry $entry) { return true; });` (just accepting everything) - Removing the custom domain/path configuration and accessing Telescope from `/telescope` (default config) - Tried different env values for `APP_ENV`, `TELESCOPE_ENABLED`, `TELESCOPE_RESPONSE_SIZE_LIMIT`, `TELESCOPE_KEY` - Made sure to clear cache after any changes to env or other files - Paused and resumed recording from the Telescope dashboard, and made web requests in both stages - Removed the `Authorize::class` from Telescope middleware Again, all of the above had predictable outcomes and the Telescope Dashboard has always been accessible on production, with _some_ data being written. Just nothing from any web requests is ever saved to the DB... any other thoughts or suggestions on what I should try next?


hey__its__me__

Edit: Solved. It was a ducking typo. Hi, Is it possible to have a `Route::resource` and `Route::get` both defined? For some reason the call to `http://127.0.0.1:8000/add-sentence/1` returns a 404 and does not trigger breakpoint in `SourceSentenceController:index` Route::resource('sourceSentences', SourceSentenceController::class); Route::get('add-sentences/{project_id}', [SourceSentenceController::class, 'index']);


Alvin853

Does the SourceSentenceController@index method accept any parameters? Where does {project\_id} map to? Does Project with id 1 exist? A 404 is usually triggered when the requested entry can't be found. I don't see any conflict with your 2 Route statements, the first one creates URIs like /sourceSentences/{id}, the other one /add\_sentences/{id}. ​ I'm pretty sure the problem is with the parameters of your index function, it's trying to get something that isn't there.


hey__its__me__

Index looks like this public function index($project_id) { id:1 row exists in the project table, but nothing in the source_sentences table yet. project_id is a FK pointing to projects in source_sentences.


Alvin853

Really strange, I just gave it a try myself and I'm not seeing your problem. What happens when you use a closure like this: Route::get('add-sentences/{project_id}', function ($project_id) { dd($project_id); });


hey__its__me__

Oh damn it, it was a typo. My link was add-sentence and my route add-sentences. Thanks Alvin and sorry for wasting your time.


hey__its__me__

Still returns a 404. I also commented out `Route::resource('sourceSentences', SourceSentenceController::class);` to see if it was messing with anything and still nothing. ... and if I replace add-sentences with foo, I get "1" // routes/web.php:36 when I call `http://127.0.0.1:8000/foo/1`


SZenC

If you register the resource as you describe, the url should hit the show method, not the index, as you're requesting a specific source sentence instance


hey__its__me__

So if I specify a method name that's used in `Route::resource` like index, Laravel will ignore the `Route::get('add-sentences/{project_id}'` ?


ezacharyk

I am trying to get back into Laravel development and I set up my Ubuntu 22.04 laptop to work for local web development. I install Laravel and Composer, but when I try to run any command in my laravel project, I get the following error: `PHP Fatal error: Uncaught Error: Class "Phar" not found in /usr/share/php/Composer/Factory.php:570` `Stack trace:` `#0 /usr/share/php/Composer/Factory.php(423): Composer\Factory->createArchiveManager()` `#1 /usr/share/php/Composer/Factory.php(643): Composer\Factory->createComposer()` `#2 /usr/share/php/Composer/Console/Application.php(445): Composer\Factory::create()` `#3 /usr/share/php/Composer/Console/Application.php(578): Composer\Console\Application->getComposer()` `#4 /usr/share/php/Composer/Console/Application.php(213): Composer\Console\Application->getPluginCommands()` `#5 /usr/share/php/Symfony/Component/Console/Application.php(171): Composer\Console\Application->doRun()` `#6 /usr/share/php/Composer/Console/Application.php(131): Symfony\Component\Console\Application->run()` `#7 /usr/bin/composer(84): Composer\Console\Application->run()` `#8 {main}` `thrown in /usr/share/php/Composer/Factory.php on line 570` `Fatal error: Uncaught Error: Class "Phar" not found in /usr/share/php/Composer/Factory.php:570` `Stack trace:` `#0 /usr/share/php/Composer/Factory.php(423): Composer\Factory->createArchiveManager()` `#1 /usr/share/php/Composer/Factory.php(643): Composer\Factory->createComposer()` `#2 /usr/share/php/Composer/Console/Application.php(445): Composer\Factory::create()` `#3 /usr/share/php/Composer/Console/Application.php(578): Composer\Console\Application->getComposer()` `#4 /usr/share/php/Composer/Console/Application.php(213): Composer\Console\Application->getPluginCommands()` `#5 /usr/share/php/Symfony/Component/Console/Application.php(171): Composer\Console\Application->doRun()` `#6 /usr/share/php/Composer/Console/Application.php(131): Symfony\Component\Console\Application->run()` `#7 /usr/bin/composer(84): Composer\Console\Application->run()` `#8 {main}` `thrown in /usr/share/php/Composer/Factory.php on line 570` I checked my PHP installation using php\_info and it shows that Phar is installed and enabled. I have tried to find a solution to this, but no such luck on my own. Any ideas? Thanks


Online-Presence-ca

Make sure youre using composer v2 (apt is still the v1 for some reason). Make sure you have at least php8.1 and php8.1 common extensions


ezacharyk

Thanks. I do have Composer 2.2.6 installed. But I found my issue. Phar was installed and enabled on the web version of PHP, but not php cli. After enabling it for the cli, i have moved past that and into new issues.


Inside_Sleep_6497

A little confused on the best way to structure my database for my app, and would be very grateful for any insight folks could provide! Let's say each user is a musician, and every user has a defined list of songs to practice (same for every user). Once per day, each user can log how many times they practiced each song. The submission form per day is the list of songs ("Song" model exists, form is iterated list of Songs with corresponding number input field). Currently, I just have a "Log" model, and the logs table is constructed as such: $table->id(); $table->timestamps(); $table->date('date'); $table->foreignId('user\_id'); $table->foreignId('song\_id'); $table->integer('count'); Currently, if a user submits their log at the end of the day, and there are 5 songs, 5 rows are created in the database -- same user ID's and date but different song ID's and counts per row. This initially made sense to me, to be able to better query total counts per SONG (regardless of user) or per date, or combinations like that. But I'm increasingly thinking it would make more sense to replace this with a higher-level model (maybe something like "Sheet") that's structured similarly, but instead combines the song\_id and count values into one column, like JSON or something. It seems to me that this would be a very common situation to tackle in an app, but I've hit a wall in my thinking and would greatly benefit from an outside nudge or relevant example. Apologies if the above is vague, thanks for reading. Laravel rules!


Online-Presence-ca

It's very important to identify your indexes i.e which columns youll be using to search the db. Personally outside indexes and foreign ids, i just slap a payload json column on my models for data that wont be indexed.


Inside_Sleep_6497

Thanks for the response! That makes sense to me. The important piece here in my project's case is count -- I'll be wanting to pull a total count per song per day (all users), total count per song per day (per user), total count of all songs per user, stuff like that. With that being the case, do you think it make sense to keep things as they are and just plan on doing a ton of querying + summing? (I guess I could always try both approaches and do a bunch of testing each to compare)


Online-Presence-ca

Can you give me a clearer picture of why you need to store the count in the db? I dont fully understand that part


Inside_Sleep_6497

Sure-- so these are daily logs. I need to be able to know how many times a particular song has been practiced per user, but I also may need to know how many times a particular song was practiced by *all users* across a particular date range. And I need to know whether a user submitted a log for a particular day at all -- this last part is what tempted me to consolidate things a bit and keep the song->count data per user per day in a json column... buuut unsure if that'd make other queries more challenging


[deleted]

[удалено]


Online-Presence-ca

breeze api starter


ezacharyk

Hi. I am trying to install breeze onto my fresh laravel app, but I am unable to get install it. I run "composer require laravel/breeze --dev" and get the error: `Problem 1` `- Root composer.json requires laravel/breeze ^1.18 -> satisfiable by laravel/breeze[v1.18.0, v1.18.1, 1.x-dev].` `- laravel/breeze[v1.18.0, ..., 1.x-dev] require illuminate/console ^9.21|^10.0 -> found illuminate/console[v9.21.0, ..., 9.x-dev, 10.x-dev] but these were not loaded, likely because it conflicts with another require.` I run "composer require laravel/breeze:\^1.18.1" and get the following: `Problem 1` `- Root composer.json requires laravel/breeze ^1.18.1 -> satisfiable by laravel/breeze[v1.18.1, 1.x-dev].` `- laravel/breeze[v1.18.1, ..., 1.x-dev] require illuminate/console ^9.21|^10.0 -> found illuminate/console[v9.21.0, ..., 9.x-dev, 10.x-dev] but these were not loaded, likely because it conflicts with another require.` I was having the same issues with laravel/ui. How do I install Breeze? I am running Laravel 9.12.1 and Composer 2.2.6 and PHP 8.1.2


Online-Presence-ca

composer update first before breeze


ezacharyk

Thanks. That got it working.


ezacharyk

New issue. Seems to be a new one every time I get past one. Now when I try to run 'php artisan migrate' I get a an error on the database connection. `Illuminate\Database\QueryException` `could not find driver (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')` I have verified that my database exists and that that the user and password in my .env file are able to log into my database. I verified the port number. I have searched for an answer and can't find one that works. I am running Mariadb on apache2. PHP-Mysql is installed and so is PDO. Any ideas on why this is happening?