i have resource controller
and inside the resource controller the index function
public function index()
{
//
}
now i want to use variable from out side the function
like this
public $data = "data";
public function index() use ($data)
{
return $data;
}
i gat this error
syntax error, unexpected 'use' (T_USE), expecting ';' or '{'
i tried it as function like this
public function data()
{
$data = "data";
return $data;
}
public function index() use (data)
{
}
i gat the same error
syntax error, unexpected 'use' (T_USE), expecting ';' or '{'
so how can i access variable and function from outside the function ..
You need to use this to access to the variable:
public $data = "data";
public function index()
{
return $this->data;
}
Related
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
I have this problem. When I click on add to cart button there is an error:
Parse error: syntax error, unexpected '__construct' (T_STRING),
expecting function (T_FUNCTION) or const (T_CONST)
I'm new in Laravel and I really have no idea what I have to do,
This is my code on button add to Cart:
Add to cart
This is my route:
Route::get('add-to-cart/{id}', 'WebController#addToCart')->name('get.addToCart');
This I have in webcontroller:
public function addToCart(Request $request, $id){
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product,$product->id);
$request->session()->put('cart',$cart);
dd($request->session()->get('cart'));
return redirect()->route(get.product);
}
And this is my cart.php
<?php
namespace App;
class Cart{
public $items=null;
public __construct($oldCart){
if($oldCart){
this->$items=$oldCart->items;
}
}
public function add($item,$id){
$storedItem= ['name'=>$item];
if(this-> $items)
{
if(array_key_exists($id, this->$items)){
$storedItem=this->$items[$id];
}
}
this->$items[$id]=$storedItem;
}
}
Your __construct needs to have the word function in front of it, or better yet public function
Change the line to:
public function __construct($oldCart)
Read out here: Line no 8
there is missing function keyword before __construct
<?php
namespace App;
class Cart{
public $items=null;
public function __construct($oldCart){
if($oldCart){
$this->items=$oldCart->items;
}
}
public function add($item,$id){
$storedItem= ['name'=>$item];
if($this->items)
{
if(array_key_exists($id, $this->items)){
$storedItem=$this->items[$id];
}
}
$this->items[$id]=$storedItem;
}
}
this->$items[$id]=$storedItem;
See, this is a keyword and is $this, and when accessing variables through this no need to do the $ sign on the accessed variable (in this case items), so it should always be:
$this->items[$id] = $storedItem;
And please indent and organise your code better, this is pretty chaotic and there is probably more than one error.
You should try this:
<?php
namespace App;
class Cart{
public $items=null;
public function __construct($oldCart){
if($oldCart){
this->$items=$oldCart->items;
}
}
public function add($item,$id){
$storedItem= ['name'=>$item];
if(this-> $items)
{if(array_key_exists($id, this->$items)){
$storedItem=this->$items[$id];
}
}
this->$items[$id]=$storedItem;
}
}
show error : Missing argument 1 for App\Http\Controllers\AdminLoginController::name()
public function name($username) {
$user = AdminLogin::find($username);
return response()->json($user);
}
AdminLoginController: Its a adminlogin controller code
class AdminLoginController extends Controller{
public function show(){
$res ="Hello world!";
return response()->json($res);
}
public function log() {
$users = AdminLogin::all();
return response()->json($users);
}
public function name($username) {
$user = AdminLogin::where('username',$username)->first();
return response()->json($user);
}
RouteLoginController: Its a adminlogin controller code :
<?php
$app->get('/', function () use ($app) {
return $app->version();
});
$app->group(['prefix' => 'api/v1'], function ($app)
{
$app->get('adminlogin', 'AdminLoginController#show'); //get single route
$app->get('user', 'AdminLoginController#log'); //get single route
$app->get('username', 'AdminLoginController#name'); //get single route
$app->post('adminlogin', 'AdminLoginController#login'); //get single route
});
Error :
(1/1) ErrorException
Missing argument 1 for App\Http\Controllers\AdminLoginController::name()
Your controller method is taking the username param but the route binding is not passing one. Change your route
$app->get('username', 'AdminLoginController#name');
to
$app->get('user/{username}', 'AdminLoginController#name');
If you don't want to change your route, change your controller function signature to the below (as shown in the other answers), and make sure you are passing the 'username' as request param while invoking the url.
public function name(\Illuminate\Http\Request $request) {
$user = AdminLogin::where('username',$request->username)->first();
return response()->json(['user' => $user]);
}
You are probably calling this function using an ajax request and putting the name in the query string. In this case, the name parameter will not be sent as an attribute of the function but will be part of the request object.
You can solve this like so:
public function name(\Illuminate\Http\Request $request) {
$user = AdminLogin::find($request->username);
return response()->json($user);
}
You should try this :
public function name($username) {
$user = AdminLogin::where('username',$username)->first();
return response()->json(['user' => $user]);
}
OR
public function name(\Illuminate\Http\Request $request) {
$user = AdminLogin::where('username',$request->username)->first();
return response()->json(['user' => $user]);
}
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);
Fatal error: Call to a member function get() on a non-object in
C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Paging\application\models\name_model.php
Severity: Error
Message: Call to a member function get() on a non-object
Filename: models/name_model.php
Line Number: 13
Backtrace:
models/name_model.php:
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Line 13: $query = $this->db->get('name');
How to fix the error message?
You didn't load database libraries that why you getting this error.
Open your autoload.php file and add this string in libraries
$autoload['libraries'] = array('database');
And another way is you need to load libraries in the model
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Kindly write it as follow when your going to fetch only name(s) from your table,
public function get_name()
{
$query = $this->db->select('name')->get('your_table_name');
return $query->result();
}
It will work fine.