laravel

Laravel eager loading with nested relationship

眉间皱痕 提交于 2021-02-17 03:09:44
问题 I know this question has been asked but my situation is different. I have Post model with relationship to Comment model defined: /*Post Model*/ public function comments(){ return $this->hasMany('comment'); } and Comment model which each comment belong to one user : / comment model / public function user(){ return $this->belongto('user'); } now I want to query all post and eager load comments (of each post) along with user information who post the comment. anyway to make it work please ? thank

Laravel eager loading with nested relationship

别等时光非礼了梦想. 提交于 2021-02-17 03:08:03
问题 I know this question has been asked but my situation is different. I have Post model with relationship to Comment model defined: /*Post Model*/ public function comments(){ return $this->hasMany('comment'); } and Comment model which each comment belong to one user : / comment model / public function user(){ return $this->belongto('user'); } now I want to query all post and eager load comments (of each post) along with user information who post the comment. anyway to make it work please ? thank

User Class not found in Laravel 5

亡梦爱人 提交于 2021-02-17 03:05:50
问题 I have got the following Problem. I have just upgraded to Laravel 5 from 4.2 but my UserController which I first just copied to the new Controllers folder does not work. It always tells me that it does not fiend the User Model. My Code looked like the following when I copied it: UserController.php <?php use App\Transformer\UserTransformer; class UserController extends ApiController { public function index() { App::abort(403, 'This is not allowed'); } public function show($id) { $user = User:

User Class not found in Laravel 5

风流意气都作罢 提交于 2021-02-17 03:04:09
问题 I have got the following Problem. I have just upgraded to Laravel 5 from 4.2 but my UserController which I first just copied to the new Controllers folder does not work. It always tells me that it does not fiend the User Model. My Code looked like the following when I copied it: UserController.php <?php use App\Transformer\UserTransformer; class UserController extends ApiController { public function index() { App::abort(403, 'This is not allowed'); } public function show($id) { $user = User:

Generate Model, Controller outside of app folder in Laravel

喜夏-厌秋 提交于 2021-02-17 02:34:33
问题 How to create controller and model files outside of app folder. For example, when we do php artisan make:model ModelName it creates Model file inside app folder. When we specify namespace like php artisan make:model SomeFolder/ModelName it creates Model file inside app/SomeFolder/ModelName. I wanted to create model files outside of app folder. How to achieve this? 回答1: I don't see the reason, but I managed to make it on my local computer by php artisan make:model ../../mOdel . Probably in

Generate Model, Controller outside of app folder in Laravel

二次信任 提交于 2021-02-17 02:32:30
问题 How to create controller and model files outside of app folder. For example, when we do php artisan make:model ModelName it creates Model file inside app folder. When we specify namespace like php artisan make:model SomeFolder/ModelName it creates Model file inside app/SomeFolder/ModelName. I wanted to create model files outside of app folder. How to achieve this? 回答1: I don't see the reason, but I managed to make it on my local computer by php artisan make:model ../../mOdel . Probably in

How to catch PostTooLargeException in Laravel?

不打扰是莪最后的温柔 提交于 2021-02-17 02:29:47
问题 To reproduce the error, simply upload a file(s) to any POST routes in Laravel that exceeds the post_max_size in your php.ini configuration. My goal is to simply catch the error so I can inform the user that the file(s) he uploaded is too large. Say: public function postUploadAvatar(Request $request) try { // Do something with $request->get('avatar') // Maybe validate file, store, whatever. } catch (PostTooLargeException $e) { return 'File too large!'; } } The above code is in standard Laravel

How to get and display an image from a guzzle request

╄→гoц情女王★ 提交于 2021-02-16 18:53:06
问题 I am trying to display an image from a guzzle request, everything worked fine but I am failing to display the image. This is the result I get, when trying to get users' data object(GuzzleHttp\Psr7\Response)#427 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(200) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(14) { ["Server"]=> array(1) { [0]=> string(6) "Cowboy" } ["Connection"]=> array(1) { [0]=>

Invalidate login in Laravel when user leaves the page

蹲街弑〆低调 提交于 2021-02-16 17:57:50
问题 It seems a small and easy to solve problem, but I cant find any solution for it. I want to login the users only for that session while the are browsing the site. As soon as they close the tab I want to make their session expired. As much as I know I can't do this in the built-in Auth class. How could I do this efficiently? 回答1: Short answer: YOU CAN'T The session can be destroyed when the entire browser is closed by simply setting expire_on_close in config/session.php (also make sure you

Laravel sorting last record

落爺英雄遲暮 提交于 2021-02-16 15:34:34
问题 I'm bulding a forum. In the main page, I want to show the last reply for each forum. The hierarchy is this: forums hasMany('App\Topic') hasManyThrough('App\Topic', 'App\Repley') topics belongsTo('App\Forum') hasMany('App\Reply') replies belongsTo('App\Topic') I did this: $forum->replies()->orderBy('created_at', 'desc')->first() And it worked. But I want to sort by topics and replies. If I post new topic after the last reply, I want that show as the last reply. Update : I did this, and it work