Call to a member function diffForHumans() on null - laravel

i am getting outputs of my posts in the system but i am getting this errror Call to a member function diffForHumans() on null... Can u please help me? Thanks...
Article model is that:
protected $fillable=[
'user_id','content','live','post_on'
];
public function setLiveAttribute($value)
{
$this->attributes['live']=(boolean)($value);
}
And the articlescontroller is that
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function create()
{
return view('articles.create');
}
public function store(Request $request)
{
Article::create($request->all());
}
public function show()
{
}
public function update()
{
}
public function destroy()
{
}

The problem is for sure not here. You probably try to run diffForHumans() in your view for example like this:
$user->post_on->diffForHumans()
but post_on is set to null instead of date.
So you should verify where exactly you run this method, set this column as date and in your view add something like this:
Posted: {{ $date ? $date->diffForHumans: '-' }}

Related

How to get model relations in Laravel?

I would like to get the model relations, in array;
My model look like:
class User extends Model
{
public function profile() {
return $this->haOne(Profile::class);
}
public function settings() {
return $this->morphOne(Settings::class, 'settingsable');
}
public function addresses() {
return $this->hasMany(Addresses::class);
}
}
And my code:
$user = User::with(['profile', 'settings', 'addresses'])->find(1);
$user->getRelations(); // return ['profile', 'settings', 'addresses'];
If I have more then 10 relation, I don't want to list all.
I would like to get like this:
$relations = ['profile', 'settings', 'addresses'];
Is this posible?
You could try adding a scope to the model, and so, you have to only write them once.
class User extends Model
{
public function profile() {
return $this->haOne(Profile::class);
}
public function settings() {
return $this->morphOne(Settings::class, 'settingsable');
}
public function addresses() {
return $this->hasMany(Addresses::class);
}
public function scopeWithRelations(Builder $query){
return $query->with([...]);
}
}
$users = User::withRelations()->get();
This way you only have to write them once there, and everywhere in the code you'll use the scope.
Not exactly 100% what you're asking, but this could be a solution.

Laravel Nova metrics filtering

I have a model called Property which has an 'active' flag. I want a metric at the top of my resource which shows a count of active Properties.
My calculate method is exactly as in the doc but this shows all Properties rather than active ones:
public function calculate(Request $request)
{
return $this->count($request, Property::class);
}
How can I add a filter?
I've tried a where clause:
public function calculate(Request $request)
{
return $this->count($request, Property::class)->where('active','=',1);
}
And a query scope:
public function calculate(Request $request)
{
return $this->count($request, Property::class)->active();
}
I thought I might be able to use the Nova filter I set up on the resource list page but that didn't seem to work either. I'm sure it's really easy but I haven't worked it out. Thanks for your help!
Your can use every type of Eloquent\Builder instance in the $model param.
Instead of:
public function calculate(Request $request)
{
return $this->count($request, Property::class);
}
Set a Scope on your Model
App\Property.php
...
public function scopeActive($query)
{
return $query->where('active', 1);
}
public function scopeInactive($query)
{
return $query->where('active', 0);
}
And use this scope as the $model param in your calculate method, because the call of the scope returns a Eloquent\Builder Instance
public function calculate(Request $request)
{
return $this->count($request, Property::active());
// return $this->count($request, Property::inactive());
}
Edit
Of course you can make the Eloquent Builder call inline:
public function calculate(Request $request)
{
return $this->count($request, Property::where('active', 1));
}

accessing custom method in model

Im practicing laravel and im making a custom method for my user
In my user model i have build a function like this
public function employee(){
return $this->where('user_type','employee');
}
and then in my controller I'm accessing the function like this
public function index(){
$users = User::latest()->employee();
return UserResource::collection($users);
}
but it return an error Method Illuminate\Database\Query\Builder::employee does not exist.how to fix this?
Use local scope instand
public function scopeEmployee($query)
{
return $query->where('user_type', 'employee');
}
Your controller can be as it was !
public function index(){
$users = User::latest()->employee()->get();
return ProductsResource::collection($users);
}

How to redirect store to update method?

How to redirect store to update method? I tryed the following code:
public function store(ProductRequest $request)
{
return $this->update(new Product, $request);
}
public function update(Product $product, ProductRequest $request)
{
// code
}
However, the first parameter of update need an already in database user and the above code does not work as expected. (it update the entire users in db!)
What is the correct way to achieve that?
public function store(UserRequest $request)
{
return $this->maintain(new User, $request);
}
public function update(User $user, UserRequest $request)
{
return $this->maintain($user, $request);
}
private function maintain($user, $request)
{
//code;
}
The model for the update method could be the problem, your code is okay for this part:
public function store(Request $request)
{
return $this->update(new Product, $request);
}
public function update(Product $product, Request $request)
{
$product->fill($request->all())->save();
// code
}
For example, with route model binding:
Route::resource('products', 'ProductController');
Route::model('products', App\Product::class);
Or with a custom binding:
Route::resource('products', 'ProductController');
Route::bind('products', function($param) {
return Product::where('slug', $param)->first();
});
Make sure you are not using get() in custom binding, it will pass back a collection.

error for inserting data in database in view in joomla

I want to insert a data in database but it show error
Fatal error: Call to a member function getform() on a non-object in
\components\com_enquiry\views\form\view.html.php
on line 9
my code is:
enqiry.php:
public function getform($data)
{
$db=JFactory::getDbo();
$db=$this->getDbo();
$query=$db->getQuery(true);
$reg=new stdClass();
$reg->name=$data['name'];
$reg->email=$data['email'];
$reg->phone=$data['phone'];
$reg->comments=$data['comments'];
$reg=$db->insertObject('#__enquiry',$reg);
}
and view.html.php:
public function display()
{
$this->msg = 'enquiry form';
$model=$this->getModel();
$data =$model->getform();
$this->assignRef('data', $data );
parent::display();
parent::display();
}
controller:
class enquiryController extends JControllerLegacy
{
public function display()
{
$vname=JRequest::getCmd('view','form');
JRequest::setVar('view',$vname);
JRequest::setVar('layout','default');
parent::display();
}
public function show()
{
$data['name']=$_POST['name'];
$data['email']=$_POST['email'];
$data['phone']=$_POST['phone'];
$data['comments']=$_POST['comments'];
$sname=JRequest::getCmd('view','thanx');
JRequest::setVar('view',$sname);
print_r($data);
}
}
could someone help me solve this error?
The problem is that your model is not loaded by the line:
$model=$this->getModel();
This either means that your model in enquiry.php is not named correctly
ComponentNameModelEnquiry
or that your view is not named enquiry.
You can try to specify which model you want, like
$model = $this->getModel('enquiry');

Resources