Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4 - php

i'm using laravel v 4.2..
i want to create update record.
can you help me.. what's wrong with this code...
this is my code :
MatakuliahsController.php
public function edit($id)
{
//$matakuliahs = $this->matakuliahs->find($id);
$matakuliahs = Matakuliah::where('id','=',$id)->get();
if(is_null($matakuliahs)){
return Redirect::route('matakuliahs.index');
}
return View::make('matakuliahs.edit',compact('matakuliahs'));
}
edit.blade.php
{{ Form::open(array('autocomplete' => 'off', 'method' => 'PATCH', 'route' => array('matakuliahs.update', $matakuliahs->id))) }}
...
{{ Form::close() }}
Error is :
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\xampp\htdocs\Laravel 4\projectLaravel\app\views\matakuliahs\edit.blade.php)
thanks for your attention and your help..

What you are trying to get is a relationship on a collection of models, the relationship exists on the object in that collection. You can use first() to return the first one or you need to use loop for each one get their items
$matakuliahs = Matakuliah::where('id','=',$id)->get()->first();

$matakuliahs = Matakuliah::where('id','=',$id)->get();
return a colllection of object where the id is equal to $id. in this case will return a collection of 1 element and not the object itself of course if the id is unique, so when you do:
$matakuliahs->id
you are triying to acess the id properties of the $matakuliahs object but the $matakuliahs in this case is not a object is a collection.
to solve this problem you can do:
1.
$matakuliahs = Matakuliah::where('id','=',$id)->get()->first();
or
$matakuliahs = Matakuliah::where('id','=',$id)->first();
to get the object and acess the properties.
2. on you view:
#foreach( $matakuliahs as $matakuliah)
//your code here
#endforeach
hope this help. thanks

Try this in your controller method:
$matakuliahs = Matakuliah::find($id);
And just pass it to the view.

The method
MyModel::find($id);
Or relationship with Eloquent no works on Laravel 4.2 just next solution
$myModel= new MyModel;
$myModel->setConnection('mysql2');
$myModel= $myModel->where('id', $id)->first();

Related

Laravel 8 Property [id] does not exist on this collection instance [duplicate]

I am following Laracasts' videos: Basic Model/Controller/View Workflow.
I have a table holds contact information.
CREATE TABLE `about` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I am trying to pass data to view using the following code in the controller file:
public function index()
{
$about = Page::where('page', 'about-me')->get(); //id = 3
return view('about', compact('about'));
}
When I try to show the code as shown below,
#section('title')
{{$about->title}}
#stop
#section('content')
{!! $about->content !!}
#stop
I get error that says:
Property [title] does not exist on this collection instance. (View: E:\laragon\www\newsite\resources\views\about.blade.php)
But if I change the retrieving method in the controller file, it works.
public function index()
{
$about = Page::find(3);
return view('about', compact('about'));
}
When I use dd($about) in the first case (where()->get()) the data is encapsulated by an array. In the second case (find(3)) it displays data as expected.
What am i doing wrong?
When you're using get() you get a collection. In this case you need to iterate over it to get properties:
#foreach ($collection as $object)
{{ $object->title }}
#endforeach
Or you could just get one of objects by it's index:
{{ $collection[0]->title }}
Or get first object from collection:
{{ $collection->first() }}
When you're using find() or first() you get an object, so you can get properties with simple:
{{ $object->title }}
With get() method you get a collection (all data that match the query), try to use first() instead, it return only one element, like this:
$about = Page::where('page', 'about-me')->first();
A person might get this while working with factory functions, so I can confirm this is valid syntax:
$user = factory(User::class, 1)->create()->first();
You might see the collection instance error if you do something like:
$user = factory(User::class, 1)->create()->id;
so change it to:
$user = factory(User::class, 1)->create()->first()->id;
$about = DB::where('page', 'about-me')->first();
in stead of get().
It works on my project. Thanks.
You Should Used Collection keyword in Controller.
Like Here..
public function ApiView(){
return User::collection(Profile::all());
}
Here, User is Resource Name and Profile is Model Name.
Thank You.
$about->first()->id or
$stm->first()->title and your problem is sorted out.
As result of $about = Page::where('page', 'about-me')->get() is a laravel collection, you can get a specific attribute like title using pluck() method which is described in https://laravel.com/docs/master/collections#method-pluck.
$titles = $about->pluck( 'title' )
#foreach($titles as $title)
{{ $title }}
#endforeach
The stored information in about valiable, is in collection format, so you just need to loop through it.
#if(count($about))
#foreach($about as $page)
{{$page->title}}
#endforeach
#endif

Trying to get property 'comments' of non-object (View: /Applications/AMPPS/www/zenit/resources/views/middleware.blade.php)

I try to get comments belonging to project, a special user has posted but get this error:
Trying to get property 'comments' of non-object (View: /Applications/AMPPS/www/zenit/resources/views/middleware.blade.php)
I created a foreach loop in middleware.blade.php as follows;
#foreach(Auth::user()->courses->comments as $comment)
In My Course model I created the hasManyThrough relationship as follows
public function comments()
{
return $this->hasManyThrough(Comment::class, Project::class, 'course_id', 'project_id', 'id', 'id');
}
My projects table has a course_id column, and my comments table has a project_id column
Is there anyone who could help me? Thanks in advance
First it'll be better if you give your belongsTo relation a singular name not plural, because it retrieves only 1 object:
public function course()
{
return $this->belongsTo(Course::class);
}
And also in some cases $user->course can be null, so it won't have comments, so just check that case before foreach loop
// in some blade template
#if(auth()->user()->course)
#foreach(auth()->user()->course->comments as $comment)
{{ $comment->id }}
#endforeach
#endif

