I create a customer with the CRUD from Laravel. I add some base data like "Name, Birthday, Sex etc.).
Now I use the "show" route from Laravel and see the data that I saved in the database table (kundens).
From this view, I generate a PDF with a button. All this works for me. All the data come from my table "kundens". The view data come from this table, too. But how can I show data from more than one table in the same "view"? I need to add some more data in the PDF - not only from the "kundens" table.
My code for the customer view is this:
<div class="row">
<div class="col-md-12 col-md-offset-12">
<h4>Übersicht</h4>
<h5>Persönliche Daten</h5>
{{ $kunden->vorname }}<br>
{{ $kunden->nachname }}<br>
{{ $kunden->strasse }}<br>
{{ $kunden->plz }}
{{ $kunden->wohnort }}<br>
<button class="btn btn-danger" style="color: #fff"><a style="color: #fff" href="{{asset('admin/generate_offer')}}{{ '/'.$kunden->id }}">Angebot erstellen</a></button>
<ul>
#foreach($kunden['offer'] as $offer)
<li>{{$offer->id}} </li>
#endforeach
</ul>
</div>
</div>
There you see that I take the information from $kunden - but I need to take more information that saved in another table.
Does anyone know a solution?
So first of all, you have to write your model and controller so we can see it better than what you write but here, for example, you want to make some other model, to show up here. So here is how you do it:
In your main model, kundens model you write like this:
public function somedata() {
return $this->hasOne('App\YOURAPP','id','Table_id'); // in case you have the name of tables as ID not the Table_id you must not the second and third argument here
}
and in your controller, you must write like this:
$kundens= Kundens::with('somedata')->get(); // here some data is the name of the function that u inserted in your controller
So you have to customize this a bit into your needs.
I hope this helps...
Apparently, you are generating a PDF file in the admin/generate_offer related controller.
So in order to have more data, just get your data in that controller (that is responsible for generating PDF file) and simply add all of them no matter what and how many tables you want to get.
Related
I have created a form where an Admin can create text and also use objects as used in Blade. I want to store the text in a table and then display it in HTML with the objects working properly.
For example, I would have a form with this input in my view
<div class='form-group'>
<textarea placeholder="" name='comments' type='' rows='10' class='form-control' id='' value = '{{ old('comments') ?? $plansubmission->comments }}'>{{ $plansubmission->comments }}</textarea>
<div>{{ $errors->first('comments') }}</div>
</div>
In that form input, I have entered the following:
Dear Employee, {{ $plansubmission->id }}
This input validates and the input is sent to comments column in the proper table in my database.
Now, I want to return the comments column back into the view with all the spacing that was submitted into the input (therefore, I use the 'pre' tags):
<pre> {{ $plansubmission->comments}} </pre>
The plan text and spacing is maintained but the blade part simply comes out as {{ $plansubmission->id}} instead of what the actual property is.
It's a major security problem to let users submit blade templates to display data. Blade is compiled to PHP so you would be essentially allowing users to execute any PHP code they want. I would recommend you use something like mustache to let users inject variables into the output.
In the controller that passes the data to the view, you can pass the $plansubmission->comments through a mustache parser. This will treat the comments field as a template, and the second parameter sets the variables that the template has access to. This way you can explicitly set what the comment template has access to so you don't let users leak more data than is required.
$m = new Mustache_Engine;
$comments = $m->render($plansubmission->comments, $plansubmission->toArray());
Then users can put something like this in the comments field
Dear Employee, {{ id }}
Then in the view do
<pre> {{ $comments }} </pre>
and it will output
<pre> Dear Employee, 123 </pre>
I want to ask if it is possible to do something like this in View in Laravel 5.2:
<p> This is window: {{$element_ + 'window'}} </p>
<p> This is wall: {{$element_ + 'wall'}} </p>
The values for this variables are from $element_window, $element_wall.
There are couple of options.
First - is to use #php block in .blade file for dynamic output:
#php
${'window'} = ${$element_.'window'}
#endphp
Second is to write custom blade extension to output anything you need.
Third is to define custom method in your Model (if you use one).
However I should mention, that such variable assignment inside template (first option) is not recommended. It's hardly readable and could cause Exceptions if such dynamically created variables do not exist at some point. Not saying that this is not presentation logic.
If you want to dynamically name a variable.. you can do the following.
<p> This is window: {{ ${'element_'.'window'} }} </p>
<p> This is wall: {{ ${'element_'.'wall'} }} </p>
That should work.
But if just want to concatenate a string to the variable... you can use "." :-)
I want to show the validation errors to the users in comma seperated
i.e.,
The username field is required, The password field is required
So far i can able to send the validation error messages to the view like this
$validation->messages()
But the only thing i can't able to do
#if(Session::has('Message'))
<p class="alert">{{ Session::get('Message') }}</p>
#endif
or
{{ $errors->first('username', '<div class="error">:message</div>') }}
The only thing i can do is to pass the messages as normal text.
So, How can i pass the validation messages to a view by plain text (rather than array or object)
Update :
I mean to say i can do any works only in controller and not in view
in controller:
return implode(',',$validation->errors()->all());
I understand that passing record ids through the url isn't usually a good idea, but I am wondering how I can avoid it in my case:
My objective is to list job statuses on a user dashboard and allow users to adjust the status.
I create my view and pass variables to it using the session:
userController.php
public function getdashboard()
{
//reading the user information
$arrPageData['user'] = Sentry::getUser();
//reading the job interviews
$arrPageData['jobInterviews'] = JobInterview::readCurrentInterviews($this->userID);
return View::make('clients.dashboard', $arrPageData);
}
This part works great and I don't use the record id in the route. I iterate through the jobInterviews in the dashboard view. Depending up on the status listed in the DB table, I give the user options
view file: dashboard.blade.php (snippet)
#foreach ($jobInterviews as $interviews)
#if ($interviews->j == $job->id)
<tbody>
<tr>
<td>
{{$interviews->contact_name}}
#if ($interviews->status == 'interview request accepted')
Hire
#elseif ($interviews->status == 'hired')
<button id="complete" class="btn btn-info btn-small">Mark Project Complete</button>
#endif
</td>
<td>{{$interviews->status}} </td>
</tr>
</tbody>
...
The problem that I am having is that to complete the job status change, I am calling the method and passing in the record id:
Still in dashboard.blade.php
<form action="../jobs/offer/{{$interviews->interview_id}}" method="post">
This is then routed through:
Route::post('/jobs/offer/{id}','JobController#jobOffer');
Everything works as I want it to but I don't think I am doing it right from a security stand point. Is there a better way to call the jobOffer method and change the status besides using the record id in the route when getting the data from an array i've iterated through?
Thanks in advance for the help.
You may try this:
{{ Form::open(array('action' => array('JobController#jobOffer', $interviews->interview_id))) }}
<!-- Rest of the form fields -->
{{ Form::close() }}
This way you don't need to add csrf/_method input manually and by default it's METHOD would be POST so you can omit that.
In Symfony docs they say to use this
<div>
{{ form_label(form.task) }}
{{ form_errors(form.task) }}
{{ form_widget(form.task) }}
</div>
But this generates the label element.
But I want to have table <td> instead of <label>
And also for the input textbox, I want to mention the size of text box. Where can I do that?
You have to define your form theme.
Probably, this tutorial is what you are looking for