logo
down
shadow

Laravel Eloquent get all in single key value pair


Laravel Eloquent get all in single key value pair

By : danyo
Date : November 21 2020, 07:01 PM
it fixes the issue You can't change how Eloquent returns relationship collections, nor should you want to, but you can certainly manipulate the collection in your response:
code :
$model->post_metas->pluck('value', 'key');


Share : facebook icon twitter icon
How to prepend Laravel Eloquent Collection array with a key-value pair?

How to prepend Laravel Eloquent Collection array with a key-value pair?


By : Mohammad Ali Minhas
Date : March 29 2020, 07:55 AM
This might help you I'm fetching something from database:
code :
$foo = collect(['' => 'Choose something…'] + $foo->all());
$foo = $this->fooRepository->all()
            ->lists('name', 'id')
            ->prepend('Choose something…', '');
$foo = $this->fooRepository->all()
            ->pluck('name', 'id')
            ->prepend('Choose something…', '');
How can I paginate two Eloquent collections on a single page with Laravel?

How can I paginate two Eloquent collections on a single page with Laravel?


By : Fabio Guerra
Date : March 29 2020, 07:55 AM
it should still fix some issue You can change the param of either pagination by
Paginator::setPageName('someparam');
code :
Paginator::setPageName('yourFirstParam');
$firstCollection = FirstModel::paginate(10);

Paginator::setPageName('yourSecondParam');
$secondCollection = SecondModel::paginate(10);
Paginator::setPageName('yourFirstParam');
$firstCollection->links();
Paginator::setPageName('yourSecondParam');
$secondCollection->links();
Returning a single Eloquent object in Laravel

Returning a single Eloquent object in Laravel


By : alexvavel
Date : March 29 2020, 07:55 AM
this one helps. Eloquent queries always return a collection, so you can easily use the filter function for this. After that call the first function to get the first element. If there are no elements in the collection, null will be returned.
For example:
code :
return $saved_items->filter(function ($item) use ($found_item) {
    return strpos($found_item, $item->x) !== false &&
           strpos($found_item, $item->y) !== false &&
           strpos($found_item, $item->z) !== false;
})->first();
Laravel 5.4 Eloquent filter from a single input

Laravel 5.4 Eloquent filter from a single input


