eloquent

Using Laravel Facades outside Laravel

蹲街弑〆低调 提交于 2019-12-23 15:36:50
问题 I have a Laravel application which I use as an API to a much larger application built in Joomla. I really love using Laravel and decided to use Eloquent within the Joomla application. I got this working by importing the bootstrap\autoload.php file in the Laravel application and creating a Capsule require JPATH_ROOT.'/../laravel_app/bootstrap/autoload.php'; $capsule = new Capsule(); $config = new JConfig(); $capsule->addConnection([ 'driver' => 'mysql', 'host' => $config->host, 'database' =>

Accessing nested relationship with Laravel 4

若如初见. 提交于 2019-12-23 15:24:50
问题 I'm having trouble figuring out how to access a nested relationship within Laravel. The specific example I have is a Movie that has many entires in my Cast table which has one entry in my People table. These are my models: MOVIE class Movie extends Eloquent { protected $primaryKey = 'movie_id'; protected $table = 'movie'; // Relationships public function cast() { return $this->hasMany('MovieCast', 'movie_id'); } } MOVIECAST class MovieCast extends Eloquent { protected $table = 'movie_cast';

Reserved word on eloquent models

旧时模样 提交于 2019-12-23 13:57:29
问题 I have a Product model and a Attribute model. Products have Attributes. Problem is I cant use Attributes as a member of a Product class that extends eloquent as eloquent uses this already. Is there a way around this? class Product extends Model { protected $attributes; // ... } 回答1: You won't get around renaming your relation. e.g. productAttributes . The problem here is, that the attributes property is used a lot by Eloquent and there is no way to change that by configuration or overriding a

Laravel NotFoundHttpException although route exists

醉酒当歌 提交于 2019-12-23 13:28:33
问题 I use vue.js and Laravel 5.1 to create a little file sharing application. Everything works perfect but now I wanted to make sure the owner of each file is able to remove users from his file (he had to share the file with those users at first of course), therefore I make a PUT request to an URL called /files/share . My Laravel route looks like this: Route::put('/files/share', 'FileController@test'); When I run php artisan route:list it gets listed as well. The client-side code looks like this:

Laravel NotFoundHttpException although route exists

时光怂恿深爱的人放手 提交于 2019-12-23 13:27:37
问题 I use vue.js and Laravel 5.1 to create a little file sharing application. Everything works perfect but now I wanted to make sure the owner of each file is able to remove users from his file (he had to share the file with those users at first of course), therefore I make a PUT request to an URL called /files/share . My Laravel route looks like this: Route::put('/files/share', 'FileController@test'); When I run php artisan route:list it gets listed as well. The client-side code looks like this:

Type Hinting Eloquent Models

穿精又带淫゛_ 提交于 2019-12-23 12:44:48
问题 I am trying to write good code, and part of that is type hinting to make it easier for work down the line and to force expectations. This may seem a little contrived, but its more of a proof of concept for me. I am writing a class to take a TSV file split on tabs and insert into my Model. In my constructor I was asking for: Illuminate\Database\Eloquent\Model To which I passed: new \App\Model() And finally the error response of: instance of App\Model given Clearly I have done something wrong,

Laravel Eloquent toArray not using square braces

我只是一个虾纸丫 提交于 2019-12-23 12:23:18
问题 I'm writing an api and attempting to convert a number of results into JSON. When the eloquent result is converted to an array, I am expecting something like this: [ { "id": "0", ... }, { "id": "", ... }, ] but instead, Laravel displays it as a key, value list using the table key: "0":{ { "id": "0", ... } }, "1":{ { "id": "1", ... } } Here is the function: $results = \App\Event::with('sandboxes')->get()->sortBy('start_time')->forPage($pageNum,$perPage); return response()->json(array( "events"

Laravel Eloquent find returning null

你。 提交于 2019-12-23 12:11:20
问题 I'm trying to retrieve a record from my database using Eloquents find method, however, it's unexpectedly returning null. If I run the query manually on the database then it returns the expected record. I'm using the following in Laravel: $support = Support::find(02155); And the following directly on the database: SELECT * FROM support WHERE id = 02155; The primary key column is named 'id' with type smallint(5), unsigned and zerofill along with auto increment set. I based the above 'manual'

how to make query with substr in eloquent (laravel 4)?

末鹿安然 提交于 2019-12-23 09:49:18
问题 I have this query: select substr(id,1,4) as id from meteo.a2012 group by substr(id,1,4) I just want to take first 4 numbers to my id row, but I'm trying to do in eloquent, how I do? Thanks. 回答1: You need to use raw expressions so you can use special functions like that. Model::select(DB::raw('substr(id, 1, 4) as id'))->groupBy(DB::raw('substr(id, 1, 4)'))->get(); Where Model is your Eloquent model you want to run the query on. 回答2: $ids = Model::get(['id']); foreach ($ids as $str) { $str->id

Laravel Eloquent orWhere Query

牧云@^-^@ 提交于 2019-12-23 08:07:46
问题 Can someone show me how to write this query in Eloquent? SELECT * FROM `projects` WHERE `id`='17' OR `id`='19' I am thinking Project::where('id','=','17') ->orWhere('id','=','19') ->get(); Also my variables (17 and 19) in this case are coming from a multi select box, so basically in an array. Any clues on how to cycle through that and add these where/orWhere clauses dynamically? Thanks. 回答1: You could do in three ways. Assume you've an array in the form ['myselect' => [11, 15, 17, 19],