Laravel 5 - generic document management - laravel-5

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally, I had a database table for each unique document type, but this was getting messy fast. So, I created a database structure that was more generic, and came up with the following
So, I create a project. Within the projects show page, I can select the type of document I want to create e.g.
<li>{!! link_to_route('projects.documents.create', 'Project Identified', array($project->id, 'documentType' => 'projectIdentified')) !!}</li>
Now if I select to create a Project Identified document, it uses the generic Document Controller to handle things. Because the link to route has a documentType param, I can grab the value of this from the url. As such, in my Document Controllers create function, I am doing the following to display the correct view for the document
public function create(Project $project)
{
$documentType = $_GET["documentType"];
if($documentType == "projectIdentified") {
return View::make('projectIdentifiedDoc.create', compact('project'));
}
}
This view has a form which is binded
{!! Form::model(new App\Document, [
'class'=>'form-horizontal',
'route' => ['projects.documents.store', $project->id]
]) !!}
However, within the document controllers store function, I once again need to get the documentType. How can I pass this within the forms model? Also, is this the correct way to do this or is there a more efficient way?
Thanks

Have you read the documentation on relationships?
https://laravel.com/docs/5.2/eloquent-relationships
You need to define the relationship within your model.
So if a document only has one documentType, within your document model, you would define
public function documentType()
{
return $this->hasOne('App\documentType');
}
The different types of relationship, how to define them, and then how to access that data, is all very well documented.

Related

Query to pass data to the edit view

I need to pass data to the "edit" view from the "index" view, the data are from two different related tables (table "personas" parent and table "residentes" child) so i'm working in the "edit()" method in the residentesController and i couldn't find a way to pass the data in a single object, is it possible to do it? if there is not What is the best way to do it?
//in the ResidentesController
//this is what i currenly have
public function edit(Residente $residente)
{
$persona = Persona::find($residente->persona_id);
return Inertia::render(
'Residentes/Editar',
[
'residente' => $residente,
'persona' => $persona
]
)
}
If i understand correctly your question you need to load the relationship you need in the original query (keep in mind to create it in the model classes)
$persona = Persona::with('residentes')->find($residente->persona_id);
and after that you can create the variable
$residente = $persona->pluck('residentes');

Laravel: retrieving one item from a relationship

I am using Laravel Eloquent to retrieve data from the database.
I want to get the related data as an object not an array ( whats inside the texts table), so it is easier to work on the data on the blade file. This is my code I tried using first() but it doesn't work
Icon::with(["texts" => function($query) use ($language){
$query->where("language_id",$language->id)->first();
}, "texts.language"])->get();
How to Acheive it?
You could create an accessor function that provides a shorthand attribute to the required value.
In your Icon class:
public function getTextAttribute() {
// You'd only have to provide the $language somehow.
return $this->texts()->where('language_id', $language->id)->first();
}
Elsewhere, like in Blade, when you're using an Icon you can then use:
{{$icon->text}}

laravel/elequent - models and relations

I trying to learn laravel and to do some tests/demo apps. I've struggling now with laravel/eloquent tables relations. And I need advice.
I have 3 models [Application, Term, AppState] and their tables applications[id, terms_id, appStates_id, and other cols ], terms[id, startDate, endDate, ...], app_states[id, caption]
Application.php
public function term()
{
return $this->belongsTo('App\Term');
}
public function appState()
{
return $this->belongsTo('App\AppState');
}
in Term.php and AppState.php i have:
public function applications()
{
return $this->hasMany('App\Application');
}
How I can get let's say "caption"/"startDay"+"endDate" in blade for each application? I can get their ids $app->terms_id/$app->appStates_id in foreach loop, but i want get caption value from app_states table.
Has to be this relations also specified in migrations? In some tuts is mentioned, that is not needed in case i want to handle it only in laravel.
Thanks for advice
You can access a model's relationship values by calling the relationship method like a property.
$application = Application::find(1);
$application->term->startDate;
$application->term->endDate;
$application->appState->caption;
Also your relationship with AppState is wrong, since your foreign key doesn't follow a snake_case typing, you'll need to provide the appropriate key for it
public function appState()
{
return $this->belongsTo('App\AppState', 'appStates_id');
}
You might also want to check terms_id as well since the model name (Term) is singular but the foreign key is plural.
Has to be this relations also specified in migrations? In some tuts is mentioned, that is not needed in case i want to handle it only in laravel.
Well, yes, you don't need to if Laravel will only be the one accessing that database. But if any cases in the near future you decide to migrate to a different framework or use the same database in another application, it's better to include these relationships in the migration. Also a database administrator would probably cringe if you don't.
So provided your relationships are correctly setup, you can access them anywhere you have an instance of that model.
So for example, lets say you have passed a collection of applications to your view ($apps):
#foreach($apps as $app)
{{ $app->term->startDate }}
{{ $app->term->endDate }}
{{ $app->appState->caption }}
#endforeach
Important Note: We are accessing the Eloquent relationship using ->appState rather than ->appState(). The later is actually accessing a Query Builder instance and has some more advanced use cases

