i want my code to keep working if the table return null
i tried using if(empty) bla bla bla, but is not seem to be the problem
the problem is laravel don't let me call function on null
#php($article = article::find($id)->get())
error : "Call to a member function get() on null"
If aritcle::find($id) returns null, it should solve the problem:
#if($article = article::find($id) != null)
// If the aricle::find($id) returns anything but null, this block will be reached.
#endif
I think that is better to you to send the data to the view from the controller, instead of using blade directives to fetch data.
something like this:
//Within some controller:
public function show($id)
{
$article = article::find($id);
return view('your-view')->with($article);
}
Then you can check if $article is null using blade:
#if(empty($article))
// The article is empty
#endif
Using ->get() after find() is unnecessary. find() automatically returns the model that matches the primary key provided. You will get errors if you attempt to access $article after find() fails, but it will stop you from getting the error you posted.
Related
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
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
I try to pass model to view.blade
when model is not null , blade file run without bugs
but if model is null , blade file run bugs
this is my code
//StudentController code
public function viewstudent($id)
{
$student = Student::find($id);
$outputs = array($student);
return view('student',['students'=>$outputs]);
}
//student.blade.php code
<table id="myTable">
#foreach ($students as $student)
<tr>
<td>{{$student->studentname}}</td>
<td>{{$student->studentlevel}}</td>
<td>{{$student->studentgender === 1 ?'Male':'Female'}}</td>
<td>{{$student->studentbirthdate}}</td>
<td>{{$student->studentnotes}}</td>
<td>{{$student->created_at}}</td>
<td>{{$student->updated_at}}</td>
</tr>
#endforeach
</table>
Of course it is an error because you're trying to access properties on a non-object. Your array would have one element of null.
This is a bad design because if the student doesn't exist, you should not be returning the view to begin with. You should be throwing some error, possibly a HTTP 404.
Luckily, Laravel makes this easy. You can use Student::findOrFail($id); to throw an exception when the student does not exist.
It seems strange to me that you're looping through students in your view when your controller is only returning one student. If for some reason you don't want a 404, you could always run $outputs through array_filter to get rid of the null value, or just not add it to $outputs when it is null..
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
I am working on a Laravel project in which I need to write a custom function, but when I call this function Laravel says:
Laravel : htmlentities() expects parameter 1 to be string, array given
Here is my function:
public static function get_checkup_time(){
$user_logged_in = Auth::user()->foreignkey_id;
$result = DB::table('doctors')
->select('checkuptime')
->where(['id'=>$user_logged_in])
->get();
return $result;
}
And this is my view in which I am trying to invoke this function .
#if(Auth::user()->userrolebit ==2)
{{
DateTimeFormat::get_checkup_time()
}}
#endif
i don't know what I am doing wrong here.
where() expects string as first parameter, so change this:
->where(['id'=>$user_logged_in])
to this:
->where('id', $user_logged_in)
If you are trying to return just checkup time you should do this in your method
public static function get_checkup_time()
{
$user_logged_in = Auth::user()->foreignkey_id;
$result = DB::table('doctors')
->select('checkuptime')
->where('id', $user_logged_in)
->first();
return $result->checkuptime;
}
Edit:
Following a downvote, I've seen that mine wouldn't work as well because the ->get() call should be replaced with ->first() as in #Paul's answer. See my comment below.
The $result you are returning from the method is not a string.
Therefore {{ DateTimeFormat::get_checkup_time() }} is the one returning the error.
Returning something like $result->checkuptime should do it.