im still newbie, and trying to save data from REAXML file.
vendor for parsing REAXML from https://github.com/i4ucode/reaxml
Route:
Route::get('/admin/synctest', 'Admin\\AdminController#synctest');
Model Property
class Property extends Model
{
protected $table = 'properties';
protected $primaryKey = 'id';
protected $fillable = ['refID', 'propkind_id', 'other column'];
public function Kind()
{
return Propkind::findOrfail($this->propkind_id)->name;
}
//other public function
}
Model Propkind
class Propkind extends Model
{
protected $table = 'propkinds';
protected $primaryKey = 'id';
protected $fillable = ['id', 'name', 'slug'];
public function Property(){
return $this->hasMany('App\Property');
}
//other public function
}
Controller
public function synctest()
{
// get new class
$processor = new \REA\XmlProcessor();
$directories = Storage::disk('reaxml')->files();
foreach ($directories as $zipFile) {
//get XML file
$files = file_get_contents(url('feed/' . $zipFile));
//process the xml
$properties = $processor->parseXmlString($files);
//loop
foreach ($properties as $property) {
Property::updateOrCreate(
['refID' => $property->getRefID()],
[
//other code
'propkind_id' => Propkind::where('name', $property->getCategoryNaskleng())->first()->id, ===> ErrorException (E_NOTICE) Trying to get property of non-object
]
);
}
}
}
this piece of code Propkind::where('name', $property->getCategoryNaskleng())->first()->id)
==>show data but trow
ErrorException Trying to get property of non-object.
some kind of enlightenment, wisdom, very appreciate.
the difference between dd() and dump() is that dd() stops the execution of the script (die), dump() shows a representation of the argument but it doesn't stop the script.
for this reason I guess that the error (notice) comes after the excution of this piece of code you have posted.
clearly one of the values of $properties doesn't return a model
after several hours i found that null is the problem.
thank you #Devon for insight. #simonecosci and #Leena Patel for your comment.
Related
I am trying to call a variable in my Models/Images.php which is for a very good reason but cannot.
My variable $langtitle = 'title'.config('app.LANG_COLUMN_CODE'); which I want to use instead of 'title' within protected $searchable
Here is my whole code:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\Traits\Search;
class Images extends Model
{
use Search;
protected $guarded = array();
public $timestamps = false;
protected $fillable = [
'title', 'description', 'categories_id', 'metakeywords', 'hash', 'subgroup','subcategories_id'];
protected $searchable = [
'title',
'metakeywords',
'subgroup'
];
public function user() {
return $this->belongsTo('App\Models\User')->first();
}
public function category() {
return $this->belongsTo('App\Models\Categories', 'categories_id');
}
public function subcategories() {
return $this->belongsTo('App\Models\Subcategories', 'subcategories_id');
}
public function tags() {
return $this->hasMany('App\Models\Images', 'metakeywords');
}
}
I get errors like Constant expression contains invalid operations
As $searchable is getting called somewhere, better to use it directly where this $searchable is gettinh called.
See for $this->searchable and replace with:
$langtitle.",".'subgroup, metakeywords';
Hint: As I can understand, this might be in Search Traits!
I have these three models in Laravel-5.8
GoalType
class GoalType extends Model
{
protected $table = 'goal_types';
protected $fillable = [
'id',
'name',
];
public function goals()
{
return $this->hasMany('App\Models\Goal');
}
public function ratinglimit()
{
return $this->belongsTo(App\Models\RatingLimit);
}
}
RatingLimit
class RatingLimit extends Model
{
protected $table = 'rating_limits';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'max_rating',
];
public function goaltype()
{
return $this->hasOne('App\Models\GoalType','goal_type_id','id');
}
}
Goal
class Goal extends Model
{
protected $table = 'goals';
protected $fillable = [
'id',
'goal_type_id',
'goal_title',
];
public function goaltype()
{
return $this->belongsTo('App\Models\GoalType','goal_type_id');
}
}
Controller
$goals = Goal::where('is_approved', 3)->get();
A RatingLimit is associated with one GoalType (hasOne) and GoalType has many Goal
When I tried to render max_rating
index.blade
{{$goal->goaltype->ratinglimit->max_rating}}
I got this error:
Trying to get property 'max_rating' of non-object
When I changed it to:
{{ $goal->goaltype->ratinglimit ? $goal->goaltype->ratinglimit->max_rating : '-' }}
it deisplays '-', and the error is no more there. But max_rating in the database is not empty
How do I resolve this?
Thanks
->get() returns a collection! so you should loop it through foreach to have access to each item and then you can use your relation:
#foreach ($goals as $g)
{{$goal->goaltype->ratinglimit->max_rating}}
#endforeach
I have one-to-one database. It's Telepon in different table. So, every 'siswa' has one 'telepon' (telephon number). Telepon has 'sometimes' type not 'required'. It can be blank.
But, if it set blank, error "Creating default object from empty value" appears when i do update.
Code in Telepon Model :
class Telepon extends Model
{
protected $table = 'telepon';
protected $primaryKey = 'id_siswa';
protected $fillable = [
'id_siswa',
'nomor_telepon',
];
public function siswa() {
return $this->belongsTo('App\Siswa', 'id_siswa');
}
Code in Siswa Model for Telepon:
public function telepon() {
return $this->hasOne('App\Telepon', 'id_siswa');
}
This in Controller for update:
public function update(Siswa $siswa, SiswaRequest $request){
$input = $request->all();
$siswa->update($input);
$telepon = $siswa->telepon;
$telepon->nomor_telepon = $request->input('nomor_telepon');
$siswa->telepon()->save($telepon);
$siswa->hobi()->sync($request->input('hobi_siswa'));
return redirect('siswa');
}
Use the null coalesce operator ?? to check for null or create a new model instance:
$telepon = $siswa->telepon ?? new Telepon();
I got a problem when I try to fetch data from table to select.
here is my Mahasiswa model :
protected $fillable = ['nim','nama','alamat','jenis_kelamin','no_tlp','email','tempat','tanggal','link','id_jurusan'];
protected $table = 'mahasiswa';
public function jurusan(){
return $this->belongsTo('App\Jurusan');
}
here is my MahasiswaController :
public function create()
{
$data['data']= Mahasiswa::with('jurusan')->get();
return view('Mahasiswa.mahasiswaInsert',$data);
}
and here is my blade view :
{{ Form::select('id_jurusan',['' => 'Pilih Jurusan']+$data ,1,['id' => 'jurusan', 'style' => 'display:inline-block','class' => 'blue-text']) }}
I just wanna get the data from table jurusan and fetch to the select ?? how to do that?? Thanks in advance, and sorry for my bad grammar.
Change your model.
protected $table = 'mahasiswa';
protected $fillable = ['nim','nama','alamat','jenis_kelamin','no_tlp','email','tempat','tanggal','link','id_jurusan'];
public function jurusan(){
return $this->belongsTo('App\Jurusan','id_jurusan','id');
}
And i think this enough your solutions for controller,
public function create()
{
$data= Jurusan::get()->pluck('name','id');
return view('Mahasiswa.mahasiswaInsert',compact('data'));
}
I got a problem with relationship one to one mechanism for edit & update condition, so I have model Siswa and Telepon, with Telepon belongs to Siswa... here is the code
Siswa.php (model)
class Siswa extends Model
{
protected $table = 'siswa';
protected $fillable = [
'nisn',
'nama_siswa',
'tgl_lahir',
'jns_klmin'
];
protected $dates = ['tgl_lahir'];
public function getNamaSiswaAttribute($nama_siswa){
return ucwords($nama_siswa);
}
public function setNamaSiswaAttribute($nama_siswa){
$this->attributes['nama_siswa']=ucwords($nama_siswa);
}
public function telepon(){
return $this->hasOne('App\Telepon', 'id_siswa');
}
}
Telepon.php (model)
class Telepon extends Model
{
protected $table = 'telepon';
protected $primKey = 'id_siswa';
protected $fillable = [
'id_siswa',
'no_telepon',
];
public function siswa(){
return $this->belongsTo('App\Siswa', 'id_siswa');
}
}
Edit and update function controller shown as follows :
public function edit($id){
$siswa = Siswa::findOrFail($id);
$siswa->no_telepon = $siswa->telepon->no_telepon;
return view('siswa.edit', compact('siswa'));
}
public function update(Request $request, $id){
$siswa = Siswa::findOrFail($id);
$input = $request->all();
$validator = Validator::make($input, [
'nisn'=>'required|string|size:4|unique:siswa,nisn,'.$request->input('id'),
'nama_siswa'=>'required|string|max:30',
'tgl_lahir'=>'required|date',
'jns_klmin'=>'required|in:L,P',
'no_telepon'=>'sometimes|numeric|digits_between:10,15|unique:telepon,no_telepon,'.$request->input('id').',id_siswa',
]);
if ($validator->fails()) {
return redirect('siswa/'.$id.'/edit')->withInput()->withErrors($validator);
}
$siswa->update($request->all());
$telepon = $siswa->telepon;
$telepon->no_telepon = $request->input('no_telepon');
$siswa->telepon()->save($telepon);
return redirect('siswa');
}
I got Trying to get property of non-object error in edit function, line "$siswa->no_telepon = $siswa->telepon->no_telepon;".
When we call edit view inside edit controller, it will give a form which inside of it has previous saved data. no_telepon itself is a column from Telepon table, not Siswa, so how to show telephone saved data for editing purposes is the problem. FYI, create works just fine, and no_telepon data saved in Telepon table. So, how about this one? Any help appreciated.
It's probably because you don't have any 'App\Telepon' in the database with 'id_siswa' pointing to the id of the record from table siswa.
You can ommit this error in this way:
public function edit($id){
$siswa = Siswa::findOrFail($id);
$siswa->no_telepon = $siswa->telepon ? $siswa->telepon->no_telepon : '';
return view('siswa.edit', compact('siswa'));
}