Download files in laravel using Response::download

Try this. public function getDownload() { //PDF file is stored under project/public/download/info.pdf $file= public_path(). “/download/info.pdf”; $headers = array( ‘Content-Type: application/pdf’, ); return Response::download($file, ‘filename.pdf’, $headers); } “./download/info.pdf”will not work as you have to give full physical path. Update 20/05/2016 Laravel 5, 5.1, 5.2 or 5.* users can use the following method instead of Response facade. … Read more

Laravel save / update many to many relationship

tldr; Use sync with 2nd param false Many-to-many relationship is belongsToMany on both models: // Task model public function users() { return $this->belongsToMany(‘User’, ‘user_tasks’); // assuming user_id and task_id as fk } // User model public function tasks() { return $this->belongsToMany(‘Task’, ‘user_tasks’); } In order to add new relation use attach or sync. Difference between … Read more

Laravel Controller Subfolder routing

For Laravel 5.3 above: php artisan make:controller test/TestController This will create the test folder if it does not exist, then creates TestController inside. TestController will look like this: <?php namespace App\Http\Controllers\test; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class TestController extends Controller { public function getTest() { return “Yes”; } } You can then register your route this … Read more

How do I pass a variable to the layout using Laravel’ Blade templating?

If you’re using @extends in your content layout you can use this: @extends(‘master’, [‘title’ => $title]) Note that same as above works with children, like: @include(‘views.subView’, [‘my_variable’ => ‘my-value’]) Usage Then where variable is passed to, use it like: <title>{{ $title ?? ‘Default Title’ }}</title>

laravel throwing MethodNotAllowedHttpException

You are getting that error because you are posting to a GET route. I would split your routing for validate into a separate GET and POST routes. New Routes: Route::post(‘validate’, ‘MemberController@validateCredentials’); Route::get(‘validate’, function () { return View::make(‘members/login’); }); Then your controller method could just be public function validateCredentials() { $email = Input::post(’email’); $password = Input::post(‘password’); … Read more

Passing data to a closure in Laravel 4

If you instantiated the $team variable outside of the function, then it’s not in the functions scope. Use the use keyword. $team = Team::find($id); Mail::send(’emails.report’, $data, function($m) use ($team) { $m->to($team->senior->email, $team->senior->first_name . ‘ ‘. $team->senior->last_name ); $m->cc($team->junior->email, $team->junior->first_name . ‘ ‘. $team->junior->last_name ); $m->subject(‘Monthly Report’); $m->from(‘info@website.example’, ‘Sender’); }); Note: The function being used is … Read more

Clone an Eloquent object including all relationships?

tested in laravel 4.2 for belongsToMany relationships if you’re in the model: //copy attributes $new = $this->replicate(); //save model before you recreate relations (so it has an id) $new->push(); //reset relations on EXISTING MODEL (this way you can control which ones will be loaded $this->relations = []; //load relations on EXISTING MODEL $this->load(‘relation1′,’relation2’); //re-sync everything … Read more