I get my $id from the url, but I couldn't find a way to use it in a function. How can I pass $id to the query function?
I tried global $id but I still can't get a value, just empty page.
My controller.
class BookController extends Controller
{
public function bookIndex($id, $slug)
{
// echo $id works without a problem
$findcover = Thing::with(array('cover' => function($query) {
$query->where('imageable_type', 'App\Models\Thing')
->where('imageable_id', $id);
}))->find($id);
$maincover = $findcover->cover;
}
Error:
ErrorException in BookController.php line 49:
Undefined variable: id
Line 49
->where('imageable_id', $id);
class BookController extends Controller
{
public function bookIndex($id, $slug) {
// echo $id works without a problem
$findcover = Thing::with(array('cover' => function($query) use ($id) {
$query->where('imageable_type', 'App\Models\Thing')
->where('imageable_id', $id);
}))->find($id);
$maincover = $findcover->cover;
}
Add the reserve word use to insert your $id along your query.
Related
I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}
I've been trying to do this for a while now. Most of the time, I solve it by using accessors. I'm currently trying to get if the column exists and I created a function in my model which is suppose to return boolean.
Model code:
class Inventory extends Model
{
protected $attributes = ['inventory'];
protected $appends = ['colorThumb'];
public function hasAttribute($attr){
return array_key_exists($attr, $this->attributes);
}
}
Controller code:
public function allInvOperation(Request $request){
$inv = Inventory::where('is_deleted', 0)->with(['product', 'size','color'])->orderByDesc('id')->get();
if(!is_null($request->searchText)){
dd($inv->hasAttribute('inventory'));
$inv = Inventory::where('is_deleted', 0)->with(['product', 'size','color'])->orderByDesc('id');
if($request->inv_filter == 'inventory'){
$inv = $inv->where('inventory', 'like', "%".$request->searchText."%")->get();
}
if($request->inv_filter == 'code'){
$inv = $inv->whereHas('product', function ($q) use ($request){
$q->where('code', "%".$request->searchText."%");
})->get();
}
}
ERROR
Method Illuminate\Database\Eloquent\Collection::hasAttribute does not exist.
The code you are doing hasAttribute on is a Collection of objects, you need to use first on that query to get a single result on which you can later do hasAttribute
This is my controller : Admin
class Admin extends CI_Controller {
public function index()
{
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
$this->load->view('Admin_view',$data);
}
}
This my model : Dash_model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
This my view : Admin_view
<?php echo $jumlah_instansi; ?>
Please help me, sorry newbie..Thank you..
Thats show error
Message: Undefined property: Admin::$Dash_model
Filename: controllers/Admin.php
and
Message: Call to a member function jml_instansi() on a non-object
You must first load a model before accessing it, e.g.
public function index()
{
$this->load->model('Dash_model');
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
// ...
}
See Loading a Model for more.
Try like this.You are returning integer value of count so no need to use result() result set in controller.
Controller:
class Admin extends CI_Controller {
public function index()
{
$this->load->model('Dash_model');//load model
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi();
$this->load->view('Admin_view',$data);
}
}
Model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
View:
<?php echo $jumlah_instansi; ?>
Hi I am passing a user_id into a my user model and joining this to the profile table to retrieve the first name of the user as so:
public function scopefirstnameByUserId($id)
{
return static::where('users.id','=',$id)->join('profiles', function ($join,$id) {
$join->where('profiles.user_id', '=', $id);
})->pluck('firstname');
}
usage:
User::firstnameByUserId(2);
However I get the following error:
Missing argument 2 for User::{closure}()
Edit#
The function has now been updated to:
public function scopefirstnameByUserId($id)
{
return static::where('users.id','=',$id)->join('profiles',function ($join) use ($id) {
$join->where('profiles.user_id', '=', $id);
})->pluck('firstname');
}
but now throws this error:
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
Any ideas what I'm doing wrong?
In your code:
function ($join,$id) {
}
Should be:
function ($join) use ($id) {
$join->where('profiles.user_id', '=', $id);
}
I am attempting to use and id that being passed into the URL to edit the query but when attempting to run the code I get an error of:
Call to a member function where() on a non-object
Controller
class HomeController extends BaseController {
public function showWelcome() {
$id = intval($_GET['wab_id']);
$results = DB::Table('films')->get()->where('wab_id','=', $id);
print_r($results);
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
I have also tried:
class HomeController extends BaseController {
public function showWelcome() {
$id = intval($_GET['wab_id']);
$results = DB::Table('films')->get()->where('wab_id', $id);
print_r($results);
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
Only to be thrown the same error message and this is the only snippet that has not thrown the error but returns with a error of:
mysql_fetch_array() expects parameter 1 to be resource, object given
class HomeController extends BaseController {
public function showWelcome() {
$id = intval($_GET['wab_id']);
$results = DB::Table("SELECT * FROM next WHERE wab_id=$id");
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
Route
Route::get('/', array(
'as' => 'home',
'uses' => 'HomeController#showWelcome'
));
You need to change the order of the chaining to put the where() function before the get() function. Anything after get() will be applied to an Collection rather than the Query Builder.
If you want to pass a parameter through the URL try:
// routes.php
Route::get('/{wab_id}', array(
'as' => 'home',
'uses' => 'HomeController#showWelcome'
));
Also, the where() constraint should be before the get() method.
Now you should be able to accept the URL parameter as an argument into your HomeController#ShowWelcome function
class HomeController extends BaseController {
public function showWelcome($wab_id) {
$id = $wab_id;
// where() constraint before the `get()` method.
$results = DB::Table('films')->where('wab_id','=', $id)->get();
print_r($results);
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
i was trying to pass an std Obj array to the view, but i had to set wab_id because if not i would get a undefined index of wab_id next i
$arrays = json_decode(json_encode($results), true); in order to get the std array that i was getting with using the query into a multi array then looped through the array and that worked
thanks everyone for your help.
Controller
class HomeController extends BaseController {
public function showWelcome(){
$id = isset($_GET['wab_id'])?$_GET['wab_id']:"";
$results = DB::Table('films')->where('wab_id', $id)->get();
$arrays = json_decode(json_encode($results), true);
foreach ( $arrays as $film ) {
}
return View::make('hello')->with('film', $film);
}
}
You get a mysql_fetch_array error because when you messed up the order of the get() and where() functions
//you did it like this
DB::table('films')->get()->where('wab_id','=', $id);
//you did it like this, instead of
DB::table('films')->where('wab_id','=', $id)->get();
the get() is ignored. that is why the result will not be an array;
EDIT :
If you use the toArray() helper function you wont need to encode and decode it as Json.
//you can do so like this
DB::Table('films')->where('wab_id','=', $id)->toArray();
//or this if thats too long
$result = DB::table('films')->where('wab_id','=', $id);
$array = $result->toArray();