Laravel Get Join Data And Non Join Data - php

I am trying to get two tables data using join
// $participantstation = DB::table('participant_station')
// ->join('reviews', 'participant_station.id', '=', 'reviews.participant_station_id')
// ->select('reviews.*', 'participant_station.*')
// ->where('station_id',$stn->id)
// ->where('status', '100')
// ;
Still some partipant are not linked , that means - participant_station.id', '=', 'reviews.participant_station_id
if I try this code it only shows the linked participants
is there any way to show both linked and non-linked participants using such query?
Thank you

Assuming you're trying to fetch all the details using Eloquent ORM here's what you can do:
$participantstation= ParticipantStation::join('participant_station', 'participant_station.id', '=', 'reviews.participant_station')->get(['reviews.*', 'participant_station.*']);
This will join the tables

try this,
Model -> ParticipantStation.php:-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ParticipantStation extends Model
{
public function reviews()
{
return $this->hasMany('App\Models\Review');
}
}
Model -> Review.php:-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
public function participant_station()
{
return $this->BelongsTo('App\Models\ParticipantStation','participant_station_id');
}
}
In controller.php
use App\Models\ParticipantStation;
$participantstation = ParticipantStation::with('reviews')->where('station_id',$stn->id)->where('status', '100')->get();
//dd($participantstation);

Related

withCount() for multiple levels?

Like you can do Project::with('events.contacts') can you also do Project::withCount('events.contacts') ? It doesn't seem to work. Is there another way to find the total count of contacts for all events for a certain project where project.id = event.project_id and event.id = contact.event_id
You can use relationship Has Many Through
Docs: https://laravel.com/docs/8.x/eloquent-relationships#has-many-through
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
public function contacts()
{
return $this->hasManyThrough(Contact::class, Event::class);
}
}
Then you can do
Project::withCount('contacts')->get();

problem in display two fields of join with same name in Laravel blade

I have two tables in MySql datebase:
person('id', 'name', 'address')
car('id', 'person_id', 'name')
I join them with this query:
$data = DB::table('person')->join('car', 'person.id', '=', 'car.person_id')->get(); return view('view1',['data'=>$data]);
in view1 we have:
person name: {{$data->name}}
car name: {{$data->name}}
the question is how can I show these two fields in view1?
notice that I can't change the fields name to two different names in my case.
I suggest you to create models for each table using php artisan commands
php artisan make:model Person
Person model look like below
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
use HasFactory;
protected $table = "person";
public function cars()
{
return $this->hasMany(Car::class);
}
}
Similarly create car model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Car extends Model
{
use HasFactory;
protected $table="car";
}
To Fetch you can do like this
$person=Person::with('cars')->get();
So you don't need alias
$data = DB::table('person')->select('car.name as carName','person.name as personName') ->join('car', 'person.id', '=', 'car.person_id')->get();

Specify DB table in laravel query

Im new to laravel, i am trying to query a specific table in my DB. I only have 1 data table and the standard user auth tables. I am getting a error: BadMethodCallException
Call to undefined method App\Figures::table().
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Figures extends Model
{
}
controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Figures;
class figuresController extends Controller
public function figurespag2() {
$dummyDetails = Figures::table('figures')->where('name', 'batman');
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
route
Route::get ( '/pagination2', 'figuresController#figurespag2' );
I know it's going to be something obvious, but I am new to this.
this is wrong
$dummyDetails = Figures::table('figures')->where('name', 'batman');
Method 1---------- laravel eloquent
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Figures extends Model
{
protected $table = 'figures';
}
Controller
$dummyDetails = Figures::where('name', 'batman')->get();
and
Method 2 ---------- laravel Query Builder
$dummyDetails = \DB::table('figures')->where('name', 'batman')->get();
Use this you not need to define table name
public function figurespag2() {
$dummyDetails = Figures::where('name', 'batman')->get();
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
First you may need to know laravel model rules.
If you create a table name like "figures" (plural) you need to create its model by Figure (singular).
if you create a table other then this rule then you have to mentioned table name in model like this.
protected $table = "table_name";
you can access table with where condition in controller like this.
public function figurespag2() {
$dummyDetails = Figure::where('name', 'batman')->get();
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
Hope this may help you.

Alternatives to writing joins with foreign keys in eloquent?

I'm using laravel-eloquent and want to return a collection that joins several tables. For now, I do this using the query builder join method, but I would like to stay within eloquent. I mean, I already defined all my relationships, why should I write joins with foreign keys all the time?
For example, if I have defined my models like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function comments()
{
return $this->hasMany('App\Comments');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
and want to return all the comments with the user names, for now I would write this:
DB::table('users')->select('users.name', 'comments.body')
->join('comments', 'users.id', '=', 'user_id')
->get();
I have tried writing
$users = new 'App\User';
$users->with('comments')
->select('name', 'comments.body');
but it didn't work. Do I need to define a new collection? I will end up with many, many collections if I do that...
Try:
$result = null;
$users = new 'App\User';
$records = $users->with('comments')->get();
if ($records->isNotEmpty()){
$result = $records->map(function($val,$key){
return ["name"=>$val->name, "comments" => $val->comments()->get(['body']);
})->values()->all();
}
dd($result);
I have not tested the codes yet. Please check and let me know if it works for you?

Laravel: How do I create the relationship and which one do I use?

I have the database structure that have a classes table, a users table and users_classes table that matches the other two, because a user can belong to multiple classes. I have a problem now. I have code like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Classes extends Model
{
protected $table = 'classes';
public function students()
{
}
}
And I want to be able to access the students of the class by typing $class = Classes::find(1) and then $class->students to access the students. How do I define the relationship without using the query builder? I want to use eloquent. Im a noobie in Laravel pls dont downvote.
You use a belongsToMany relation.
If your users_classes table has the fields user_id and class_id you can do the following:
public function students()
{
return $this->belongsToMany(Student::class, 'users_classes', 'class_id', 'user_id');
}

Categories