jeudi 9 juin 2022

How to use laravel group concat

I have a pivot table for property, client and agent

...

public static function client_property_list($id)
{
    $client_property_list = DB::table('agent_client_property as acp')
        ->leftJoin('clients as c', 'acp.client_id', '=', 'c.id')
        ->leftJoin('properties as p', 'p.id', '=', 'acp.property_id')
        ->leftJoin('agents as a', 'a.id', '=', 'acp.agent_id')
        ->select(DB::raw('p.*'))
        ->where('c.id','=',$id)
        ->get();

    return $client_property_list;
} 

...

one property can have multiple agents, i want to group those agents to show only 1 on the list

tried this didnt worked

public static function client_property_list($id)
    {
        $client_property_list = DB::table('agent_client_property as acp')
            ->leftJoin('clients as c', 'acp.client_id', '=', 'c.id')
            ->leftJoin('properties as p', 'p.id', '=', 'acp.property_id')
            ->leftJoin('agents as a', 'a.id', '=', 'acp.agent_id')
            ->select(DB::raw('p.*,a.first_name as name'))('group_concat(name) as names')
            ->where('c.id','=',$id)
            ->whereNull('c.deleted_at')
            ->whereNull('p.deleted_at')
            ->whereNull('a.deleted_at')
            ->get();

        return $client_property_list;
    } 


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/FYEs74A
via IFTTT

mercredi 8 juin 2022

View [views.create] not found [closed]

I do not even know what should I do more?

this is my web.php

use App\Http\Controllers\HomeController;

 Route::get('/home', [HomeController::class,'index']); // have another version no worry

This is controller:

class HomeController extends Controller
{

    public function index()
{
    //
    return view('views.create');
}

this is view:

create.blade.php  // in views folder

sdhfbskdfnkds

What did I miss? Why does appear this error? I did lots of things:

composer update
php artisan config:cache
php artisan cache:clear
checked folders - views, sessions,cache in storage
re installed laravel 


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/5sFpD8R
via IFTTT

Unable to fetch results from hasManyJson Using staudenmeir / eloquent-json-relations

I have been working on two tables Category & Product. In Category Model I have a relationship like

class Category extends Model
{
  use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
  public function products(){

    return $this->hasManyJson(Product::class,'category_ids[]->id');
  }
}

In Products Model I have a relationship like

class Product extends Model
    {
      use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
      protected $casts = [
          'category_ids'=>'json', 
      ];
      public function products(){

        return $this->belongsToJson(Category::class,'category_ids[]->id');
      }
    }

Now in my controller when I'm doing trying to get count of each categories product, it is giving me Empty results, below is my controller code.

public function two_category()
{
    $list = Category::where('home_status', true)->get();
    foreach($list as $ls){
        echo $ls->name.'    '.count($ls->products).'<br>';
    }
    dd('ended');
   }

This is giving -

Category1 0

Category2 0

And finally this is how my column in product table looks like. Category id column in json



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/abfNz0M
via IFTTT

mardi 7 juin 2022

Can not set columns of db table in Laravel Php

I am new to laravel. I have three tables in my DB

  1. dsp_account with fields id,name_display,short_name.

enter image description here

  1. dsp with fields id,dsp_account_id.

enter image description here.

  1. rtbcfg_region_dsp with fields dsp_id,status.

enter image description here.

dsp table connected to dsp_account table via dsp_account_id field.

dsp table connected to rtbcfg_region_dsp via dsp_id.

I try to create this sql query:

select distinct dsp_account.id,dsp_account.name_display,dsp_account.short_name
from dsp_account
inner join dsp on dsp.dsp_account_id = dsp_account.id
AND dsp.id 
IN(SELECT dsp_id
FROM rtbcfg_region_dsp
group by dsp_id, status
having count(*)>0 and status = 1) order by dsp_account.name_display ASC;

this is my code:

$all_list6 = DspAccount::select('DspAccount.id', 'DspAccount.name_display','DspAccount.short_name')
            ->join('dsp', 'dsp.dsp_account_id', '=', 'dspAccount.id')
            ->whereIn('DSP.id',function($query)
            {
                $query->select('dsp_id')
                ->from('rtbcfg_region_dsp')
                ->groupBy('dsp_id','status')
                ->havingRaw('COUNT(*) > 0 AND status = 1');
        
            })
            ->get();

this is the error that I got: what I am missing:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DspAccount.id' in 'field list' (SQL: select `DspAccount`.`id`, `DspAccount`.`name_display`, `DspAccount`.`short_name` from `dsp_account` inner join `dsp` on `dsp`.`dsp_account_id` = `dspAccount`.`id` where `DSP`.`id` in (select `dsp_id` from `rtbcfg_region_dsp` group by `dsp_id`, `status` having COUNT(*) > 0 AND status = 1) and `dsp_account`.`deleted_at` is null) 

enter image description here



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/xqUYi1K
via IFTTT

How to write brackets in sql command with laravel

I am new to laravel and eloquent , and I want to make a query to the DB (MySQL). I have this code.

$all_list_Active = TableA::select("id", "name_display", "short_name")->orderBy('name_display', 'ASC')->get();

and the issue that I need to change the query for this in MySQL:

select distinct TableA.id,TableA.name_display,TableA.short_name
from TableA
inner join TableB on TableB.account_id = TableA.id
AND TableB.id 
IN(SELECT id
FROM TableC
group by id, status
having count(*)>0 and status = 1) order by TableA.name_display ASC;

I am not understand how to write fully mysql in the laravel as string? is their an option to run this query in laravel



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/9Y6L2V3
via IFTTT

Call to a member function comments() on null when use foreign key

This is error Call to a member function comments() on null, in model give a relation still show this error Article model

function user() {
return $this->belongsTo(User::class, 'id');
}
public function comments()
{
    return $this->hasMany(Comment::class, 'id');
}

Comment Model

