首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Laravel管理主页横幅的最佳方法是什么?

用Laravel管理主页横幅的最佳方法是什么?
EN

Stack Overflow用户
提问于 2021-05-24 08:08:34
回答 1查看 1.2K关注 0票数 1

这是我主页上的横幅结构:

如你所见,我有4节的横幅--小横幅,中等横幅,新闻横幅,大横幅

我有一个叫做Banner的模型,有4个控制器来管理这个横幅,还有4个表来保存数据。

这是Banner模型:

代码语言:javascript
复制
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    protected $fillable = [
        'title', 'image', 'url', 'image_title', 'image_alt'
    ];
}

及管制员:

SmallController :

代码语言:javascript
复制
class SmallController extends Controller
{
    public function small_list()
    {
        $smallBanners = DB::table('small_banner')->get();
        return view('admin.banners.small.list', compact('smallBanners'));
    }

    public function small_create()
    {
        return view('admin.banners.small.add');
    }

    public function small_store(Request $request)
    {
        $data = $request->validate([
            'title' => 'required',
            'url' => 'required',
            'image' => 'required',
            'image_title' => 'max:255',
            'image_alt' => 'max:255'
        ]);
        DB::table('small_banner')->insert($data);
        return redirect(route('admin.banners.small.index'));
    }

    public function small_edit($id)
    {
        $small = DB::table('small_banner')->where('id', $id)->first();
        return view('admin.banners.small.edit', compact('small'));
    }

    public function small_update(Request $request, $id)
    {
        $small = DB::table('small_banner')->where('id', $id)->first();
        if ($request->has('image')) {
            if (file_exists($small->image)) {
                unlink($small->image);
            }
            DB::table('small_banner')->where('id', $id)->update([
                'image' => $request['image']
            ]);
        }
        DB::table('small_banner')->where('id', $id)->update([
            'title' => $request['title'],
            'url' => $request['url'],
            'image_title' => $request['image_title'],
            'image_alt' => $request['image_alt']
        ]);
        return redirect(route('admin.banners.small.index'));
    }

    public function small_delete($id)
    {
        $small = DB::table('small_banner')->where('id', $id)->first();
        DB::table('small_banner')->where('id', $id)->delete();
        if (file_exists($small->image)) {
        unlink($small->image);
        }
        return redirect(route('admin.banners.small.index'));
    }
}

其他控制器类似于SmallController

我就是这样展示这些横幅的:

代码语言:javascript
复制
@foreach($smallBanners as $small)
        <div class="col-6 col-lg-3">
             <div class="widget-banner card">
                  <a href="{{ $small->url }}" target="_blank" rel="noopener">
                     <img class="img-fluid w-100" loading="lazy"
                          src="{{ $small->image }}" title="{{ $small->title }}"
                          alt="{{ $small->image_alt }}" width="350" height="200">
                  </a>
              </div>
         </div>
@endforeach

其他景观,比如小横幅。

但在这种情况下,例如在小横幅,如果我们上传5个图像,而不是4个图像,结构将混乱。

管理这些横幅和优化代码的最佳方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-24 09:58:43

让我们回到这个概念,从减少表的使用开始,或者您可以继续使用您的概念

让我们将结构改为

表:banners

栏:

代码语言:javascript
复制
$table->increments('id');
$table->string('title');
$table->string('image');
$table->string('url');
$table->string('image_title')->nullable(); //guessing from validator that it can be null
$table->string('image_alt')->nullable();
//extra columns
$table->enums('banner_type', ['small', 'medium', 'large', 'news']);
//or
$table->string('banner_type');
$table->boolean('isActive')->default(0);

你有模型,但不用它

代码语言:javascript
复制
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    protected $table = 'banners'; //add this line to define table name, make sure you have set the database config in .env
    protected $fillable = [
        'title', 'image', 'url', 'image_title', 'image_alt', 'banner_type', 'isActive'
    ];
}

现在,将用于管理横幅的控制器简化为一个控制器

代码语言:javascript
复制
use Banner;

class BannerController extends Controller
{
    public function index()
    {
        $banners = Banner::get();
        return view('admin.banners.index', compact('banners'));
    }

    public function create()
    {
        return view('admin.banners.create');
    }

    public function store_count($request, $type)
    {
        //using array limit
        return Banner::where('banner_type', $type)
        ->where('isActive', 1)->count() < $this->limits[$type] && $request->isActive == 1;
    }

    public function update_count($banner, $type)
    {
        return Banner::whereNotIn('id', [$banner->id])
        ->where('isActive', 1)
        ->where('type', $banner->banner_type)->count() < $this->limits[$type] && $banner->isActive == 1;
    }

    public function store(Request $request)
    {
        //validating form data
        $data = $request->validate([
            'title' => "required",
            'url' => "required",
            'image' => "required",
            'image_title' => "max:255",
            'image_alt' => "max:255",
            'banner_type' => "required|in:small,medium,large,news",
            'isActive' => "nullable|in:0,1" //active or not
        ]);

        //validating images active count
        if (!$this->store_count($request, $request->banner_type)) {
        return redirect()->back()->withInput($request->all())
            ->withErrors(['isActive' => ' نمیتوان بیشتر از ' . $this->limits[$request['banner_type']] . ' عکس برای این بنر آپلود کرد! ']);
    }

        Banner::create($data);
        return redirect(route('admin.banners.index'));
    }

    public function show($id)
    {
        $banner = Banner::findOrFail($id);
        return view('admin.banners.edit', compact('banner'));
    }

    public function update(Request $request, $id)
    {
        $banner = Banner::findOrFail($id);
        //validate update form data here
        //your validation
        //validating images active count
        if(!$this->update_count($banner, $request->banner_type)){
            return redirect()->back()
           ->withInput($request->all())
           ->withErrors(['isActive' => 'There cant be more than '.$this->limits[$request['banner_type']].' images active');
        }
        $banner = $banner->fill([
            'title' => $request['title'],
            'url' => $request['url'],
            'image_title' => $request['image_title'],
            'image_alt' => $request['image_alt'],
            'banner_type' => $request['banner_type'],
            'isActive' => $request['isActive'] ?? 0
        ]);
        if ($request->has('image')) {
            if (file_exists($banner->image)) {
                unlink($banner->image);
            }
            $banner->image = $request['image'];
        }
        $banner->update();
        return redirect(route('admin.banners.index'));
    }

    public function delete($id)
    {
        $banner = Banner::findOrFail($id);
        if (file_exists($banner->image)) {
           unlink($banner->image);
        }
        $banner->delete();

        return redirect(route('admin.banners.index'));
    }
}

现在我们设置代码来选择哪些映像是活动的,您可以使用ajax方法或者使用上面的控制器。

代码语言:javascript
复制
public function set_active($id)
{
     $banner = Banner::findOrFail($id);
     $this->validate_count((new Request([])), $banner->banner_type);
     $banner->update(['isActive' => 1]);
     return redirect(route('admin.banners.index'));
}

//you can use array if want to set different limit of banner type, put it as public variable inside controller class
public $limits = [
    'small' => 4,
    'medium' => 4,
    'large' => 4,
    'news' => 4
];

将数据资源加载到视图

代码语言:javascript
复制
public class home()
{
    $small = Banner::where('banner_type', 'small')
    ->where('isActive', 1)->get();
    $medium = Banner::where('banner_type', 'medium')
    ->where('isActive', 1)->get();
    $large = Banner::where('banner_type', 'large')
    ->where('isActive', 1)->get();
    $news = Banner::where('banner_type', 'news')
    ->where('isActive', 1)->get();
   return view('home', compact('small', 'medium', 'large', 'news'));
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67668653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档