Have you ever needed to share one variable with multiple views in Laravel? Here's a simple solution for that.
use App\Models\Post;use Illuminate\Support\Facades\View;use Illuminate\Support\Facades\Schema;use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider{ public function boot() { if (Schema::hasTable('posts')) { View::share('recentPosts', Post::latest()->take(3)->get()); } }}
Tip given by @codewithdary
Enjoyed This Tip?
Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.
Recent Courses
[NEW] Building a Typical Laravel SaaS
8 lessons
55 min
Laravel Modules and DDD
16 lessons
1 h 59 min
Claude Code for Laravel Projects: Crash Course
8 lessons
48 min