 public function user()
{
    return $this->belongsTo(User::class);
}
public function article()
{
    return $this->belongsTo('App\Article');
}

This is contoller

  public function store(Request $request)
{
    //dd($request->all());
      Auth::user();   
    $comment = new Comment;
    $comment -> user_id=Auth::user()->id;
    $comment-> comment = $request->get('comment');
   $article = Article::find($request->article_id);

    $article->comments()->save($comment);
     return redirect()->back()->with('success', 'your comment,successfully save');   

}

This is blade file

<form method="post" action="">
                    @csrf
                    <div class="form-group">
                        <textarea class="form-control" name="comment"></textarea>
                        <input type="hidden" name="article_id"/>
                    </div>
                    <div class="form-group">
                        <input type="submit" class="btn btn-success" value="Add Comment" />
                    </div>
                </form>


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/czhIyN0
via IFTTT

Laravel Job Withcount Parameter not Accessible in job Laravel 5.6

Hii I created a job to send mail. I execute a query in the controller using withCount() and am able to get the count parameter inside the controller but while I am parsing the same data to the job and assign to a local variable in constructer then I use a local variable inside the handle method count parameter was missing in my local.

I am using SYNC as QUEUE_DRIVER and code files below mention

Controller

 $confernceIterationData = ConferenceIteration::with('AbstractNews')->withCount('AbstractNews')->where('id', $unserializeData['confid'])->first();
              
 $this->dispatch(new SendtronAutomatedEmailJob($confernceIterationData, $unserializeData, $attachments));

Controller Output of dd($confereceIterationData)

array:42 [
    "id" => 9085
    "conference_iteration_id" => "e3f65fda-7776-4e64-82d8-b5f1289141e2"
    "conference_id" => 259
    "name" => "American Association of Cancer Research Annual Meeting 2022"
    "acronym" => "AACR 2022"
    "abstract_news_count" => 8339
  ]

Job

<?php

namespace App\Jobs;

use App\Mail\SendTronAutomatedMail;
use App\TeamConferences;
use App\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;

class SendtronAutomatedEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * conference Iteration data
     *
     * @var object
     */
    protected $confrenceData;

    /**
     * Planner Form Data
     *
     * @var array
     */
    private $formData;

    /**
     * attchement files
     *
     * @var array
     */
    private $files;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($confrenceData, $formData, $files)
    {
        $this->confrenceData = $confrenceData;
        $this->confrenceData->abcount = $confrenceData->abstract_news_count;
        $this->formData = $formData;
        $this->files = $files;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        dd($this->confrenceData);

        $request = $this->formData;
        $i = 0;
        $users = User::select('email')->distinct()->wherehas('teams', function ($q) use ($request) {
            $q->whereIn('id', $request['teams']);
        })->pluck('email')->toarray();

        $subject = $this->confrenceData->acronym . " Conference Planner (" . $this->confrenceData->data_status . ")";

        

        $this->extractConfenceData($this->confrenceData);

      
 
        // dd($this->confrenceData->AbstractNews_count);

        $count = count($users);
        foreach ($users as $user) {
            Log::channel('sendtron_email')->info('Authomated Mail Send To .', ['id' => $user]);

            Mail::to($user)->send(new SendTronAutomatedMail($request['content'], $subject, $this->files));
            if (++$i === $count) {
                unset($users);
                TeamConferences::where('conference_iteration_id', $request['confid'])->whereIn('team_id', $request['teams'])->update(['last_mail_sent' => Carbon::now()->toDateTimeString()]);
                Storage::disk('s3SendTron')->delete(array_column($this->files, 's3path'));
            }
        }

    }
}

If we dd($confrenceData) in constructor we get the attribute abstract_news_count as mention in controller output.

but at the same time if we dd($this->confrenceData) we unable to get abstract_news_count



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/qyVEXRz
via IFTTT