In Laravel 5.2, I'm using send() method to return [] only from construct:
In the same controller:
public function __construct() {
return response()->json()->send();
}
public function index() {
return 22;
}
But actually, when call function index, I get []22;
How can I fix this, or another method can do that?
Related
I would like to reference a private function as the third parameter to the Cache::remember function.
Instead of this (try{}catch() was removed for a cleaner code):
class ApiController extends Controller
{
public function index(){
$data = Cache::remember('dataKey', 60, function () {
return Model::multipleMethodsHere()->get();
});
return response()->json($data,200);
}
}
I'd like to do this:
class ApiController extends Controller
{
public function index(){
$data = Cache::remember('dataKey', 60, $this->getIndex());
return response()->json($data,200);
}
private function getIndex(){
return Model::....->get();
}
}
I got this error if I try to reference a private function.
Argument 3 passed to Illuminate\\Cache\\Repository::remember() must be an instance of Closure, instance of Illuminate\\Database\\Eloquent\\Collection given
Is it possible ? If yes, how should I do ?
Based on comments in the discussion to the OP, re-strategize the Cache:remember to be a part of the getIndex function like:
class ApiController extends Controller
{
public function index(){
$data = $this->getIndex();
return response()->json($data,200);
}
private function getIndex(string $dataKey = 'dataKey', int $time = 60){
return Cache::remember($dataKey, $time, function () {
return Model::....->get();
})
}
}
I got an error which is undefined function.
I tried to use a public function in return:
public function CreateForm() //This the function that I want to use back
{
$names2 = DB::table('pendaftaran')
->where('isActive',0)
->orderBy('id','desc')
->get();
return view('contact')->with($variables);
}
So this a function I want to return to a function
public function AddUserSubmit(Request $request)
{
$this->validate($request,
[
'Nama'=>'required',
'NoKP'=>'required',
]);
Pendaftaran::create($request->all());
return CreateForm(); //Can I return to a public function ?
}
Yes this should work but you need to add context and use the $this keyword otherwise return CreateForm(); will be interpreted as trying to invoke a global function.
Try replacing:
return CreateForm();
With
return $this->CreateForm();
See this other question for more information: https://stackoverflow.com/a/17861505/4517964
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);
}
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: '-' }}
I have two return in my controller method.
How can I combine both?
public function index()
{
$data=Event::get(['title','start','color']);
$objectifs=Objectif::all();
$ob=Array('objectifs'=>$objectifs);
return view('newc')->with('objectifs', $objectifs);
return Response()->json($data);}
we can't use 'return' multiple times
public function index() {
$data = Event::pluck('title', 'start','color' );
return Response()->json($data);
}
use another controller function
public function jason() {
$ob=Array('objectifs'=>$objectifs);
return view('newc')->with(compact('ob'));
}
You should read this laravel docmentaion
https://laravel.com/docs/5.2
In place of using two return you can use multiple "with" like this
public function index()
{
$data=Event::get(['title','start','color']);
$objectifs=Objectif::all();
$ob=Array('objectifs'=>$objectifs);
return view('newc')->with('objectifs', $objectifs)->with('data',$data);