I am trying to display data through a query on Codeigniter, but return an error
"ATTEMPT TO READ PROPERTY "JENIS_PRODUK" ON STRING"
Here's my code on controller :
public function minimalist($id = 'minimalist')
{
$data["minimalis"] = $this->welcome_model->getMinimalis($id);
$this->load->view('frontend/minimalis.php', $data);
}
My code on Model :
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
On view I added foreach to display those data
Can you tell me what's wrong?
Please change this
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
To
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->result();
}
Related
I'm using Laravel 5.8 and I wanted to retrieve some data from an Api route:
Route::prefix('v1')->namespace('Api\v1')->group(function(){
Route::get('/articles','ArticleController#index');;
});
Then I made a Resource Collection and a Controller as well:
ArticleCollection.php:
public function toArray($request)
{
return parent::toArray($request);
}
ArticleController:
public function index()
{
$articles = Article::all();
return new ArticleCollection($articles);
}
Now it properly shows data:
{"data":[{"art_id":3,"art_cat_id":15,"art_author_id":23973,"art_title":"\u0627\u062d\u06cc\u0627\u06cc...
But when I change the Resource Collection to this:
public function toArray($request)
{
return [
'title' => $this->art_title,
'desc' => $this->art_description,
];
}
And trying to find an specific article in the Controller:
public function index()
{
$articles = Article::find(1);
return new ArticleCollection($articles);
}
I will get this error somehow:
FatalThrowableError
Call to a member function toBase() on null
So what's going wrong here? How can I solve this issue?
Change this:
public function index()
{
$articles = Article::find(1);
return new ArticleCollection($articles);
}
to this:
public function index()
{
$articles = Article::where('id',1)->get();
return new ArticleCollection($articles);
}
This is because the find returns just one record and you are converting that to a collection, by using get() you can return a collection as you intend to here.
App\Classes
public function enroll()
{
return $this->hasMany(Enrolls::class,'cid');
}
App\Enrolls
public function classes()
{
return $this->belongsTo(Classes::class);
}
controller
public function index()
{
$enrolls = Enrolls::all();
return view('home')->with('enrolls', $enrolls);
}
blade
{{$enroll->classes->title}}
I was trying to get data from enrolls table. this contains two foreign Keys.
SCREENSHOT
You should try this:
public function index()
{
$enrolls = Enrolls::with('classes')->get();
return view('home',compact('enrolls'));
}
Hi guys i am not able to see what am doing wrong even after doing research. Please assist. i am getting a NotFoundHttpException in RouteCollection.php line 161: I am trying to access the getShow method or rather the stove.show route
public function getIndex()
{
$stoves = Stove::all();
return view('stoves.index');
}
public function anyData()
{
$stoves = Stove::select(['id','stoveno', 'refno', 'manufactuerdate', 'created_at']);
return Datatables::of($stoves)
->addColumn('action', function ($stoves) {
return '<i class="glyphicon glyphicon-edit"></i> History';
})
->make(true);
}
public function addData()
{
//
return view('stoves.new');
}
public function store(AddStove $request)
{
Stove::create($request->all());
return redirect('stove');
}
public function getShow($id)
{
$stove = Stove::findorFail('$id');
return view('stoves.view', compact('stove'));
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
My route looks like
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getShow' => 'stove.show',
'getIndex' => 'stove',
]);
Route::get('newstove', 'StoveController#addData');
Route::post('newstove', 'StoveController#store');
my view folder contains index.blade, new.blade and finally view.blade
Thanks i appreciate
Try using this:
Route::get('getShow/{id}','stoveController#getShow');
If problem exist, remove this line
'getShow' => 'stove.show',
i am facing a strange problem. i have a function defined in model which i have been using from weeks now, is suddenly giving me error
ClassName and its behaviors do not have a method or closure named "getRandomPlayers"
following is my code:
Model
public function getRandomPlayers($params)
{
$criteria_obj= new CDbCriteria;
$criteria_obj->order="random()";
$criteria_obj->condition="user_id!=".$params['user_id'];
$criteria_obj->limit=7;
$random_users= Users::model()->findAll($criteria_obj);
if(!empty($random_users))
return $random_users;
else
return false;
}
Controller
public function actionInviteRandom()
{
$body_data = $this->getRequest()->getRawBody();
$data_posted = json_decode($body_data);
if(!empty($data_posted->user_id))
{
$check_valid_user=Users::model()->findByPk($data_posted->user_id);
if(empty($check_valid_user))
{
$this->sendResponse(200, array("status_code" => "002",
"status_message" => Yii::t('strings', 'User does not exist'),
)
);
}
else
{
$params=array(
"user_id"=>$data_posted->user_id,
);
$get_users= Users::model()->getRandomPlayers($params);
$count=0;
if(!empty($get_users))
{
die("here");
}
}
}
}
please check. Thanks in advance
add static to functions name :
public static function getRandomPlayers($params){ ...
UPDATE:
then you can use it like :
$get_users= Users::getRandomPlayers($params);
i just try to save input data and in line of save() method show me error , this is my controller register method :
public function register() {
if ($this->Helloworlds->save($this->request->data)) {
$id = $this->Helloworld->id;
$this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
$this->Auth->login($this->request->data['Helloworld']);
return $this->redirect('/helloworld/index');
}
else
{
$this->Session->setFlash(__('Sorry no data has saved '));
}
}
and the error :
Error: Call to a member function isNew() on a non-object File
C:\wamp\www\vendor\cakephp\cakephp\src\ORM\Table.php Line: 1092
i would mention this i'm sure about my model name'helloworlds'
any idea?
at least i find out answer , the true code is :
public function register() {
if ($this->request->is('post')) {
$Helloworld = TableRegistry::get('helloworlds'); $Helloworlds = $Helloworld->newEntity($this->request->data); if ($Helloworld->save($Helloworlds)) {
$id = $this->Helloworld->id;
$this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
$this->Auth->login($this->request->data['Helloworld']);
return $this->redirect('/helloworld/index');
} else { $this->Session->setFlash(__('Sorry no data has saved ')); } } }