By : Aress Test
Date : March 29 2020, 07:55 AM
this one helps. This is how you would do it with relationship methods.
You indeed need to use ->whereHas(), but you need to wrap both ->whereHas('addresses', ...) and ->orWhereHas('digits', ...) in a ->where(function ($q) ...) method.
code :
public static function search($request)
{
    $filters = $request->all();
    $sortby = isset($filters['sortby']) ? $filters['sortby'] : 'created_at';
    $orderby = isset($filters['orderby']) ? $filters['orderby'] : 'desc';

    return self::where(function ($q) use ($filters) {
            $q->whereHas('addresses', function ($q) use ($filters) {
                $q->where('address_line_1', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('address_line_2', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('city', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('county', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('post_code', "LIKE", '%'.$filters['name'].'%');
            })
            ->orWhereHas('digits', function ($q) use ($filters) {
                $q->where('number', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('extension', "LIKE", '%'.$filters['name'].'%');
            });
        })
        ->with(['addresses', 'digits'])
        ->orderBy('companies.'.$sortby, $orderby)
        ->select(['id', 'registered_name', 'trading_name', 'created_at', 'type'])
        ->paginate(20);
}
// Use it like: \App\Company::search($request->all())->paginate(20);
public function scopeSearch($q, $filters)
{
    $sortBy = isset($filters['sortby']) ? $filters['sortby'] : 'created_at';
    $orderBy = isset($filters['orderby']) ? $filters['orderby'] : 'desc';

    return $q->where(function ($q) use ($filters) {
            $q->whereHas('addresses', function ($q) use ($filters) {
                $q->where('address_line_1', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('address_line_2', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('city', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('county', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('post_code', "LIKE", '%'.$filters['name'].'%');
            })
            ->orWhereHas('digits', function ($q) use ($filters) {
                $q->where('number', "LIKE", '%'.$filters['name'].'%')
                    ->orWhere('extension', "LIKE", '%'.$filters['name'].'%');
            });
        })
        ->with(['addresses', 'digits'])
        ->orderBy('companies.'.$sortby, $orderby)
        ->select(['id', 'registered_name', 'trading_name', 'created_at', 'type']);
}
laravel - get a single value from eloquent model to be imported with data as single filed

laravel - get a single value from eloquent model to be imported with data as single filed


By : vladimir douritsky
Date : March 29 2020, 07:55 AM
should help you out It is mostly better to keep your methods defining relationships as easy as possible.
code :
public function reviews(){

    return $this->hasMany('App\Review');
}
protected $appends = ['rate'];
public functions getRateAttribute(){

     return $this->reviews->avg('rate') ?? 0;
}
$product = Product::first();
public functions getRate(){

     return $this->reviews->avg('rate') ?? 0;
}
$product = Product::first();

// The query fo average will be done below only when you call `getRate` on `$product`

$rate = $product->getRate();
Related Posts Related Posts :
  • Which is the best php opensource e-commerce for handle 5 million prducts import daily?
  • $_SERVER['HTTP_X_REQUESTED_WITH'] seemingly not available on PHP 5.1.6
  • radio value lost by $_post array
  • Tool to add code highlighting and export to HTML
  • youtube video downloaded successfully on local, but on server(online) only 0 byte flv is downloading
  • Populating a Dropdown list in PHP dynamically
  • Why is turning magic_quotes_gpc on considered a bad practice?
  • Replace html text in javascript include
  • OOP Design - Where/When do you Validate properties?
  • RSA Encrypt in PHP to decrypt in .NET
  • Encrypting with Perl CBC and decrypting with PHP mcrypt
  • Code equivalence between Javascript and PHP
  • How to track DHL in our site from DHL site , by PHP
  • php resize image
  • How to use ^/$ if it's already used as a delimiter for regex in PHP?
  • Nobody owner (99 99) in FTP caused by php functions?
  • Php deployment to remote server using FTP from SVN after each commit
  • PHP: upload image max 150x150
  • How to implement Gmail OAuth API to send email (especially via SMTP)?
  • PHP comparion doesnt work..why?
  • Curl redirect,, not working?
  • SCORM lib for php
  • After form validation, how do you redirect to a new url w/ PHP
  • A solid nickname regexp
  • SVN checkout browser
  • Redirecting a large number of URLs with htaccess or php header
  • mail sent with php's mail() has blank From field
  • PHP intersection between array and object
  • Cannot get new product attribute in grid display
  • How do you make a count that counts the number of page-views? (PHP)
  • Reading data from a MySQL database into a HTML Table?
  • How to test PHP in MySQL Strict
  • Using a database class in my user class
  • C++ equivalent of PHP's pack()
  • splitting strings in php
  • Strange bug with PHP on Windows 7
  • PHP array question
  • How to get the age from a birthdate using PHP & MySQL?
  • Setting post tags in wordpress via XMLRPC API when submitting a post?
  • Generating random thumbnails with PHP+FFMPEG
  • what are the difference between for loop & for each loop in php
  • PHP FTP Upload thousands of files
  • How to get structed result using nested set in MySQL and PHP?
  • MYSQL Select and group by date
  • Detect base64 encoding in PHP?
  • Simple PHP GET question
  • What's the meaning of the PHP token name T_PAAMAYIM_NEKUDOTAYIM?
  • jquery GET and POST confusion
  • PNGs alpha transparancy in AS3 - Unknown file-type
  • how to use htaccess redirectmatch with query_string?
  • Creating a numerical order index on a MySQL table
  • mobile browsers' can't login to my site
  • Split PDF documents into separate pages using PHP (or possibly perl)
  • How to retrieve the value from Select html element using JS prototype in php?
  • How to execute Imagemagick in PHP
  • How do you make a function read form a txt file and store random lines in a variable?
  • Use fgetcsv for tab delimited file
  • PHP: What's the best way to check equality of $_SERVER['HTTP_REFERER']?
  • best way to escape and create a slug
  • Searching for a complex and well-designed PHP OOP application to learn from
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk