Laravel Select Query Accessing Returned Data - mysql

I've been working with Laravel for a short time, and am confused about accessing data retrieved from queries. I'm trying to save data to variable but am getting the following:
Trying to get property of non-object
Queries tried:
$data = DB::table('table_1')->select('user_id', 'email')->where('email', '=', Input::get('email_address'))->get();
// also tried
$data = DB::table('table_1')->where('email', '=', Input::get('email_address'))->pluck('user_id');
// accessing data
$userID = $data->user_id;
Both return the same.

To elaborate a little bit, ->get() will return a Collection. You might iterate over it as an array, but you can profit from methods that this class offers. pluck is one of these.
That's why $userID = $data->user_id; wouldn't work.

first() did the trick, rather than get().
$data = DB::table('table_1')->select('user_id', 'email')->where('email', '=', Input::get('email_address'))->first();

Related

Need suggestion in query builder of Laravel 9 application

How to avoid foreach-loop because Laravel query builder is used to get a single record detail.
When I tried without foreach-loop, the following error occurred "Property [name] does not exist on this collection instance".
Controller
public function show(Student $student)
{
$result = DB::table('students')
->select('*')
->where('id', "=" ,$student['id'])
->get();
foreach($result as $studentData){}
return view('student.show', ['studentData' => $studentData]);
}
Blade View
{{ $studentData->name}}
->get() always returns a Collection, even if there's only one match. If you only want the first record, you can use ->first() instead.
Do you really have to use raw queries? I assume Student is a Laravel Model, in that case, you should be able to use Student::find($id) to get the model directly.

when i use where on json column on laravel return empty collection

When I use this code in laravel for json where
$users = App\User::where('meta->Address->addressState','4');
Laravel returns an empty collection
Illuminate\Database\Eloquent\Collection {#3909
all: [],
}
How could i solve this issue?
Try using whereJsonContains like
$users = App\User::whereJsonContains('meta->Address->addressState', '4')->get();
or
You can use
$users = DB::table('users')
->where('meta->Address->addressState', '4')
->get();
I think where too work correctly may be you need to remove the quote
$users = App\User::where('meta->Address->addressState', 4)->get();
or
$users = App\User::where('meta->Address->addressState', "'4'")->get();
depend on how you have stored it in your DB.
$users = App\User::where('meta->Address->addressState','4');
$data=$users->first();
This will work if you want only one data from table that have addressState of 4 else if you want multiple data you can try this
$data=$users->get();

Laravel "->where(like) issue"

I have this code
$users = array();
$users_to_add = User::all()->where('unit', 'LIKE', '%'.$request->unit.'%');
But i get nothing on $users_to_add. The like doesn't work. What am i missing?
all() actually runs a query to get all users. Therefore, your where() call is on the Collection (PHP), not the Query Builder (SQL).
You'll want to use get() but make sure you declare your query conditions prior to calling it.
get() will execute the query, with your defined conditions, and return a collection of results.
User::where('unit', 'LIKE', '%'.$request->unit.'%')->get();

Laravel 5 Eloquent ORM select where - array as parameter

I'm getting grade_id from the database:
$grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->select('grade_id')->get();
and then I want to use that grade_id array in the where eloquent clause so I run
$home_feed = Home::join('home_grade', 'home_grade.home_id', '=', 'homes.id')
->whereIn('grade_id', $grade_id)
->get();
but when I run this I'm getting an error: Object of class stdClass could not be converted to string
What could be the problem? Thanks guys.
Depending on laravels version your $grade_id is either an array or a collection of objects. What you need is an array or a collection of values.
You can achieve that using the pluck() method insted of select() like IzzEps suggested.
But you can get the same result by passing a subquery to the whereIn() method:
$gradeSubquery = DB::table('grades')->where('teacher_id',$teacher_id)->select('grade_id');
$home_feed = Home::join('home_grade', 'home_grade.home_id', '=', 'homes.id')
->whereIn('grade_id', $gradeSubquery)
->get();
This way you will run only one query instead of two.
Update: Before version 5.2 you have to use lists() instead of pluck(). And the whereIn() method doesn't accept a Builder as second parameter. To get the same query you would need to use a closure:
$home_feed = Home::join('home_grade', 'home_grade.home_id', '=', 'homes.id')
->whereIn('grade_id', function($query) use($teacher_id) {
$query->from('grades')
->where('teacher_id', $teacher_id)
->select('grade_id');
})
->get();
your first query is returning a collection, not the grade_id.
Try this instead: $grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->pluck('grade_id');
Using lists worked.
$grade_id = Grade::where('teacher_id', $teacher_id)->lists('grade_id');
They return an array instead of a collection
You need to create the array correctly. To do this use two functions that Eloquent work with: pluck() and toArray(). Look at example below:
$grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->pluck('grade_id')->toArray();

What is wrong with this Code Igniter mySQL query?

I have two tables for storing information about a user. One is for authentication, the other is information the user will enter themselves. I am writing a model that will be used when the user interacts with this information. The following method is to return data for display and modification.
I need a query that will return 'email' and 'username' from $accounts_table and * from $profiles_table. I can't seem to get my head around the JOIN syntax though. I understand how joins work, but my queries throw sentax errors.
function get_userdata($id){
$data = array();
$this->db->get_where($this->profiles_table, array('user_id' => $id));
$this->db->join($this->accounts_table.'.email', $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
$data= $this->db->get();
return $data;
}
I see a couple of issues:
You should be using $this->db->where(), instead of $this->db->get_where(). get_where() executes the query immediately.
$this->db->get_where('user_id', $id);
Also the first argument of $this->db->join() should only be the table name, excluding the field.
$this->db->join($this->accounts_table, $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
And you're returning $data which is just an empty array(). You would need to pass the query results to $data like this:
$data = $record->result_array();
get_where executes the query. So, your join is its own query, which doesn't work.
You need to break get_where into where and from.
Also, in MySQL, you JOIN a table, not a field. If you want that field, add it to the SELECT.
$this->db->select($this->profiles_table.'.*');
$this->db->select($this->accounts_table.'.email,'.$this->accounts_table.'.username');
$this->db->from($this->profiles_table);
$this->db->where('user_id', $id);
$this->db->join($this->accounts_table, $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
$data = $this->db->get();
NOTE: $this->db->get() returns a query object, you need to use result or row to get the data.
I think you've a mistake:
$this->db->join($this->accounts_table.'.email', $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
First parameter should a table NOT a field: $this->accounts_table.'.email' is wrong IMHO. Or only a typo :)