Laravel Archives - Thoughts https://blog.jstassen.com/tag/laravel/ A place for collecting my miscellaneous thoughts. Thu, 01 Oct 2020 16:33:11 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.7 92836340 Remove public directory for laravel https://blog.jstassen.com/2015/04/remove-public-directory-laravel/ https://blog.jstassen.com/2015/04/remove-public-directory-laravel/#respond Wed, 01 Apr 2015 16:16:00 +0000 http://blog.jstassen.com/?p=384 To get rid of the /public you can either: A) Upload vender, bootstrap, and app folders on directory higher than the htdocs or www folder. Then place all files in the public folder to the into htdocs or www. B) Move files in public directory one folder higher, modify index.php removing the up directory “../” … Continue reading Remove public directory for laravel →

The post Remove public directory for laravel appeared first on Thoughts.

]]>
To get rid of the /public you can either:
A) Upload vender, bootstrap, and app folders on directory higher than the htdocs or www folder. Then place all files in the public folder to the into htdocs or www.

B) Move files in public directory one folder higher, modify index.php removing the up directory “../” from the includes.

C) Make an .htaccess file in the root something to the effect of

RewriteEngine On

RewriteRule ^(.*)$ public/$1 [L]

The post Remove public directory for laravel appeared first on Thoughts.

]]>
https://blog.jstassen.com/2015/04/remove-public-directory-laravel/feed/ 0 384
Code: Laravel Scope Query on a Model https://blog.jstassen.com/2015/03/code-laravel-scope-query-on-a-model/ https://blog.jstassen.com/2015/03/code-laravel-scope-query-on-a-model/#respond Sun, 08 Mar 2015 03:28:40 +0000 http://blog.jstassen.com/?p=514 Let’s talk a bit about scoped queries in Laravel. First here is a basic Book Eloquent Model with two handy scopes for type and fewerPagesThan. //Book Model class Book extends Eloquent { public function scopeType( $query, $type ) { return $query->where( 'books.type', '=', $type ); } public function scopeFewerPagesThan( $query, $pages ) { return $query->where( … Continue reading Code: Laravel Scope Query on a Model →

The post Code: Laravel Scope Query on a Model appeared first on Thoughts.

]]>
Let’s talk a bit about scoped queries in Laravel. First here is a basic Book Eloquent Model with two handy scopes for type and fewerPagesThan.

//Book Model
class Book extends Eloquent {
public function scopeType( $query, $type ) {
return $query->where( 'books.type', '=', $type );
}

public function scopeFewerPagesThan( $query, $pages ) {
return $query->where( 'books.pages', '<=', $pages ); } }

Using scoping

I often see confusion when we try to use scoping. Some would expect the following to give them back all 'autobiography' books:


Book::type('autobiography'); // This works right? Nope!

But it doesn't. Scoping functions will only refine your query. For instance all the following are great examples of using scoping:


Book::type('autobiography')->get();
Book::with('author')->type('autobiography')->get();
Book::fewerPagesThan(500)->type('autobiography')->get();

Building up scopes

When using scopes to refine and filter down which Models to retrieve, you will always need to add a ->get() at the end. This allows you can chain multiple "scopes" onto each other as in the 3rd example. Basically it needs to know when you are done filtering down and when to actually hit the database.

So the 3rd example could be broken down like this to really drive home the point:


$maxPages = 500;

$books = new Book();
$books->type('autobiography');
if( $maxPages )
$books->fewerPagesThan($maxPages);
$opportunities->get();

In every case before the ->get() call $books var is a Eloquent/Builder that is being built up.

After the ->get() call $books var gets overwritten with an Eloquent/Collection that has been filled with our data from the database.

This is a very powerful and flexible to way to scope and filter down models that are in your database. It is better to filter them down with scopes in the DB query than filter down a collection that has already been loaded into memory. Make the your DB do some weight lifting for you. 😉

The post Code: Laravel Scope Query on a Model appeared first on Thoughts.

]]>
https://blog.jstassen.com/2015/03/code-laravel-scope-query-on-a-model/feed/ 0 514
Code: Getting multiple specific Laravel models https://blog.jstassen.com/2014/06/code-getting-multiple-specific-laravel-models/ https://blog.jstassen.com/2014/06/code-getting-multiple-specific-laravel-models/#respond Thu, 19 Jun 2014 17:32:24 +0000 http://blog.jstassen.com/?p=397 I had a case where I had an array of model ids, and I wanted to return a collection of these models. That is I had a list of model ids, but it appeared no nice way to get all of them at once. However from the 4.2 API docs you can see that the find() … Continue reading Code: Getting multiple specific Laravel models →

The post Code: Getting multiple specific Laravel models appeared first on Thoughts.

]]>
I had a case where I had an array of model ids, and I wanted to return a collection of these models. That is I had a list of model ids, but it appeared no nice way to get all of them at once. However from the 4.2 API docs you can see that the find() should be able to return either a Model, or a Collection of models.

Find()

Naturally to get one model just use:
User::find( $id );
You could loop over the ::find() to pull each one manually, but that will result in multiple unnecessary queries to the database right? Who wants that?

Find() with Array

Here is a quite simple approach that Eloquent supports that seems quite obvious after you see it. You can just pass an array of model ids right to the find() function!

$arr = array($id1, $id2, $id3);
User::find( $arr );

This will return a collection of models just as you would expect, no fussing around with loops.  A simple way to get Multiple Specific Laravel Models by Id.

The post Code: Getting multiple specific Laravel models appeared first on Thoughts.

]]>
https://blog.jstassen.com/2014/06/code-getting-multiple-specific-laravel-models/feed/ 0 397