Laravel : Call to undefined method Illuminate\\Database\\Query\\Builder - laravel

On query get the error with undefine method. Simply i want to get data from two tables query look linke
public static function userDetail($id){
$result = User::whereHas('user_details', function ($query) {
$query->where('user_details.user_id',$id);
})->first();
return $result ;
}
Relationship
On Model User define relationship
public function userDetails()
{
return $this->hasOne(UserDetails::class);
}
and in userDetails model
public function user()
{
return $this->belongsTo(User::class);
}

just change
public static function userDetail($id){
$result = User::whereHas('user_details', function ($query) use($id) {
$query->where('user_id',$id);
})->first();
return $result ;
}

If you dont need to use static, you can use this $result value on userDetails
public function userDetail($id){
$result = userDetails::where('user_id', '=', $id)->first();
return $result ;
}
Then the result can get access to the user from the vale by using $value->user->{user property}

Related

Laravel excel export how to export conditional data?

I am trying to export partial data from my table not all data, I am using Maatwebsite plugin
I have tried below code in controller
public function report(Request $request)
{
$sdate = $request->query('sdate');
$edate = $request->query('edate');
$report = Report::whereDate('created_at', '>=', $sdate)
->whereDate('created_at', '<=', $edate)
->get();
return Excel::download($report, 'report.xlsx');
}
In this time I am getting empty excel file.
I am able to get all data after creating app/export
like below
app/export/ReportExport.php
public function collection()
{
return Report::all();
}
but how I can I do it in controller ? or how can I sent $report data controller to collection ?
You can use the FromQuery concern provided by the package.
namespace App\Exports;
use App\Report;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class ReportExport implements FromQuery
{
use Exportable;
public function __construct($start, $end)
{
$this->start = $start;
$this->end = $end;
}
public function query()
{
return Report::query()->whereDate('created_at', '>=', $this->start)
->whereDate('created_at', '<=', $this->end);
}
}
Then from you controller, you can call it
return (new ReportExport($sdate, $edate))->download('report.xlsx');
I have not tested the code. So apologies if I made a mistake. You can refer to official documentation.
There are also other methods of exporting which you can find from the documentation.
you can do it by doing this way.
Step 1: in your controller
bind $request in Report-Export Class. add code in method where you are calling export excel.
return Excel::download(new ReportExport($request), 'report.xlsx');
step 2:
protected $request;
public function __construct($request)
{
$this->request = $request;
}
public function collection()
{
$request = $this->request ;
return User::where('customer_id',$request->cId)->get();
}

Sort eloquent model with relationships Laravel

I have a model that I want to sort based on a relationship property.
First model "DeviceType":
public function make()
{
return $this->hasMany(DeviceMake::class);
}
Second model: "DeviceMake":
public function type()
{
return $this->hasOne(DeviceType::class, 'id', 'device_type_id');
}
public function model()
{
return $this->hasMany(DeviceModel::class);
}
Controller:
$type = DeviceType::with(['make'])->where('id', '=', $device_type_id)->first();
Table name is device_makes and I want to sort it by name. How can I do this?
And what about?
$type = DeviceType::with(['make' => function ($query) {
$query->orderBy('name', 'asc');
}])->where('id', '=', $device_type_id)->first();
Please note that first() will only return the first model that matches your query whereas get() will return all of them.
Maybe you could try this instead in your Model:
public function make() {
return $this->hasMany(DeviceMake::class)->orderBy('name', 'asc');
}

simple One to many relationship laravel

i have a user(ADMIN) and a student i want them to be in one table that is on the dashboard controller i cant find and exact answer in the searching please help me
Student model
public function user()
{
return $this->belongsTo("App\User", 'User_id', 'id');
}
User model
public function student()
{
return $this->hasMany("App\Student");
}
DashboardController
$users = User::with('user_id');
return view('superadminpage.admin_table')->with('users', $users);
Edit Student model code like this:
public function user(){
return $this->belongsTo("App\User");
}
Also edit User model:
public function students(){
return $this->hasMany("App\Student");
}
then use in DashboardController
$users = User::query()->with("students")->all();
return view('superadminpage.admin_table')->with('users', $users );
then you can do this:
foreach($users as $user){
$students = $user->students;
// use
}
first change the User_id to user_id at student model
public function user()
{
return $this->belongsTo("App\User", 'user_id');
}
second change at DashboardController
$users = User::with('user_id'); to $users = User::with('student')->all();
first change the User_id to user_id at student model
public function user()
{
return $this->belongsTo("App\User", 'user_id' , 'id');
}
second change at DashboardController
$users = User::with('user_id'); to $users = User::with('student')->all();
it's also good to change at user model public function student() to public function students() and call at DashboardController $users = User::with('students')->all();
public function users()
{
return $this->hasMany('App\User');
}
public function role()
{
return $this->belongsTo('App\Role');
}

Use the attribute of a model in a scope

I have defined an attribute in my model and want to use it in a scope.
protected $appends = ['lowest_price'];
public function getLowestPriceAttribute()
{
$lowest_price = NULL;
foreach($this->shops as $shop) {
if(is_null($lowest_price)) {
$lowest_price = (double)$shop->pivot->price;
}
if($lowest_price > (double)$shop->pivot->price) {
$lowest_price = (double)$shop->pivot->price;
}
}
return $lowest_price;
}
The lowest_price attribute I would like to use in a scope but it's always 'null':
public function scopeMaxPrice(Builder $query, $max_price): Builder {
return $query->where('max_price', '>=', $this->lowest_price);
}
If I dd(); the getLowestPriceAttribute function in the scope it also returns 'null'. In my view I have access to the 'lowest_price' attribute.
Can the attribute be part of the query? If not, is there an elegant solution?
EDIT: The scope gets called in the Product controller:
public function index()
{
$products = QueryBuilder::for(Product::class)
->allowedFilters(
AllowedFilter::exact('type_id')->default('1'),
AllowedFilter::scope('min_speed')->default(0),
AllowedFilter::scope('min_range')-> default(0)
# AllowedFilter::scope('max_price')-> default(999)
)
->get();
$types = DB::table('types')->pluck('name', 'id');
return view('pages.home', compact('products', 'types'));
}
Note: I am using the Spatie querybuilder

DB query to relationship

My query:
$data = DB::table('countries')
->leftJoin('matches', function ($join) {
$join->on('matches.hometeam', '=', 'countries.id')
->orOn('matches.awayteam', '=' ,'countries.id');
})
->where('countries.id', $id)->get();
return dd($data);
How can I get same results using relationship?
In your Model (You need a matches model aswell):
<?php
class Country extends Model {
public function matches() {
return $this->hasMany(App\Matches::class, ['hometeam', 'awayteam'], 'id');
}
}
In your controller:
$countrys = Countrys::where('id', $id)->get()
foreach($countrys as $country) {
var_dump($country->matches);
}
die();

Resources