<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Laravel &#8211; Roy Duineveld</title>
	<atom:link href="https://royduineveld.nl/tag/laravel/feed/" rel="self" type="application/rss+xml" />
	<link>https://royduineveld.nl</link>
	<description>Waarom moeilijk doen als het makkelijk kan?</description>
	<lastBuildDate>Tue, 27 Jan 2026 07:48:00 +0000</lastBuildDate>
	<language>nl</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>
	<item>
		<title>[EN] Laravel 5.5 validated() method on Form Requests</title>
		<link>https://royduineveld.nl/laravel-5-5-validated-method-on-form-requests/</link>
		<comments>https://royduineveld.nl/laravel-5-5-validated-method-on-form-requests/#respond</comments>
		<pubDate>Sat, 07 Oct 2017 14:36:05 +0000</pubDate>
		<dc:creator><![CDATA[Roy Duineveld]]></dc:creator>
				<category><![CDATA[Tips & Trucs]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">https://royduineveld.nl/?p=1065</guid>
		<description><![CDATA[<p>A while ago when Laravel 5.5 wasn&#8217;t released yet Jeffrey Way started a &#8220;What&#8217;s New in Laravel 5.5&#8221; serie on Laracasts. In the second lesson Streamlined Request Validation Jeffrey introduced...</p>
<p>Het bericht <a rel="nofollow" href="https://royduineveld.nl/laravel-5-5-validated-method-on-form-requests/">[EN] Laravel 5.5 validated() method on Form Requests</a> verscheen eerst op <a rel="nofollow" href="https://royduineveld.nl">Roy Duineveld</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>A while ago when Laravel 5.5 wasn&#8217;t released yet Jeffrey Way started a &#8220;<a href="https://laracasts.com/series/whats-new-in-laravel-5-5" target="_blank" rel="noopener">What&#8217;s New in Laravel 5.5</a>&#8221; serie on <a href="https://laracasts.com/" target="_blank" rel="noopener">Laracasts</a>. In the second lesson <a href="https://laracasts.com/series/whats-new-in-laravel-5-5/episodes/2" target="_blank" rel="noopener">Streamlined Request Validation</a> Jeffrey introduced us to the validated data array when validating a request:</p>
<pre class="brush: plain; title: ; notranslate">
public function store()
{
    $validatedData = request()-&gt;validate([
        'title' =&gt; 'required',
        'body' =&gt; 'required',
    ]);
}
</pre>
<p>Normally you would do something like <code>request()->all()</code> which isn&#8217;t always save if you&#8217;re not setup the mass assignment variables on the model right. For example when you&#8217;ve disabled mass assignment by adding <code>protected $guarded = [];</code> to a model. A better way is to use <code>request()->only()</code> with the fields you need. With the above example that would be: <code>request()->only(['title', 'body'])</code> but that&#8217;s feels like duplication right? You&#8217;ve already defined the fields in the validation array. From Laravel 5.5 <code>$validatedData</code> will output an array with the validated fields, the same as <code>request()->only()</code> would do.</p>
<h2>What about Form Requests?</h2>
<p>The first thing I was thinking when I&#8217;ve watched that lesson; is there something similar when using <a href="https://laravel.com/docs/5.5/validation#form-request-validation" rel="noopener" target="_blank">Form Requests</a>? I&#8217;m not a big fan of validating data in my controller. A comment under the lesson brought me here: <a href="https://github.com/sebastiaanluca/laravel-validator" rel="noopener" target="_blank">laravel-validator</a> which introduced that functionality to Laravel 5.4 with:</p>
<pre class="brush: plain; title: ; notranslate">
public function store(FormRequest $request)
{
    Page::create($request-&gt;valid());
}
</pre>
<p>But Laravel 5.5 introduced the <code>validated()</code> <a href="https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Http/FormRequest.php#L168:L180" rel="noopener" target="_blank">method</a> (currently still undocumented) so that package isn&#8217;t needed anymore and <a href="https://github.com/sebastiaanluca/laravel-validator/issues/1#issuecomment-326118084" rel="noopener" target="_blank">confirmed by the author</a>. I did find a <a href="https://github.com/laravel/framework/issues/21186" rel="noopener" target="_blank">bug</a> with it but was fixed already in Laravel 5.5.4.</p>
<h2>What if not all the data should be saved to the model?</h2>
<p>In a project I&#8217;m currently working on I&#8217;m using <a href="https://github.com/spatie/laravel-medialibrary" rel="noopener" target="_blank">Spatie&#8217;s Medialibrary package</a> so I&#8217;ve rules to validate images and lately I&#8217;m using <code>protected $guarded = [];</code> on all my models to disable the mass assignment checks. Let&#8217;s say we&#8217;ve a page with a <code>title</code>, <code>body</code> and <code>image</code>. In our <code>rules()</code> method on our <code>PageRequest</code> we&#8217;ve these rules:</p>
<pre class="brush: plain; title: ; notranslate">
return [
    'title' =&gt; 'required',
    'body'  =&gt; 'required',
    'image' =&gt; 'image'
];
</pre>
<p>When we save the page with <code>Page::create($request->validated());</code> we&#8217;ll get a database error because there is no <code>image</code> column. Spatie&#8217;s Medialibrary saves all the media in a seperated table so we don&#8217;t have a <code>image</code> column on our <code>pages</code> table. After we&#8217;ve saved the page we&#8217;ll process the image.</p>
<p>We&#8217;ve a few option to fix this:</p>
<ol>
<li>Instead of using <code>protected $guarded = [];</code> we could specify the fillable ones with <code>protected $fillable = ['title', 'body'];</code>.</li>
<li>We could use <code>Page::create($request->only('title', 'body'));</code> here instead of the <code>validated()</code> function.</li>
<li>We exclude the image: <code>Page::create(collect($request->validated())->except(['image'])->toArray());</code></li>
</ol>
<p>I&#8217;m using option 3, it may look like the longest / most complex option but if you&#8217;re getting more and more fields you&#8217;ll see it&#8217;s the easiest because it&#8217;s just a blacklist of fields you don&#8217;t want. You could refactor this back to your model so the blacklist is defined there.</p>
<blockquote><p>What option do you prefer?</p></blockquote>
<p>Het bericht <a rel="nofollow" href="https://royduineveld.nl/laravel-5-5-validated-method-on-form-requests/">[EN] Laravel 5.5 validated() method on Form Requests</a> verscheen eerst op <a rel="nofollow" href="https://royduineveld.nl">Roy Duineveld</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://royduineveld.nl/laravel-5-5-validated-method-on-form-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[EN] Laravel theme fallback views based on the user</title>
		<link>https://royduineveld.nl/laravel-theme-fallback-views-based-on-the-user/</link>
		<comments>https://royduineveld.nl/laravel-theme-fallback-views-based-on-the-user/#comments</comments>
		<pubDate>Thu, 21 Sep 2017 14:52:05 +0000</pubDate>
		<dc:creator><![CDATA[Roy Duineveld]]></dc:creator>
				<category><![CDATA[Tips & Trucs]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">https://royduineveld.nl/?p=1050</guid>
		<description><![CDATA[<p>In almost any CMS or e-commerce system there is any kind of theme system. For example in WordPress and Magento it&#8217;s possible to create a theme. In some cases you...</p>
<p>Het bericht <a rel="nofollow" href="https://royduineveld.nl/laravel-theme-fallback-views-based-on-the-user/">[EN] Laravel theme fallback views based on the user</a> verscheen eerst op <a rel="nofollow" href="https://royduineveld.nl">Roy Duineveld</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In almost any CMS or e-commerce system there is any kind of theme system. For example in WordPress and Magento it&#8217;s possible to create a theme. In some cases you want to use a default or existing theme and change some parts in a &#8220;child theme&#8221;. With Laravel there are some great packages to achieve this, but what if the loaded theme should be based on the current authenticated user?</p>
<p>While figuring out how I could accomplish this I came across <a href="https://sebastiandedeyne.com/posts/2017/theme-based-views-in-laravel-using-vendor-namespaces" target="_blank" rel="noopener">this great article</a> from <a href="https://sebastiandedeyne.com/about" target="_blank" rel="noopener">Sebastian de Deyne</a> from <a href="https://spatie.be" target="_blank" rel="noopener">Spatie</a>. He registers a view namespace in a service provider with <code>$this-&gt;loadViewsFrom()</code> which allows you to specify the theme directories and a namespace. But from a service provider I don&#8217;t have access to the current authenticated user because that part is loaded later, see <a href="https://laravel.com/docs/5.5/lifecycle" target="_blank" rel="noopener">Laravel&#8217;s request lifecycle</a>. When I looked at the <code>loadViewsFrom()</code> function I noticed a <code>addNamespace()</code> function on the views which can be used from (for example) the view facade: <code>View::addNamespace()</code>.</p>
<p>So what&#8217;s the best place to register the view namespace? I&#8217;d like to base it on the current user so I need to make sure the user is logged in. What about a middleware? Let&#8217;s create the middleware:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;

class UseThemeForUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        View::addNamespace('theme', [
            resource_path('views/themes/' . Auth::user()-&gt;theme),
            resource_path('views/themes/default'),
        ]);

        return $next($request);
    }
}
</pre>
<p>But this should only be executed for the frontend when a user is logged in. Let&#8217;s first register the middleware as a route middleware in <code>app/Http/Kernel.php</code> as <code>theme</code>. Now we can use this middleware in our routes file (for example with a route group) or controller. In my case I&#8217;ve created a dedicated frontend routes file and registered it in my route service provider together with the <code>web</code> and <code>auth</code> middleware because our <code>theme</code> middleware depend on those.</p>
<p>In all our controller and views which depend on a theme we can use for example <code>theme::home</code> to load our <code>/views/themes/custom/home.blade.php</code> file if the users theme is set to <code>custom</code>, or if that one doesn&#8217;t exists it will fallback to the <code>/views/themes/default/home.blade.php</code> template.</p>
<p>Het bericht <a rel="nofollow" href="https://royduineveld.nl/laravel-theme-fallback-views-based-on-the-user/">[EN] Laravel theme fallback views based on the user</a> verscheen eerst op <a rel="nofollow" href="https://royduineveld.nl">Roy Duineveld</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://royduineveld.nl/laravel-theme-fallback-views-based-on-the-user/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>