The database table is : prefix_adminUsers and I have a model in UserAdmin.php as below:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class UserAdmin extends Model
{
protected $table = 'adminUsers';
public $timestamps = false;
}
When I tried to access the same by controller it show me the error:
Illuminate\Database\Eloquent\Builder Object ( [query:protected] =>
Illuminate\Database\Query\Builder Object ( [connection] =>
Illuminate\Database\MySqlConnection Object ( [pdo:protected] => PDO
Object
my controller is as follows:
use App\Model\UserAdmin;
$userRecord = UserAdmin::where('id', '=', Auth::user()->id);
print_r($userRecord);
It's not an error, you are printing the eloquent class Builder, used to build the queries to retrieve the model data, but it doesn't give you the results yet because you are missing ->get() at the end.
Your code should look like:
use App\Model\UserAdmin;
$userRecord = UserAdmin::where('id', '=', Auth::user()->id)->get();
print_r($userRecord);
You can read more how to retrieve Models on the Laravel 5.5 docs.
Since each Eloquent model serves as a query builder, you may
also add constraints to queries, and then use the get method to
retrieve the results.
Related
I have an eloquent query builder in Laravel
$builder = Model::orderBy('created_at', 'DESC');
I want to specify a connection to use, because I have a separate db for some data - let's call it 'archive' for example.
Now, I know I can do it statically, like $builder = Model::connection('archive'); [edit] ::on('archive'); apparently
But if I already have the $builder object, and can't use the static function, how can I do it on the builder object?
I've tried
$builder->connection('archive');
$builder->onConnection('archive');
$builder->setConnection('archive');
And these are all failing for me.
There are two solutions.
Define in class
class MyModel extends Model {
protected $connection= 'archive';
protected $table = 'table_name';
}
In a query
(new MyModel())->on('archive')->get();
This sounds stupid but I can't find a way to retrieve a record from eloquent model Gallery.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Gallery extends Model
{
use HasFactory;
protected $fillable = ['text'];
}
Calling from a Route in web.php:
$data = Gallery::with('id', '=', 1)->get();
throws this error in laravel.log:
local.ERROR: Call to undefined relationship [id] on model [App\Models\Gallery]. {"exception":"[object] (Illuminate\\Database\\Eloquent\\RelationNotFoundException(code: 0): Call to undefined relationship [id] on model [App\\Models\\Gallery]. at /Users/artur/PhpstormProjects/strong-moebel.art/vendor/laravel/framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php:35)
The only thing that works is:
$data = Gallery::all();
The with() method at:
Gallery::with
Is used to refer to relationship with another model.
If you are looking to get data from just the Gallery model change with() to where() as below:
$data = Gallery::where('id', '=', 1)->get();
This will return a collection (think of it as an object containing an array of the results).
If you're trying to access additional data from a related model you will need to setup the relationships as per the Laravel documentation.
If you want just the instance of Gallery related to that id, you can get it with
$gallery = Gallery::where('id', '=', 1)->first();
// or
$gallery = Gallery::find(1);
I would like to know if is it possible to work in a model using query builder to make a join between two tables, I don't want to use eloquent
this is my model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
class Tbl_Perimetro extends Model
{
puplic function perimetros(){
$carteras = DB::table('tbl_perimetros')
->join('tbl_equipo_postventaatcs', 'tbl_equipo_postventaatcs.id', '=', 'tbl_perimetros.postventaatc_id')
->join('tbl_lista_carteras', 'tbl_equipo_postventaatcs.carteras_id', '=', 'tbl_lista_carteras.id')
->get();
return $carteras;
}
}
If you just want to use query builder you could to do so. But I realy recomend to use eloquent methods.
Also, is important to look about mutators, appends and scopes options to models that could help you more than query builder alone.
Other appointments:
DB::table('tbl_perimetros') could be replaced by $this in this model
Do not forget return for the method.
Try protect $appends = ['perimetros'] and rename method to getPermietrosAttribute to return perimetros as an item in model object.
how to convert sql to eoquent
$getAllRequirementRecord = Requirement::select()
->join('requirement_locations','requirements.id','=','requirement_locations.requirement_id')
->join('locations','requirement_locations.location_id','=','locations.id')
->where('isdelete',2)
->where('requirements.id',$id)->get();
help to solve this problems
Let's say we've an eloquent Model named Requirement and we've a database table named requirements associated with that model and we have another model named requirementLocation associated with a database table named requirement_locations.
and we have a database relation ship between both tables "requirements" & "requirement_locations" based on the requirement_id
Requirement Model:
class Requirement extends Model{
protected $table = 'requirements';
}
RequirementLocation Model:
class RequirementLocation extends Model{
protected $table = 'requirement_locations';
}
and Now we need to setup a relationship between these two models like the database tables .. so inside the Requirement we're gonna use has many relationship
use App\RequirementLocation;
class Requirement extends Model{
protected $table = 'requirements';
public function locations(){
return $this->hasMany(RequirementLocation::class);
}
}
and simply to fetch your data use
$id = 1;
$getAllRequirementRecord = Requirement->whereHas('locations' , function($query){
$query->where('isdelete',2);
})->where('id',$id)->with('locations')->get();
You can use eloquent with SQL raw query likewise:
$activeusers = User::selectRaw('users.name, count(*) submitted_games')
->join('games', 'games.user_id', '=', 'users.id')
->groupBy('users.name')
->orderBy('submitted_games', 'DESC')
->get();
To output to the screen the last queries ran you can use this :
dd(DB::getQueryLog());
I hope you have created Requirement, Location and RequirementLocation models
So first add this code in Requirement.php model file:
public function locations()
{
return $this->hasMany('RequirementLocation', 'requirement_locations');
}
Then access the requirements like this :
$requirement = Requirement::where('id', $requirement_id)->first();
Now you can access locations like this :
$requirement->locations;
Using Laravel 5. I have a model Property, that has many Weeks available to it. This is the Property Model.
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* #property mixed week
*/
class Property extends Model
{
public function week()
{
return $this->hasMany('App\Models\Week', 'PropertyID', 'PropertyID');
}
}
I am trying to use WhereBetween on a hasMany relation of an object using the WhereBetween method. This is the error I get though.
Method 'WhereBetween' not found in class 'illumainate\Database\Eloquent\Relations\HasMany.
list($from, $to) = $params->get('range');
$week = $property->week()
->whereBetween('WeekDate', array($from, $to))
->get();
The same error occurs if I try WhereRaw instead of WhereBetween. hasMany class doesn't have WhereRaw or WhereBetween. Is there a way around this?
I had a similar issue in Laravel 5.2 with the following code:
$events = Events::whereBetween('start', array($start, $end))
->whereBetween('end', array($start, $end))
->get();
This throws an error like:
Call to undefined method App\Events::whereBetween()
Somehow in Laravel 5.0+ I could not use whereBeetween directly on the model. But in the API documentation whereBetween is still listed as function of the DB class (see here). By replacing the model with the DB class we can fix it:
use DB;
...
$events = DB::table('events')
->whereBetween('start', array($start, $end))
->whereBetween('end', array($start, $end))
->get();