I have two Models (Color and ColorFamily Table).I would like to join two table.When I join Second Table (ColorFamily),its shows an error message "Invalid Object Name ColorFamily".If I use to Join First Table then its shows an error message "Invalid Object Name Color".
Note:-
If I use Individual Query (Select Query) without join ,then the records is showing,that means both the tables are available...
I have check with google but can not find the solution.....
Color Models:-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Color extends Model
{
protected $primaryKey = 'ColorId';
protected $table = 'Common_Ref_Color';
public $timestamps = false;
}
ColorFamily Models:-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ColorFamily extends Model
{
protected $primaryKey = 'ColorFamilyId';
protected $table = 'Common_Ref_ColorFamily';
public $timestamps = false;
}
Controller:-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Models\ColorFamily;
use App\Models\Color;
class ColorController extends Controller
{
public function Index()
{
$Data = Color::select ('ColorId','ColorName','ColorCode','ColorShortCode')
->join('ColorFamily','ColorFamilyId','=','Color.ColorFamilyId')
->get();
return $Data;
}
}
Can you try the following code:
class ColorController extends Controller {
public function Index()
{
$Data = DB::table('Color')
->select ('ColorId','ColorName','ColorCode','ColorShortCode')
->join('ColorFamily','ColorFamily.ColorFamilyId','=','Color.ColorFamilyId')
->get();
return $Data;
}
}
I think that you need to add joined table name inside join
('ColorFamily.ColorFamilyId','=','Color.ColorFamilyId')
Related
How to get "category name" show in the product table not as "category_id"?
I already try to combine any solutions for this. But still can't solve this prob.
I'd love to hear other suggestions from the masters here.
Category Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Kategori extends Model
{
use HasFactory;
protected $casts = [
'updated_at' => 'datetime:d/m/Y, H:i:s'
];
public function Kategori()
{
return $this->hasMany('App\Models\Produk');
}
}
Product Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Produk extends Model
{
use HasFactory;
protected $casts = [
'updated_at' => 'datetime:d/m/Y, H:i:s'
];
public function Produk()
{
return $this->belongsTo('App\Models\Kategori', 'kategori_id');
}
}
Category Table
Product Table
Product Controller >>>> in my opinion may be my prob at here, but not so sure.
<?php
namespace App\Http\Controllers;
use App\Models\Produk;
use RealRashid\SweetAlert\Facades\Alert;
use Yajra\Datatables\DataTables;
use Illuminate\Http\Request;
class ProdukController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function json(){
return Datatables::of(Produk::all())->make(true);
}
public function index(){
return view('back.produk.show');
}
}
Pass the other model using your relationship
public function json(){
return Datatables::of(Produk::with('produk')->get())->make(true);
}
to which say that is named $produks, you can access it as
$produk->produk->nama;
As a side note, Do name your relationships the names of the other model.
for example, in the Kategori class, the relationship to Produk should be named produks (it is a hasMany relationship) as opposed to Kategori. Similarly, in the Produk class, the relationship to Kategori being named kategori() to which from the above answer you access it like
$produk->kategori->nama;
You should use:
public function json(){
return Datatables::of(Produk::with('Kategori')->all())->make(true);
}
I recommend to use camelCase for method names. I'm not sure how Datatables will handle this case.
In your ProdukController you need pass below code.
$result = Produk::with('Produk')->get();
When you dd($result);, you should see the related models in the relations array attribute.
To access the relations' properties from there, it is simply
$result->Produk->catagory_name
My code is:
App\Models\Client.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
protected $guarded = ['id'];
public function transactions()
{
return $this->hasMany('App\Models\Transaction','clients_id');
}
}
App\Models\Transaction.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
protected $guarded = ['id'];
public function clients()
{
return $this->belongsTo('App\Models\Client','clients_id');
}
}
In controller i tried
use App\Models\Client;
use App\Models\Transaction;
Client::with('transactions')->orderBy('id', 'desc')->get();
but it's not working as expected
Thanks in Advance
To get all clients in desc order with your transactions using
$clients = Client::with('transactions')->orderBy('id', 'desc')->get();
returns all clients which has 1 or more transactions. By your mention in above comments "It returns all of the clients despite having zero transaction records" below query will not do that
$clients = Client::has('transactions')->orderBy('id', 'desc')->get();
or
$clients = Client::has('transactions', '>=', 0)->orderBy('id', 'desc')->get(); // you can add any number in place of zero
It is all given in the docs
I want to send two collection model to view and make it as one paginate......but im still confused when i want to paginate as the two collection models
Here's my model RefHoliday.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RefHoliday extends Model
{
//
public $timestamps = false;
public $incrementing = false;
protected $table = 'refHoliday';
}
second model -- HDHolidayCalendar.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class HDHolidayCalendar extends Model
{
//
public $timestamps = false;
public $incrementing = false;
protected $table = 'Holiday_Calendar';
}
holiday controller
public function index()
{
$refHol = RefHoliday::all();
$HolCal = HDHolidayCalendar::all();
// $paginationRecord = ??
return view('administration.holiday.HolidayMasterList', compact('??'));
}
I try to explore to solve this things but seems not clearly explain...
To merge two collection.
$refHol = RefHoliday::all();
$holCal = HDHolidayCalendar::all();
$data = $refHol->merge($holCal);
I want to to retrieve all buyers for a specific saller.When I remove pluck and others methods chaining after get method it's working. But is not exact thing that I want. How Can I solve this provlem?
(source: licdn.com)
<?php
namespace App\Http\Controllers\Seller;
use App\Http\Controllers\ApiController;
use App\Seller;
use Illuminate\Http\Request;
class SellerBuyerController extends ApiController
{
public function index(Seller $seller)
{
$buyers = $seller->products()
->whereHas('transactions')
->with('transactions.buyer')
->get()->pluck('transactions')
->collapse()->pluck('buyer')
->unique('id')
->values();
return $this->showAll($buyers);
}
protected function showAll(Collection $collection, $code = 200)
{
return $this->successResponse($collection, $code);
}
protected function successResponse($data, $code)
{
return response()->json($data, $code);
}
}
Seller model hasMany relation to products
<?php
namespace App;
use App\Scopes\SellerScope;
class Seller extends User
{
public function products()
{
return $this->hasMany(Product::class);
}
}
Product Model hasMany relation to transactions
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
use SoftDeletes;
protected $fillable = [
'name', 'description', 'quantity', 'status', 'image', 'seller_id',
];
public function transactions()
{
return $this->hasMany(Transaction::class);
}
}
Transaction Model and relation to buyer
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Transaction extends Model
{
use SoftDeletes;
protected $fillable = [
'quantity', 'buyer_id', 'product_id'
];
public function buyer()
{
return $this->belongsTo(Buyer::class);
}
}
You are missing an import at the top:
use Illuminate\Support\Collection;
otherwise it assumes Illuminate\Database\Eloquent\Collection is to be used.
And values() obviously returns the support collection, not an eloquent one.
If you have a relationship from Buyer to Transaction you can go at this from the other direction to get buyers. You also need to make sure there is a relationship from Product to Seller (make sure you have the inverse of every relationship setup)
Buyer::whereHas('transactions.products.seller', function ($query) use ($seller) {
$query->where('id', $seller->id); // might have to be a more specific key name
})->get();
You would end up with a Eloquent Collection of Buyers who have transactions including products from a particular seller.
you should put use Illuminate\Support\Collection; in folder traits over apiResponser.php
After creating several Apps with Laravel and using softDelete properties I realized that methods like destroy(), restore() and kill() are exactly the same among several controllers. Therefore I am trying to put themn in a trait and use it from diferent Controllers.
My code is as follows:
ProfilesController.php
<?php
namespace App\Http\Controllers;
use App\Profile;
class ProfilesController extends Controller
{
public function destroy(Profile $profile)
{
Profile::del($profile, 'profiles');
return redirect()->route('profiles.index');
}
public function trashed()
{
Profile::trash('Profile');
}
}
Profile.php (model)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Profile extends Model
{
protected $fillable = ['user_id', 'role_id', 'title', 'subtitle', 'slug', 'birthday', 'about'];
use SoftDeletes, Helpers, commonMethods;
public function getRouteKeyName()
{
return 'slug';
}
// ... more code here
}
trait file: commonMethods.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use App\Profile;
use Session;
trait commonMethods
{
public static function del($element, $page_name)
{
$element->delete();
Session::flash('success', $element . ' successfully deleted!');
}
public static function trash($model)
{
$total = $model::onlyTrashed()->get();
$total_tr = count($total);
$all_tr = $model::all();
return view('partials.templates.trashed', compact('total', 'total_tr', 'all_tr'));
}
// ...more code here
}
The problem:
I try to visit the view "Trashed" that will list all elements "softdeleted" but not "killed", the method.
I pass the $model variable with the method trash($model)
I get the following error:
Class App/Profile does not found. Try to call App/Profile
I have debugged and the $model variable contains exactly what I need, the string 'Profile' which is what I need to build the Query:
$total = Profile::onlyTrashed()->get();
This query works while in the ProfilesController, but does not work while in a trait, since the model class is not found.
Any idea how could I make it work?
I am using Laravel 6.
If you need to use a class as a string you will want to use its full name. 'App\Profile' instead of 'Profile'.
$model = 'Profile';
new $model; // will use `\Profile`
$model = 'App\Profile';
new $model; // will use '\App\Profile';
In your controller( ProfilesController ) write :
use App\Profile;
In your model write :
use App\commonMethods;