I'm trying to fetch data from database and pass this data to pdf view and download it.
I tried this code but its not working:
Download PDF
Route
Route::get('download-PDF/{id}', 'PDFController#pdf');
Controller
class PDFController extends Controller
{
public function pdf($id){
$getEvent=Event::find($id);
$eventId=$getEvent->id;
if(isset($eventId)) {
$eventData = Event::where('id', $eventId)->first();
$getDays = Day::where('event_id', $eventId)->get();
for ($i = 0; $i < count($getDays); $i++) {
$dayId = $getDays[$i]->id;
$schedule[$i] = DaySchedule::where('day_id', $dayId)->get();
}
}
$pdf=PDF::loadView('pdf',['eventData' => $eventData, 'schedule' => $schedule]);
return $pdf->download('event.pdf');
}
}
Config
'providers' => [
Barryvdh\DomPDF\ServiceProvider::class,
]
'aliases' => [
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Are you getting any exception?
Here is a code I use for generating the pdfs:
Controller:
public function generateReport(TimetableRequest $request, $id) {
$reportData = $this->prepareReportData($id, $request->startDate, $request->endDate);
$pdf = App::make('dompdf.wrapper');
$pdf->loadView('pdf.report', $reportData);
return $pdf->download('report.pdf');
}
Config
'providers' => [
...
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
...
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Route
Route::get('projects/{id}/report', 'Controller#generateReport');
Related
we are currently working on a laravel 8 application. We are trying to create factories to create some dummy data for manual / developer based application testing.
The current code of my main Database-Seeder is below:
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
UserTableSeeder::class,
]);
\App\Models\User::factory(10)->create();
\App\Models\Activity::factory(5)->create();
/* 1. try
$tenFact = \App\Models\Tenant::factory(2)->has(
\App\Models\Project::factory(2)->state(
function (array $attributes, \App\Models\Tenant $tenant) {
return ['tenant_id' => $attributes['id']];
}
)->hasTasks(5)->hasLocation()
)->hasContracts(3)->create();
*/
/* Currently being used: */
\App\Models\Tenant::factory(10)->has(
\App\Models\Project::factory(5)->hasTasks(5)->hasLocation()
)->hasContracts(3)->create();
}
ProjectFactory.php:
class ProjectFactory extends Factory
{
protected $model = Project::class;
public function definition()
{
return [
'name' => 'Projekt: '. $this->faker->name,
'budget' => $this->faker->randomDigitNotNull*1000,
'progress' => $this->faker->randomDigitNotNull*10,
'budget_used' => $this->faker->randomDigitNotNull*50,
//'tenant_id' => Tenant::factory(),
'location_id' => Location::factory()->hasTenant(1),
];
}
}
LocationFactory.php:
class LocationFactory extends Factory
{
protected $model = Location::class;
public function definition()
{
return [
'name' => 'Standort: ' . $this->faker->company,
'street' => $this->faker->streetName,
'house_number' => $this->faker->buildingNumber,
'house_addition' => $this->faker->secondaryAddress,
'zip' => $this->faker->postcode,
'city' => $this->faker->city,
'tenant_id' => Tenant::factory(),
];
}
}
Our relationships look like this:
Tenant
|-- Project (has: tenant_id, but also has location_id)
| | -- Task (has: project_id)
|-- Locations (has: tenant_id)
|-- Contracts (has: tenant_id)
When creating datasets with the above named Tenant-Factory the following happens:
Tenant->id is being passed to Project(tenant_id)
but: Tenant->id is not being passend to Location (which depends on the tenants id but is also used for Project).
How can we pass the id of \App\Models\Tenant::factory(10) to Project::factory(5)->hasTasks(5)->hasLocation()?
Additionally we do have the problem, that even though we request 10 tenants, we will get around 60, because Location/Project create new objects when they should be using existing ones.
I gave up using the chained usage of the Tenant-Factory - I finally used some for-Loop that connected the related objects to each user by using laravels for() and state() methods:
for ($i=0; $i < 10 ; $i++) {
$tenant = \App\Models\Tenant::factory()->hasContracts(3)->create();
for ($j=0; $j < 5; $j++) {
$location = \App\Models\Location::factory(1)->for($tenant)->create();
$project = \App\Models\Project::factory(1)->state([
'location_id' => $location->first()['id'],
'tenant_id' => $tenant['id']])->hasTasks(5)->create();
}
}
class ProjectFactory extends Factory
{
$location_ids = App\Models\Location::pluck('id')->toArray();
protected $model = Project::class;
public function definition()
{
return [
'name' => 'Projekt: '. $this->faker->name,
'budget' => $this->faker->randomDigitNotNull*1000,
'progress' => $this->faker->randomDigitNotNull*10,
'budget_used' => $this->faker->randomDigitNotNull*50,
//'tenant_id' => Tenant::factory(),
'location_id'=> $faker->randomElement($location_ids),
];
}
}
class LocationFactory extends Factory
{
$tenant_ids = App\Models\Tenant::pluck('id')->toArray();
protected $model = Location::class;
public function definition()
{
return [
'name' => 'Standort: ' . $this->faker->company,
'street' => $this->faker->streetName,
'house_number' => $this->faker->buildingNumber,
'house_addition' => $this->faker->secondaryAddress,
'zip' => $this->faker->postcode,
'city' => $this->faker->city,
'tenant_id'=> $faker->randomElement($tenant_ids),
];
}
}
I'm trying to use "babenkoivan/scout-elasticsearch-driver" with "astrotomic/laravel-translatable", but i don't understand how I could index the translated words.
My Model looks like :
namespace App\Models;
use Astrotomic\Translatable\Translatable;
use App\Models\Search\ShowIndexConfigurator;
use ScoutElastic\Searchable;
...
class Show extends BaseModel
{
...
use Translatable;
use Searchable;
protected $indexConfigurator = ShowIndexConfigurator::class;
protected $searchRules = [
//
];
protected $mapping = [
'properties' => [
// How to index localized translations ???
'title' => [
'type' => 'string'
],
]
];
....
public $translatedAttributes = [
...,
'title'
...
];
Best regards
I found a solution with the override of the method
public function toSearchableArray() with something like:
public function toSearchableArray(): array
{
$document = [];
if ($this->published) {
$document = [
//...
];
foreach ($this->translations()->get() as $translation)
{
if (!$translation->active) {
continue;
}
$locale = $translation->locale;
$document['title_' . $locale] = $translation->title;
$document['url_' . $locale] = $this->getLink($locale);
$document['sub_title_' . $locale] = $translation->sub_title;
$document['keywords_' . $locale] = "";
}
}
return $document;
}
The purpose of $mapping=[] is only to define the structure of data. Something like that is expected:
protected $mapping = [
'properties' => [
'title_en' => [
'type' => 'string'
],
'title_fr' => [
'type' => 'string'
],
...
]
];
I am using Laravel version 5.7 and I want to translate the value of required_if. I used below action but it does not work. what shall I do?
in RequestObject.php:
public function rules() {
$fee = '';
if ($this['params.fee_status'] == 'pay') {
$fee = 'required_if:params.fee_status,pay|numeric|min:100';
}
return [ 'params.fee' => $fee,];
}
in resources/lang/en/validation.php :
'values' => [
'params.fee_status' => [
'pay' => 'default'
],
],
I am new to php and I am currently trying to make a testcase for an add function I wrote for adding records in the table "project_point" of my database. In this testcase I want to post some test data to that add function and check if the data is set correctly.
Project Point Add function
public function addProjectPoint (Request $request) {
$point = new ProjectPoint();
$location = new Point($request->markerLat, $request->markerLong);
$point->project_id = $request->project_id;
$point->location = $location;
$point->area = $request->area;
$point->name = $request->name;
$point->information = $request->information;
$point->category = $request->category;
$point->save();
}
My test case
public function testCreateProjectPoint()
{
$this->post('admin/projectpoint/create', [
'project_id' => 1,
'markerLat' => 5.287020206451416,
'markerLong' => 51.68828138589033,
'area' => null,
'name' => 'TestCaseProjectPoint',
'information' => 'This is a automated test ProjectPoint, please delete this point!',
'category' => 'bezienswaardigheid'
]);
$this->assertDatabaseHas('interest_point', [
'project_id' => 1,
'location' => new Point(5.287020206451416, 51.68828138589033),
'area' => null,
'name' => 'TestCaseProjectPoint',
'information' => 'This is a automated test ProjectPoint, please delete this point!',
'category' => 'bezienswaardigheid'
]);
/*
$test = factory(ProjectPoint::class)->create();
$this->post('admin/projectpoint/create', $test);
$this->assertDatabaseHas('project_point', $test);
*/
}
ProjectPoint model
class ProjectPoint extends Model
{
use SpatialTrait;
protected $table = 'interest_point';
protected $fillable = ['project_id', 'name', 'information', 'category' ];
protected $spatialFields = [
'location',
'area'
];
public $timestamps = false;
public function project()
{
return $this->belongsTo('App\Models\Project', 'project_id');
}
}
The output of the test is:
Failed asserting that a row in the table [interest_point] matches the attributes {
"project_id": 1,
"location": {
"type": "Point",
"coordinates": [
51.68828138589033,
5.287020206451416
]
},
"area": null,
"name": "TestCaseProjectPoint",
"information": "This is a automated test ProjectPoint, please delete this point!",
"category": "bezienswaardigheid"
}.
But I expect to see the test case succeed and when checking the database no records have been added to the database
Have you tried still putting location and area in your fillables?
I've searched around and couldn't find a definitive answer for this...
I have a package DevDojo Chatter and would like to extend it using my application. I understand I'd have to override the functions so that a composer update doesn't overwrite my changes.
How do I go about doing this?
UPDATE
public function store(Request $request)
{
$request->request->add(['body_content' => strip_tags($request->body)]);
$validator = Validator::make($request->all(), [
'title' => 'required|min:5|max:255',
'body_content' => 'required|min:10',
'chatter_category_id' => 'required',
]);
Event::fire(new ChatterBeforeNewDiscussion($request, $validator));
if (function_exists('chatter_before_new_discussion')) {
chatter_before_new_discussion($request, $validator);
}
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
$user_id = Auth::user()->id;
if (config('chatter.security.limit_time_between_posts')) {
if ($this->notEnoughTimeBetweenDiscussion()) {
$minute_copy = (config('chatter.security.time_between_posts') == 1) ? ' minute' : ' minutes';
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => 'In order to prevent spam, please allow at least '.config('chatter.security.time_between_posts').$minute_copy.' in between submitting content.',
];
return redirect('/'.config('chatter.routes.home'))->with($chatter_alert)->withInput();
}
}
// *** Let's gaurantee that we always have a generic slug *** //
$slug = str_slug($request->title, '-');
$discussion_exists = Models::discussion()->where('slug', '=', $slug)->first();
$incrementer = 1;
$new_slug = $slug;
while (isset($discussion_exists->id)) {
$new_slug = $slug.'-'.$incrementer;
$discussion_exists = Models::discussion()->where('slug', '=', $new_slug)->first();
$incrementer += 1;
}
if ($slug != $new_slug) {
$slug = $new_slug;
}
$new_discussion = [
'title' => $request->title,
'chatter_category_id' => $request->chatter_category_id,
'user_id' => $user_id,
'slug' => $slug,
'color' => $request->color,
];
$category = Models::category()->find($request->chatter_category_id);
if (!isset($category->slug)) {
$category = Models::category()->first();
}
$discussion = Models::discussion()->create($new_discussion);
$new_post = [
'chatter_discussion_id' => $discussion->id,
'user_id' => $user_id,
'body' => $request->body,
];
if (config('chatter.editor') == 'simplemde'):
$new_post['markdown'] = 1;
endif;
// add the user to automatically be notified when new posts are submitted
$discussion->users()->attach($user_id);
$post = Models::post()->create($new_post);
if ($post->id) {
Event::fire(new ChatterAfterNewDiscussion($request));
if (function_exists('chatter_after_new_discussion')) {
chatter_after_new_discussion($request);
}
if($discussion->status === 1) {
$chatter_alert = [
'chatter_alert_type' => 'success',
'chatter_alert' => 'Successfully created a new '.config('chatter.titles.discussion').'.',
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
} else {
$chatter_alert = [
'chatter_alert_type' => 'info',
'chatter_alert' => 'You post has been submitted for approval.',
];
return redirect()->back()->with($chatter_alert);
}
} else {
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => 'Whoops :( There seems to be a problem creating your '.config('chatter.titles.discussion').'.',
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
}
}
There's a store function within the vendor package that i'd like to modify/override. I want to be able to modify some of the function or perhaps part of it if needed. Please someone point me in the right direction.
If you mean modify class implementation in your application you can change the way class is resolved:
app()->bind(PackageClass:class, YourCustomClass::class);
and now you can create this custom class like so:
class YourCustomClass extends PackageClass
{
public function packageClassYouWantToChange()
{
// here you can modify behavior
}
}
I would advise you to read more about binding.
Of course a lot depends on how class is created, if it is created using new operator you might need to change multiple classes but if it's injected it should be enough to change this single class.