Pagination of the results is not working as expected - laravel

I am using paginate() inside the controller and passing the results to view.
I have 10 results.
If I pass paginate(3 or some number higher) dd() function is giving me
html "" empty string
If I pass paginate(2) I have some results
When I pass those results to the view I have an error
::name {{$name->links()}} is not recognized
I am puzzled here. Have no idea what to do
My paginate is working normally on my previous view.
My controller
public function tagnews($team_id) {
$teamNews = Team::with('news')->find($team_id)->paginate(5);
return view ('news.tag_news', compact('teamNews'));
}
in the view file I have
{{ $teamNews->links() }}
Models are connected with pivot table
They have BelongsToMany relationship Team and News
Without the pagination everything works as expected.
When I include pagination I have all these mentioned problems

Related

Return paginated data from rebing laravel graphql server

I am using the rebing graphql laravel library and it works very fine returning json data. Currently I am trying to build a paginated table via reactjs frontend so I need to get a paginated data just the way it works normally with blade. I have used the paginate method in place of the get method but it somehow still gives same data format. This is what I have in my resolve method currently:
$models = YearModel
::with($with)
->select($select)
->where($where)
->orderByDesc('year_id');
if (isset($args['limit'])) {
$models = $models->paginate($args['limit']);
} else {
$models = $models->get();
}
return $models;
paginate and get returns same data to the frontend whereas I was expecting paginate to give some data which would include stuffs like currentPage, nextPage, previousPage as the LengthAwarePaginator class would normally do. So I believe the graphql library is somehow formating the paginated data as a normal data before returning it to the frontend. I am not sure why this is like this as I am new to graphql. Any idea or solution will be appreciated please. Thanks
I was able to get a paginated data response by changing my query return type from Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('YearModel')))) to GraphQL::paginate('YearModel') and modified my query this way:
query {
yearModels (limit: 10) {
data {
id
year
}
total
per_page
current_page
from
to
last_page
has_more_pages
}
}`
data, total, per_page, current_page, from, to, last_page, has_more_pages are fields coming from Rebing\GraphQL\Support\PaginationType getPaginationFields method.

How to retrieve data with composite primary key in Laravel

I am working with four tables in Laravel and trying to display data to a view. I believe I am stuck because one of the tables has a composite primary key.
I have the following in my controller:
public function show($id)
{
//Get application for drug
$application = PharmaApplication::where('ApplNo', $id)->first();
//Return all products for application
$drugs = $application->products;
return view('profiles.drug', compact('drugs'));
}
I have the following in my PharmaApplication model:
public function products()
{
return $this->hasMany('App\PharmaProduct', 'ApplNo', 'ApplNo');
}
I have the following in my view (which I cannot complete)
#foreach ($drugs as $drug)
<li>{{$drug->Strength}}</li>
<li>{{$drug->Form}}</li>
<li>{{$drug->Form}}</li>
#endforeach
I am trying to accomplish the following:
Get the application for a drug - this part of my code works
Return an array of objects for the products (i.e. 7 products that belong to one application). I can do this but get stuck going to the next part.
Next, I have to use the array of objects and search a table with the following columns: MarketingStatusID, ApplNo, ProductNo. I know how to query this table and get one row using DB Query, but then how do I get the proper result to that query within the for loop in my view?
Finally, I use the MarketingStatusID to retrieve the MarketingStatusDescription which I will know how to do.

Laravel Eager-loading works only when calling attribute

I've defined my Slot model to load the relations from User model like so :
public function userAssignedFull(): HasOne {
return $this->hasOne(User::class,'id','user_assigned');
}
('slots' table contains 'user_assigned' field by which I connect to User records on 'id')
The following code finds Slot model but without 'userAssignedFull'. I get only the user ID in 'user_assigned'.
$slot = Slot::with('userAssignedFull')->find($slot_id);
But calling this afterward returns me the wanted relation:
$fullUserModel = $slot->userAssignedFull;
Can anyone tell me what am I doing wrong ?
Builder::with() returns the Builder instance.
So you have to call $slot->userAssignedFull; to get the collection of data.
From the docs:
When accessing Eloquent relationships as properties, the relationship
data is "lazy loaded". This means the relationship data is not
actually loaded until you first access the property.
And this $slot->userAssignedFull; is your "first access the property".
Try this
$slot = Slot::where('id', $slot_id)->with('userAssignedFull')->first();
$slot->userAssignedFull;

laravel relations error Trying to get property of non-object

I'm trying to retrieve data from two tables and in order using a hasMany relationship. i.e
Public childModel (){
return $this->hasMany(childModel);
}
In the view when I run the foreach loop:
foreach($parentModel as $parentModel)
or
foreach($parentModel->childModel as $childModel)
then
{{parentModel->childModel}}
I get json printed on my screen just fine (including the column I want to output.)
When I try
`{{parentModel->childModel->column}}`
I get "Trying to get property of non-object"
Figured it out. I was making a 'where' statments when i initialized the parentModel variable, that 'where' statement rejected the table in the childModel. Only found out after running tests.

Laravel 5 - generic document management

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.

Resources