I am a beginner.
I'm trying to fix, i think i need help.
-This is code Controller
if(Input::hasFile('image')){
$dest = 'media/images/product/';
$name = str_random(6).'_'.Input::file('image')->getClientOriginalName();
//$resize =
Input::file('image')->move($dest,$name);
}
$loaispname = Input::get('loaispname');
$loaisp = new Loaisp;
$datas = $loaisp->getidloaisp($loaispname);
$idloaisp = $datas->id;
$item = new Sanpham;
$item->loaisp_id = $idloaisp;
$item->sanpham_name = Input::get('sanpham');
$item->sanpham_img = $name;
$item->sanpham_tieude = Input::get('tieude');
$item->sanpham_gia = Input::get('gia');
$item->sanpham_chitiet = Input::get('chitiet');
$item->sanpham_vitri = Input::get('vitri');
$item->save();
return Redirect::to('admin/dsachsanpham')->with('thanhcong','Saved');
-This is code Model
public function getidloaisp($loaispname){
//return Loaisp::where('loaisp_name','=',$loaispname)->get();
return DB::table('loaisp')->where('loaisp_name',$loaispname)->first();
}
-this is Error
ErrorException (E_UNKNOWN)
Trying to get property of non-object
Open: E:\xampp\htdocs\www\daunhot\app\controllers\AdminController.php
$loaispname = Input::get('loaispname');
$loaisp = new Loaisp;
$datas = $loaisp->getidloaisp($loaispname);
$idloaisp = $datas->id; // This is error
$item = new Sanpham;
When you see this error there could be two error: You got nothing back with your query(DB::table('loaisp')->where('loaisp_name',$loaispname)->first();), Or you got back an array, if you are using ->get() instead of ->first() you will get this error.
Try to use a foreach on your query result, or try to use dd() function on your result so you can see if is empty or not. If it is empty that is your porblem.
Same as what the rest is saying, dump $datas before the error, maybe add '=' to your where clause in that 'getidloaisp' function. You should be able to use the one your quoted out though, that is if you change the '->get()' to '->first()'.
Check if you are getting 'loaispname' on Input::get by:
echo "<pre>";
print_r($loaispname);
echo "</pre>";
exit;
If you're getting your posted data then do:
echo "<pre>";
print_r($datas);
echo "</pre>";
exit;
$datas->id work with DB table is need to be the table datas and cell id, and also they are case sensitive, not use Id or other.
Related
i have use explode variable to get the value using get method but that return only one value
$part_id=explode(",",$request->input('part_id'));
$get_ltr = part::where('status',1)->where('part_no',$part_id)->get();
foreach($get_ltr as $key =>$value)
{
$ltr[$key] = $value['ltr'];
}
that gives me only one record what is issue in my code please help me
Look like you should use whereIn().
$ids = explode(',', $request->input('part_id'));
$get_ltr = part::where('status',1)->whereIn('part_no', $ids)->get();
foreach($get_ltr as $key =>$value){
$ltr[$key] = $value['ltr'];
}
You should try this:
$color_id=explode(",",$request->input('color_id'));
$part_id=explode(",",$request->input('part_id'));
$qty=explode(",",$request->input('qty'));
$qty_ltr = [30,15];
$get_ltr = part::where('status',1)->whereIn('part_no',$part_id)->get();
foreach($get_ltr as $key =>$value)
{
$ltr[$key] = $value['ltr'];
}
I'm getting this error whenever trying to upload and excel file
My function is
use Illuminate\Support\Facades\Input;
use Excel;
function:-
public function importProduct(){
$file = Input::file('file');
$file_name = $file->getClientOriginalName();
$file->move('excelData/',$file_name);
$result = \Excel::load('excelData/'.$file_name, function($reader){
$reader::all();
})->get();
return json_encode($result);
}
$result = \Excel::load('excelData/'.$file_name, function(LaravelExcelReader $reader){
$reader::all();
})->get();
I think this should work, if you are using 2.1 version of excel
Two Steps
1.First get the Path
$path = $request->file('file')->getRealPath();
2. Get data in array
$data = Excel::load($path)->get();
Now we can Play with data array
I am trying to simply iterate though a result set in laravel on the controller side. This is what I tried but I get the following error:
Cannot use object of type stdClass as array
Controller snippet:
$result = DB::select($query);
foreach($result as $r){
echo $r['email'];
}
I appreciate any help with this,
Thanks in advance!
You need to use it as an object:
$result = DB::select($query);
foreach($result as $r){
echo $r->email;
}
Or if for some reason you want to use it as array, you need to convert it first:
$result = DB::select($query)->toArray();
foreach($result as $r){
echo $r['email'];
}
After pulling data from database you can convert it to array but it is optional, it's depends on your necessity then you can simply use the foreach to loop through to each distinct object and get access their property
$data = DB:: table('categories')
->where('parent_id', $request->value)
->get()->toArray();
$output = '<option value="">Select '.ucfirst($request->dependent).'</option>';
foreach($data as $row){
$output.= '<option value="'.$row->id.'">'.$row->title.'</option>';
}
return response($output, 200);
The Laravel Collection class supports the standard methods for iterables: map, sort, filter etc. see: vendor/laravel/framework/src/Illuminate/Collections/Collection.php
if you're querying on a model:
$results = MyAwesomeModel::where('something' '=' 'other')->get();
//this is valid
$results->map(function($result) {
echo $result->field;
});
//this is also valid
for($i = 0; $i < sizeof($results); $i++){
echo $results[$i]->field;
}
I would suggest avoiding ->toArray() on hydrated results (instances of models) because you loose all the model properties.
I'm wonder how can I get custom attribute,
My custom attribute call "tim_color"
I was try get by $_product->getAttributeText('tim_color');
after execute I get fatal error Call to a member function getAttributeText() on a non-object
when I'm used
$data['color'] = $product->getTim_color();
in the result i'm get id, but I need the name of atrribute, how can I resolve this problem
My code of script:
$mage_csv = new Varien_File_Csv(); //mage CSV
$products_model = Mage::getModel('catalog/product')->getCollection();; //get products model
$products_model ->addAttributeToSelect('*');
$products_row = array();
foreach ($products_model as $prod)
{
#print_r($prod);
$product = Mage::getModel('catalog/product')->load($prod->getId());
$data = array();
$data['id_product'] = $product->getId();
$data['color'] = $product->getTim_color();
$data['sku'] = $product->getSku();
$data['name'] = strip_tags($product->getName());
$data['description'] = trim(preg_replace('/\s+/', ' ', strip_tags($product->getDescription())));
$data['price'] = $product->getPrice();
$products_row[] = $data;
}
thx for help
Try,
$_product->getData(’tim_color’);
I hope you can get the attribute value by getAttributeText() also. Check out,
$_product = Mage::getModel('catalog/product')->load($item->getId()); //getting product
$_product->getAttributeText('tim_color'); //getting custom attribute value
Detailed discussions are here and here.
I am running the following query in codeiginter, I want to return the data from the database but I'm getting a blank array as a response. My attempts to debug this issue have shown that the return value from the model may be the issue. I need a extra pair of eyes on this so if anyone can help me let me know.
Model
function test($term)
{
$sql = "select description from category where title = '$term'";
$query = $this->db->query($sql);
return $query->result();
}
Controller
function test()
{
$this->load->model('test');
$term = $this->input->post('term',TRUE);
$rows = $this->test->test($term);
echo json_encode($rows);
}
your code looks correct... try printing the last query that was made and check it out
COntroller
function test()
{
$this->load->model('test');
$term = $this->input->post('term',TRUE);
$rows = $this->test->test($term);
echo $this->db->last_query();exit; //this gives you the last query that was made.
echo json_encode($rows);
}
check if the posted value is correct... you can even try running this in mysql and see if this is returning any rows...
use echo $this->db->last_query(); and get you query and try it with your mysql editor
I notice by running echo $this->db->last_query(); the value of $term had a leading white space that cause the query to return null. by adding $term = trim($term, " "); I was able to remove the leading white space and get the correct result.
$this->load->model('tp_model');
$term = $this->input->post('term',TRUE);
$term = trim($term, " ");
$rows = $this->tp_model->test($term);