I'm using spatie/laravel-translatable package for translating some fields in my model.
I have model model where declated that 'title' field is translatable attribute.
When I getting list of my Model data in controller controller this field is not translated and gives all possible translations in json format result1 (this package requires that translatable fields must be json type). But then I getting title from my model getting title its gives correct translated result correct result of title
I need to when I getting all data of my model every translatable fields was translated like :
{
"id": 6,
"title":"Tbilisi"
"country_id": 4
},
Please help if it possible to get data like this. thanks.
Related
I'm trying to get the numbers of records from my notifications, where the candidate_user_id column from inside the data attribute is the same as the UserId (Authenticated User).
After I dd, I was able to get the data from all of the records in the table by using the pluck method (Line 1). I then tried to use the Where clause to get the items that I need
but this just didn't work, it was still returning all of the records in the table.
DashboardController.php:
public function index()
{
$notifications = Notification::all()->pluck('data');
$notifications->where('candidate_user_id', Auth::user()->id);
dd($notifications);
}
Here is a partial screenshot of the data that is being plucked.
How can I get the data from this, in a way like this ->where('candidate_user_id', Auth::user()->id);?
If data was a JSON field on the table you could try to use a where condition to search the JSON using the -> operator:
Notification::where('data->candidate_user_id', Auth::id())->pluck('data');
Assuming you only want this data field and not the rest of the fields, you can call pluck on the builder directly. There isn't much reason to hydrate Model instances with all the fields to then just pluck a single field from them if it is just a table field, so you can ask the database for just the field you want.
The data in the data field is a json string, so you can tell Laravel to automatically cast it as an array using the $casts property on each of the models that is notifiable.
For instance, if you have a User model which uses the trait (ie has use Notifiable), add this:
protected $casts = [
'data' => 'array',
];
If you want to access all notifications for the auth user.
$user = auth()->user();
dd($user->notifications->pluck('data'));
If you really want to do in your question way, here is how.
$notifications = Notification::all()->pluck('data');
$notifications = $notifications->where('candidate_user_id', Auth::user()->id)
->all();
This assumes you that you did not modify the default laravel notifications relationship and database migration setup. If you have modified some of the default ones, you need to provide how you modify it.
In my model (In yii2 project) I have two columns called product and code. And the issue is how to validate only code not product. We know that $model->validate() validates entire model. But I need only one input field: code. Is it possible??
More clearly, In my input form I'm using 3 models. How to validate these 3 models in my controller. That's why I'm trying to validate fields of each model separalety? I meant to validate like:
$model->validate(someField)
$anotherModel->(anotherField)
Is this possible??
You can replace this function with model. also show what you have tried so far?
public function rules()
{
return [
[['code', ], 'required'],
];
}
You can use scenario approach in validations rules, and validate only needed fields, by passing appropriate scenario.
More info about scenarios:
http://www.yiiframework.com/doc-2.0/guide-structure-models.html
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.
given this json?
[
{
"CompanyId":20,
"CompanyName":"Walmart",
"CompanyContacts":[
{
"CompanyId":20,
"FirstName":"Bob",
"LastName":"Green",
"Email":"bob#test.com",
"Phone":"1234567",
"IsActive":false
}
]
}
]
The KendoUI datasource schema.Model does not currently support nested json or json with related entities. It needs flat data. Hopefully in the future the schema.Model will support mapping complex json to flat in the model definition. However you can still use complex data in the grid you just can't define it in a schema.Model definition.
The mapping is actually done in the field definitions of the grid.
In addition see schema docs you can parse your data using the schema.parse or schema.data functions to manually transform your nested data into flat data.
Here is a fiddle example with your data
{
field : "CompanyContacts[0].FirstName",
title: "First Name"
}
Also note, if you don't need parent record CompanyName and CompanyID since you have CompanyID in your CompanyContacts in the way your data is currently defined then you can use the data attribute of the schema to indicate the starting point of your records like so
schema : {
model: mySchema,
data: "CompanyContacts"
},
I am using mvc 3 razor and problem is recording the dropdownlist value in database using razor helpers:
#Html.DropDownListFor(m => m.Question, (IEnumerable<SelectListItem>)ViewBag.QuestionList)
Here, My view is using model binding. while in database the question column is of sting data type (varchar) and while running application it shows following erros after submitting form
The ViewData item that has the key 'Question' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
What should I change here to avoid erros here I have to use model binding.
What should I change here to avoid erros here I have to use model binding.
You should ensure that inside the controller action that rendered this view you have populated the ViewBag.QuestionList property with an IEnumerable<SelectListItem>. People usually forget to do this in their POST actions when redisplaying the same view containing this DropDown:
IEnumerable<SelectListItem> items = ...
ViewBag.QuestionList = items;
return View(someModel);
Also make sure that the Question property on your model is a scalar type (string, integer, ...) and not a complex type. If it is a complex type you need to select the corresponding scalar property to bind the selected value to:
#Html.DropDownListFor(
m => m.Question.QuestionId,
(IEnumerable<SelectListItem>)ViewBag.QuestionList
)