Property [title] does not exist on this collection instance

I am following Laracasts' videos: Basic Model/Controller/View Workflow.
I have a table holds contact information.
CREATE TABLE `about` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I am trying to pass data to view using the following code in the controller file:
public function index()
{
$about = Page::where('page', 'about-me')->get(); //id = 3
return view('about', compact('about'));
}
When I try to show the code as shown below,
#section('title')
{{$about->title}}
#stop
#section('content')
{!! $about->content !!}
#stop
I get error that says:
Property [title] does not exist on this collection instance. (View: E:\laragon\www\newsite\resources\views\about.blade.php)
But if I change the retrieving method in the controller file, it works.
public function index()
{
$about = Page::find(3);
return view('about', compact('about'));
}
When I use dd($about) in the first case (where()->get()) the data is encapsulated by an array. In the second case (find(3)) it displays data as expected.
What am i doing wrong?
When you're using get() you get a collection. In this case you need to iterate over it to get properties:
#foreach ($collection as $object)
{{ $object->title }}
#endforeach
Or you could just get one of objects by it's index:
{{ $collection[0]->title }}
Or get first object from collection:
{{ $collection->first() }}
When you're using find() or first() you get an object, so you can get properties with simple:
{{ $object->title }}
With get() method you get a collection (all data that match the query), try to use first() instead, it return only one element, like this:
$about = Page::where('page', 'about-me')->first();
A person might get this while working with factory functions, so I can confirm this is valid syntax:
$user = factory(User::class, 1)->create()->first();
You might see the collection instance error if you do something like:
$user = factory(User::class, 1)->create()->id;
so change it to:
$user = factory(User::class, 1)->create()->first()->id;
$about = DB::where('page', 'about-me')->first();
in stead of get().
It works on my project. Thanks.
You Should Used Collection keyword in Controller.
Like Here..
public function ApiView(){
return User::collection(Profile::all());
}
Here, User is Resource Name and Profile is Model Name.
Thank You.
$about->first()->id or
$stm->first()->title and your problem is sorted out.
As result of $about = Page::where('page', 'about-me')->get() is a laravel collection, you can get a specific attribute like title using pluck() method which is described in https://laravel.com/docs/master/collections#method-pluck.
$titles = $about->pluck( 'title' )
#foreach($titles as $title)
{{ $title }}
#endforeach
The stored information in about valiable, is in collection format, so you just need to loop through it.
#if(count($about))
#foreach($about as $page)
{{$page->title}}
#endforeach
#endif

Laravel htmlentities() expects parameter 1 to be string

I am trying to fetch data from a table by using a custom class .But it says htmlentities() expects parameter 1 to be string.
This is My DateTimeFormat Class .Here vitals is a table.which have vita_name attribute.
public static function get_vital_details($vital_id)
{
$result = DB::table('vitals')
->select('vita_name')
->where(['id' => $vital_id])
->get();
return $result;
}
This is the view i am trying to access the data .
<?php $vitalsinfo=DateTimeFormat::get_vital_details($vitaldetails->vital_id) ?>
#foreach($vitalsinfo as $vitalsinfo)
{{$vitalsinfo}}
#endforeach
i Am new to laravel .Any suggestions would be more than welcome. Thank You
You're trying to dipslay object as string, so try to use first() instead of get() to get just one object instead of collection:
$result = DB::table('vitals')
->select('vita_name')
->where(['id' => $vital_id])
->first();
And do just this (instead of #foreach construction) to display property of an object:
{{ $vitalsinfo->vita_name }}

Using ::find but with a custom where clause

I use ::find to find the business but business_position I need to use WHERE business_id = and WHERE id to find the correct position, and example is avalible below...
<?php
$s = Stats::find(Auth::user()->id);
$myJob = \App\Businesses::find($s->business_id);
$matchedValues = ['business_id' => $s->business_id, 'id' => $s->business_position];
$myPosition = \App\BusinessPositions::where($matchedValues)->get();
?>
And using $myPosition I use
{{ $myPosition->pay_amount }}
And on that line it throws and error...
Undefined property:
Illuminate\Database\Eloquent\Collection::$pay_amount (View:
C:\Users\Darren\MyLavProject\resources\views\pages\business\business.blade.php)
Laravel's get() returns a collection of results that match your query (even if there are none or just one, it'll be an array of Laravel model instances).
You can use first() instead of get() to grab the first result, or you can foreach through each of the results get() provides. It depends if your application permits more than one permission per business_id/id combination.
$myPosition This is an array of objects.
So if you need the result:
{{ $myPosition[0]->pay_amount }}
Or
#foreach($myPosition as $position)
{{ $position->pay_amout }}
#endforeach
Of if you don't want to use any of these:
Change your query to :
$myPosition = \App\BusinessPositions::where($matchedValues)->first();
Now you can use: {{ $myPosition->pay_amount }}

Categories