Creating a filtered search based on different parameters

i need to create a filtered search based on different paramnters chosen by a user. So, for example, my app is a property app. I want them to be able to define the county, town, max,min bedrooms ect from a dropdown list and press search, and this returns all the properties that match the criteria.
How do I go about doing this? I can't find any tutorials online, but maybe I'm not phrasing it right.
Here is an image of what I'm after from a UI point of view.
https://imgur.com/a/YEEqt
The following is an easy to go solution considering your experience.
Create a new controller or a method in an existing one.
First create a simple form (method GET) in blade that will return predefined values from simple hidden fields
Create a new GET route in which the form must be submitted and link it to the controller method that you created
In your controller method get the submitted form data from your request, make the correct queries and return the blade template file that contains the form with the results
Modify blade template to show results
Finally replace the hidden fields with selects drop-downs and modify your controller in order to populate them
You can make the form submission process and the select drop-down fields population asynchronous but based on your experience with Laravel it should be hard. Following the steps above will do your job.
Always take a look to the official documentation. You will ge a lot of help from there.
You can try like this for filter options in your controller
public function filter(Request $request, Property $property)
{
$property = $property->newQuery();
// Search for a property based on country
if ($request->has('country')) {
return $property->where('country', $request->input('country'));
}
// Search for a property based on their area.
if ($request->has('areas')) {
return $property->where('areas', $request->input('areas'));
}
// Search for a property based on max_price
if ($request->has('max_price')) {
return $property->where('price','<=', $request->input('max_price'));
}
// Continue for all of the filters.
return $property->get();
}
For more info refer the link

Laravel Backpack : Storing Belongs To Many relationships using custom view

I have a flight class and this flight has a custom view field like so:
This represents a belongs to many relationship which stores website_id / flight_id and pricing as pivot data in a pivot table.
The custom view uses JS to send this data back to the controller in this format:
{"1":{"price_adult":"434","price_child":"545"},"2":{"price_adult":"323","price_child":"324"},"3":{"price_adult":"434","price_child":"43"}}
Trying to send this data with the request doesn't create the relations fields, and because I do not have a flight ID at the point of creating this within the controller I can not loop this JSON to make the relations manually.
Can anyone point out what the best course of action is or if there is support for this? I took a look at the docs but they are woefully short and patchy in terms of being much help.
EDIT:
I should have said I can probably make this work using a custom name attribute on the model for the relation, then add a set mutator to loop this data and update the prices relation but I don't want to go down this route if there is support for this I am missing out of the box in backpack.
EDIT2:
Someone asked about the relation:
$this->belongsToMany(Website::class, 'website_pricing')->withPivot('price_adult', 'price_child');
This is working fine its not a problem with the relation working its how can I get backpack to store the data as a relation when the flight has no ID yet, or how can I pass the data I posted above in such a way that the backpack crud controller can handle it?
You may need to create a flight first, if no flight id is being provided. Can you explain the database relational structure more?
Basically thought I should post what I did because no one could provide an answer to this.
So basically you have to copy the store / update functions from the parent, changing a few lines.
$this->crud->hasAccessOrFail('create');
// fallback to global request instance
if (is_null($request)) {
$request = \Request::instance();
}
// replace empty values with NULL, so that it will work with MySQL strict mode on
foreach ($request->input() as $key => $value) {
if (empty($value) && $value !== '0') {
$request->request->set($key, null);
}
}
// insert item in the db
$item = $this->crud->create($request->except(['save_action', '_token', '_method']));
$this->data['entry'] = $this->crud->entry = $item;
// show a success message
\Alert::success(trans('backpack::crud.insert_success'))->flash();
// save the redirect choice for next time
parent::setSaveAction();
return parent::performSaveAction($item->getKey());
Basically any line which references a function in the parent class using $this->method needs to be changed to parent::
This line is what I used to submit the relations JSON string passed to the controller as relations $item->prices()->sync(json_decode($request->input('prices'), true));
This is done after the line containing $item = $this->crud->create as the item id that just got stored will be available at that point.

Resources