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
Testing in Laravel 13 For Beginners
26 lessons
1 h 41 min read
Queues in Laravel 13
18 lessons
1 h 12 min read
How to Build Laravel 13 API From Scratch
30 lessons
1 h 23 min